天天看点

iOS 拖动手势

iOS 手势中还包含了一种--拖动

上节我们简单介绍了旋转手势:http://blog.csdn.net/lwjok2007/article/details/50835884

这节 我们简单介绍一下拖动手势 

UIPanGestureRecognizer

同样 先建项目

iOS 拖动手势
iOS 拖动手势

起名 testPan

在默认生成的ViewController中写代码

iOS 拖动手势

创建一个ImageView 我们来拖动他

UIImageView *imageV;
           

创建 ImageView 和 手势 

UIPanGestureRecognizer

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    imageV = [[UIImageView alloc] initWithFrame:CGRectMake(10, 120, 120, 120)];
    imageV.image = [UIImage imageNamed:@"3.jpeg"];
    [self.view addSubview:imageV];
    
    UIPanGestureRecognizer *panGR = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGRAct:)];
    [imageV setUserInteractionEnabled:YES];
    [imageV addGestureRecognizer:panGR];
    
    
}
           

实现 手势对应的方法

- (void)panGRAct: (UIPanGestureRecognizer *)rec{
    
    
    CGPoint point = [rec translationInView:self.view];
    NSLog(@"%f,%f",point.x,point.y);
    rec.view.center = CGPointMake(rec.view.center.x + point.x, rec.view.center.y + point.y);
    [rec setTranslation:CGPointMake(0, 0) inView:self.view];
}
           

运行代码看看   拖动图片看看是不是在动

好了 代码上传到群空间 有兴趣的可以下载看看

代码:【60309拖动手势Pan.zip】

苹果开发群 :414319235  欢迎加入,共同学习