Masonry+拖动

时间:2022-08-04 01:02:25

最近遇到一个问题,用Masonry写的布局;

拖动其中某个view,拖动方法按传统的写成如下形式。如果view中的label更改text值,拖动之后的view就会回到最初被设定的位置。

- (void)objectDidDragged:(UIPanGestureRecognizer *)paramSender {

    if (paramSender.state != UIGestureRecognizerStateEnded && paramSender.state != UIGestureRecognizerStateFailed){
//通过使用 locationInView 这个方法,来获取到手势的坐标
CGPoint location = [paramSender locationInView:paramSender.view.superview];
paramSender.view.center = location;
}
}

经试验后,拖动方法需改为如下所示:

//
// 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:];
[panGR setMinimumNumberOfTouches:];
//将手势添加到draggableObj里
[_panView addGestureRecognizer:panGR]; [_panView mas_remakeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(self.view).offset();
make.left.mas_equalTo(self.view).offset();
make.width.mas_equalTo();
make.height.mas_equalTo();
}]; _countLabel = [[UILabel alloc]initWithFrame:CGRectMake(, , , )];
[_countLabel setText:@""];
[_countLabel setTextColor:[UIColor redColor]];
[_countLabel setTextAlignment:NSTextAlignmentCenter];
[_countLabel setFont:[UIFont systemFontOfSize:]];
[_panView addSubview:_countLabel]; [_countLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.right.top.mas_equalTo(_panView);
make.height.mas_equalTo();
}]; timer = [NSTimer scheduledTimerWithTimeInterval: target:self selector:@selector(countAdd) userInfo:nil repeats:YES];
[timer fire]; count = ;
} - (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();
make.height.mas_equalTo();
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