最近遇到一個問題,用Masonry寫的佈局;佈局
拖動其中某個view,拖動方法按傳統的寫成以下形式。若是view中的label更改text值,拖動以後的view就會回到最初被設定的位置。atom
- (void)objectDidDragged:(UIPanGestureRecognizer *)paramSender { if (paramSender.state != UIGestureRecognizerStateEnded && paramSender.state != UIGestureRecognizerStateFailed){ //經過使用 locationInView 這個方法,來獲取到手勢的座標 CGPoint location = [paramSender locationInView:paramSender.view.superview]; paramSender.view.center = location; } }
經試驗後,拖動方法需改成以下所示:spa
// // ViewController.m // PanGesTest // // Created by Vivien on 16/9/18. // Copyright © 2016年 Vivien. All rights reserved. // #import "Masonry.h" #import "ViewController.h" @interface ViewController () { NSTimer *timer ; int count; CGPoint panPoint; } @property (strong, nonatomic) UIView *panView; @property (strong, nonatomic) UILabel *countLabel; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. _panView = [[UIView alloc]init]; _panView.backgroundColor = [UIColor grayColor]; [self.view addSubview:_panView]; UIPanGestureRecognizer *panGR = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(objectDidDragged:)]; //限定操做的觸點數 [panGR setMaximumNumberOfTouches:1]; [panGR setMinimumNumberOfTouches:1]; //將手勢添加到draggableObj裏 [_panView addGestureRecognizer:panGR]; [_panView mas_remakeConstraints:^(MASConstraintMaker *make) { make.top.mas_equalTo(self.view).offset(10); make.left.mas_equalTo(self.view).offset(30); make.width.mas_equalTo(150); make.height.mas_equalTo(50); }]; _countLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 20)]; [_countLabel setText:@"00000"]; [_countLabel setTextColor:[UIColor redColor]]; [_countLabel setTextAlignment:NSTextAlignmentCenter]; [_countLabel setFont:[UIFont systemFontOfSize:13]]; [_panView addSubview:_countLabel]; [_countLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.left.right.top.mas_equalTo(_panView); make.height.mas_equalTo(20); }]; timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(countAdd) userInfo:nil repeats:YES]; [timer fire]; count = 0; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (void)objectDidDragged:(UIPanGestureRecognizer *)sender { if (sender.state == UIGestureRecognizerStateBegan) { panPoint = [sender locationInView:_panView]; NSLog(@"panPoint:%@",NSStringFromCGPoint(panPoint)); } if (sender.state != UIGestureRecognizerStateEnded && sender.state != UIGestureRecognizerStateFailed) { CGPoint inViewLoction = [sender locationInView:self.view];//sender.view.superview CGPoint location = [sender translationInView:sender.view.superview]; NSLog(@"locationInView:%@,translationInView:%@",NSStringFromCGPoint(inViewLoction),NSStringFromCGPoint(location)); // sender.view.center = inViewLoction; [_panView mas_remakeConstraints:^(MASConstraintMaker *make) { make.width.mas_equalTo(150); make.height.mas_equalTo(50); make.left.mas_equalTo(inViewLoction.x-panPoint.x); make.top.mas_equalTo(inViewLoction.y-panPoint.y); // make.left.mas_equalTo(0).offset(inViewLoction.x-panPoint.x); // make.top.mas_equalTo(0).offset(inViewLoction.y-panPoint.y); }]; NSLog(@"className:%@",NSStringFromClass([sender.view.superview class])); [sender setTranslation:CGPointZero inView:self.view]; } } - (void)countAdd { count ++; [_countLabel setText:[NSString stringWithFormat:@"%d",count]]; } @end