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


#import "ViewController.h"

#import "UIView+Clone.h"


@interface ViewController ()

{

    UIView *viewA;

    UILabel *labelA;

    UIView *viewB;

    UIView *viewC;

}

@end


@implementation ViewController


- (void)viewDidLoad

{

    [super viewDidLoad];

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

    viewA.userInteractionEnabled = YES;

    viewA.backgroundColor = [UIColor blueColor];

    [self.view addSubview:viewA];

    

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

    labelA.text = @"A";

    labelA.tag = 100;

    labelA.userInteractionEnabled = YES;

    labelA.backgroundColor = [UIColor redColor];

    [viewA addSubview:labelA];

    

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

    viewB.backgroundColor = [UIColor greenColor];

    [self.view addSubview:viewB];

    

    

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

    viewC.backgroundColor = [UIColor purpleColor];

    [viewB addSubview:viewC];

    

    UILongPressGestureRecognizer *longGes = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@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;

        [viewA addSubview:copyView];

        UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(moved:)];

        [copyView addGestureRecognizer:pan];

        times ++;

    }

}


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

    UIView *piece = [gestureRecognizer view];

    [self adjustAnchorPointForGestureRecognizer:gestureRecognizer];

    if ([gestureRecognizer state] == UIGestureRecognizerStateBegan || [gestureRecognizer state] == UIGestureRecognizerStateChanged) {

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

        [piece setCenter:CGPointMake([piece center].x + translation.x, [piece center].y + translation.y)];

        [gestureRecognizer setTranslation:CGPointZero inView:[piece superview]];

    }

    [self moveView:piece fromView:viewA toView:viewC onBaseView: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 = [aView convertRect:moveView.frame toView:baseView];

    }else if(moveView.superview == bView){

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

    }else{

        moveFrame = moveView.frame;

    }

    CGRect aViewRect = [aView.superview convertRect:aView.frame toView:baseView];

    CGRect bViewRect = [bView.superview convertRect:bView.frame toView:baseView];

    if(CGRectContainsRect(aViewRect,moveFrame)){

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

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

            moveView.frame = viewAFrame;

            [aView addSubview:moveView];

        }

    }else if (CGRectContainsRect(bViewRect, moveFrame)) {

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

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

            moveView.frame = viewAFrame;

            [bView addSubview:moveView];

        }

    }else{

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

        BOOL hasLabelA = NO;

        for (UIView *view in [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;

            }else if(moveView.superview == bView){

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

                moveView.frame = viewBFrame;

            }

            [baseView addSubview: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 = [gestureRecognizer locationInView: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 = [NSKeyedArchiver archivedDataWithRootObject:self];

    id clone = [NSKeyedUnarchiver unarchiveObjectWithData:archivedViewData];

    return clone;

}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值