拖动视图,视图的父视图根据情况变化.

来源:互联网 发布:php手机号正则表达式 编辑:程序博客网 时间:2024/06/05 12:01


#import "ViewController.h"

#import "UIView+Clone.h"


@interface ViewController ()

{

   UIView *viewA;

   UILabel *labelA;

   UIView *viewB;

   UIView *viewC;

}

@end


@implementation ViewController


- (void)viewDidLoad

{

    [superviewDidLoad];

    viewA = [[UIViewalloc] initWithFrame:CGRectMake(50, 50, 150, 150)];

    viewA.userInteractionEnabled =YES;

    viewA.backgroundColor = [UIColorblueColor];

    [self.viewaddSubview:viewA];

    

    labelA = [[UILabelalloc] initWithFrame:CGRectMake(50, 50, 50, 50)];

    labelA.text =@"A";

   labelA.tag = 100;

    labelA.userInteractionEnabled =YES;

    labelA.backgroundColor = [UIColorredColor];

    [viewA addSubview:labelA];

    

    viewB = [[UIViewalloc] initWithFrame:CGRectMake(50, 300, 150, 150)];

    viewB.backgroundColor = [UIColorgreenColor];

    [self.viewaddSubview:viewB];

    

    

    viewC = [[UIViewalloc] initWithFrame:CGRectMake(0, 0, 100, 100)];

    viewC.backgroundColor = [UIColorpurpleColor];

    [viewB addSubview:viewC];

    

    UILongPressGestureRecognizer *longGes = [[UILongPressGestureRecognizeralloc] initWithTarget:selfaction:@selector(longPressed:)];

    [labelA addGestureRecognizer:longGes];

}


- (void)longPressed:(UILongPressGestureRecognizer *)longGes

{

   static NSInteger times = 0;

   if (times == 0) {

       UIView *longGesView = [longGes view];

       UIView *copyView = [longGesView clone];

        copyView.frame = longGesView.frame;

        copyView.tag = 101;

        [viewAaddSubview:copyView];

        UIPanGestureRecognizer *pan = [[UIPanGestureRecognizeralloc] initWithTarget:selfaction:@selector(moved:)];

        [copyViewaddGestureRecognizer:pan];

        times ++;

    }

}


- (void)moved:(UIPanGestureRecognizer *)gestureRecognizer{

   UIView *piece = [gestureRecognizer view];

    [selfadjustAnchorPointForGestureRecognizer:gestureRecognizer];

    if ([gestureRecognizerstate] == UIGestureRecognizerStateBegan || [gestureRecognizerstate] == UIGestureRecognizerStateChanged) {

       CGPoint translation = [gestureRecognizer translationInView:[piece superview]];

        [piecesetCenter:CGPointMake([piececenter].x + translation.x, [piececenter].y + translation.y)];

        [gestureRecognizersetTranslation:CGPointZeroinView:[piece superview]];

    }

    [selfmoveView:piece fromView:viewAtoView:viewConBaseView:self.view];

}


- (void)moveView:(UIView *)moveView fromView:(UIView *)aView toView:(UIView *)bView onBaseView:(UIView *)baseView

{

   CGRect moveFrame = CGRectMake(0, 0, 0, 0);

   if (moveView.superview == aView) {

        moveFrame = [aViewconvertRect:moveView.frametoView:baseView];

    }elseif(moveView.superview == bView){

        moveFrame = [bViewconvertRect:moveView.frametoView:baseView];

    }else{

        moveFrame = moveView.frame;

    }

   CGRect aViewRect = [aView.superviewconvertRect:aView.frametoView:baseView];

   CGRect bViewRect = [bView.superviewconvertRect:bView.frametoView:baseView];

   if(CGRectContainsRect(aViewRect,moveFrame)){

       if ([aView viewWithTag:moveView.tag] ==nil) {

           CGRect viewAFrame = [baseView convertRect:moveView.frame toView:aView];

            moveView.frame = viewAFrame;

            [aViewaddSubview:moveView];

        }

    }elseif (CGRectContainsRect(bViewRect, moveFrame)) {

       if ([bView viewWithTag:moveView.tag] ==nil) {

           CGRect viewAFrame = [baseView convertRect:moveView.frame toView:bView];

            moveView.frame = viewAFrame;

            [bViewaddSubview:moveView];

        }

    }else{

        //判断父视图上有没有labelA.

       BOOL hasLabelA = NO;

       for (UIView *viewin [baseView subviews]) {

           if (view.tag == moveView.tag) {

                hasLabelA =YES;

               break;

            }

        }

       if (hasLabelA == NO) {

           if (moveView.superview == aView) {

               CGRect viewAFrame = [aView convertRect:moveView.frame toView:baseView];

                moveView.frame = viewAFrame;

            }elseif(moveView.superview == bView){

               CGRect viewBFrame = [bView convertRect:moveView.frame toView:baseView];

                moveView.frame = viewBFrame;

            }

            [baseViewaddSubview:moveView];

        }

    }

}


- (void)adjustAnchorPointForGestureRecognizer:(UIGestureRecognizer *)gestureRecognizer {

    if (gestureRecognizer.state ==UIGestureRecognizerStateBegan) {

       UIView *piece = gestureRecognizer.view;

       CGPoint locationInView = [gestureRecognizer locationInView:piece];

       NSLog(@"%f,%f",locationInView.x,locationInView.y);

       CGPoint locationInSuperview = [gestureRecognizerlocationInView:piece.superview];

       NSLog(@"%f,%f",locationInSuperview.x,locationInSuperview.y);

        piece.layer.anchorPoint =CGPointMake(locationInView.x / piece.bounds.size.width, locationInView.y / piece.bounds.size.height);

        piece.center = locationInSuperview;

    }

}


复制视图的方法:

- (id) clone {

    NSData *archivedViewData = [NSKeyedArchiverarchivedDataWithRootObject:self];

    id clone = [NSKeyedUnarchiverunarchiveObjectWithData:archivedViewData];

   return clone;

}



原创粉丝点击