#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;
}