拖动实现图片移动效果

   拖动实现图片移动效果


先写一个手势,注意图片的 userInteractionEnabled设置为yes

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

    UIImage *image = [UIImage imageNamed:@"r.jpg"];

    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 40, 80, 80)];

    imageView.image = image;

    imageView.userInteractionEnabled = YES;

    [self addSubview:imageView];

    [imageView addGestureRecognizer:pan];

    

   

}

拖动的方法,最后一句是关键代码


- (void)pan:(UIPanGestureRecognizer *)gesture

{

    CGPoint point = [gesture translationInView:self];

    gesture.view.center = CGPointMake(gesture.view.center.x + point.x, gesture.view.center.y + point.y);

    [gesture setTranslation:CGPointMake(0, 0) inView:self];


}


posted @ 2017-05-19 12:19  clnchanpin  阅读(403)  评论(0编辑  收藏  举报