美文网首页
UIPanGestureRecognizer拖动手势

UIPanGestureRecognizer拖动手势

作者: 哈哈大p孩 | 来源:发表于2016-04-21 16:14 被阅读8112次

拖动手势在开发过程中用到频率较高,比如在app里弹出广告,我们可以将广告view向上或向下划出.话不多说,直接上代码

- (void)viewDidLoad {
    [super viewDidLoad];
    _imageView = [[UIImageView alloc] initWithFrame:CGRectMake(100, 200, 80, 80)];
    _imageView.backgroundColor = [UIColor redColor];
    [self.view addSubview:_imageView];
    _imageView.userInteractionEnabled = YES;
    
    UIPanGestureRecognizer *panGes = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(move:)];
    [_imageView addGestureRecognizer:panGes];
    panGes.delegate = self;
}
- (void)move:(UIPanGestureRecognizer *)sender {
    CGPoint pt = [sender translationInView:_imageView];
    sender.view.center = CGPointMake(sender.view.center.x +pt.x , sender.view.center.y);
    //每次移动完,将移动量置为0,否则下次移动会加上这次移动量
    [sender setTranslation:CGPointMake(0, 0) inView:self.view];
    if (sender.state == UIGestureRecognizerStateEnded) {
         NSLog(@"pan.view == %f", sender.view.center.x);
    }
   
}

- (BOOL)gestureRecognizerShouldBegin:(UIPanGestureRecognizer *)gestureRecognizer {
    CGPoint translation = [gestureRecognizer translationInView:gestureRecognizer.view];
    NSLog(@"translation == %f", translation.x);
//    if (translation.x >= 0) {
//        return NO;
//    }
    return YES;
}

具体操作还得具体分析,这里简单记录下做法。

相关文章

网友评论

      本文标题:UIPanGestureRecognizer拖动手势

      本文链接:https://www.haomeiwen.com/subject/jbmtrttx.html