拖动实现图片移动效果

时间:2015-07-01 18:22:32   收藏:0   阅读:217

   拖动实现图片移动效果


先写一个手势,注意图片的 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];


}


版权声明:本文为博主原创文章,未经博主允许不得转载。

原文:http://blog.csdn.net/lu_ca/article/details/46711681

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!