天天看点

UIKit中 轻拍、长按、清扫、旋转、捏合、拖拽 手势详解

UIKit中 轻拍、长按、清扫、旋转、捏合、拖拽 手势详解

在OC的UIKit 框架中有很多手势,经常用的也无非就 轻拍、长按、清扫、旋转、捏合、拖拽 这几种。接下来一一介绍一下这些手势的用法。

在OC中每一种手势都是一个都已经封转成一个相应的类,所以用起来很方便。

为了方便看出手势的效果,我对一个图片进行操作,定义了一个 UIImageView 属性。

一、轻拍

轻拍手势 就是点击的意思,你可以设置点击一次 或者多次!

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction)] ;
            tap.numberOfTapsRequired = 2 ; // 轻拍的次数 双击 !
            [imgV addGestureRecognizer:tap] ;// 把手势添加到 imageView 上
           

上面代码的意思是 实例化一个 UITapGestureRecognizer 对象,tap就可以实现 点击操作,参数 self 是 设置自己为代理,tapAction 是执行这个手势后要调用的的函数。可以设置点击的次数。然后把这个手势添加到 ImageView上。设置完后 别忘了在初始化的时候 桥上这句代码

imgV.userInteractionEnabled = YES ;// 设置ImageView支持用户交互 属性
           

因为默认的imageView是不支持用户手势的。

现在就可以编写 相应轻拍函数要调用的函数了

-(void) tapAction
{
    imgV.image = [UIImage imageNamed:@"test.jpg"] ;

}
           

点击图片就会切换到test.jpg图片。

二、长按

长按手势就是按着图片一定时间后执行的动作! 这是时间是可以设置的。

UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(topAction)] ;
            longPress.minimumPressDuration = 1 ;// 长按的时间
            [imgV addGestureRecognizer:longPress] ;
           

设置一下长按的时间为 1秒,把长按的手势添加到imageview上。长按imageview 就会调用更换头片的方法。

三、清扫

清扫手势就是 上下左右滑动的意思。

UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(doswipe:)];
            swipe.direction = UISwipeGestureRecognizerDirectionLeft | UISwipeGestureRecognizerDirectionRight;
            [imgV addGestureRecognizer:swipe] ;
           

swipe.direction 就是设置相应的手势,在这里设置的是 左划 或右划,就是左划和右划都会相应。就行手机上的相册一样,当我们左右滑动的适合,我们希望图片会左右切换。就可以在doswipe函数中判断,然后再执行相应的方法。

要调用的方法是有参数的,不要忘记写 doswipe后面的 冒号

四、旋转 旋转是多点触控,就是用两个或多个手指在屏幕画圆,旋转一定的角度。

UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(dorotat:)] ;             [imgV addGestureRecognizer:rotation] ;
-(void) rotat:(UIRotationGestureRecognizer *)sender {
imgV.transform = CGAffineTransformMakeRotation(sender.rotation) ; //参数是当前旋转的角度
}

           
CGAffineTransformMakeRotation(sender.rotation)
函数是用来旋转的旋转手势你计算了一个角度而已。
在imageView上执行这个手势后 图片就会旋转相应的角度。


五、捏合 捏合和旋转差不多都是 多点触控的,就是两个手往中间滑动是 缩小,往两边滑动是放大。然后根据放大或缩小的比例对图片进行缩放。

UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(dopinch:)] ;
[imgV addGestureRecognizer:pinch] ;
把捏合手势放到imageview上。
 -(void)  dopinch:(UIPinchGestureRecognizer *)sender {     
imgV.transform = CGAffineTransformMakeScale(sender.scale, sender.scale) ;
 } 
CGAffineTransformMakeScale(sender.scale, sender.scale) ;函数是 缩放函数, 两个参数分别是 x轴方向 和 Y轴方向的缩放比例

六、拖拽手势 按住图片 移动到指定位置
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(dopan:) ];
            [imgV addGestureRecognizer:pan] ;

-(void) dopan:(UIPanGestureRecognizer *)sender
{
    if(sender.state == UIGestureRecognizerStateBegan)
    {
        oldPoint = [sender locationInView:self.view] ;
    }
    CGPoint newPoint = [sender locationInView:self.view] ;
    CGFloat dx = newPoint.x -oldPoint.x ;
    CGFloat dy = newPoint.y - oldPoint.y ;
    
    imgV.center = CGPointMake(imgV.center.x+dx, imgV.center.y + dy) ;//重新设置 图片中心点坐标
    
    oldPoint = newPoint ;// 记录 第二个点
}
           
UIKit中 轻拍、长按、清扫、旋转、捏合、拖拽 手势详解

2013-7-20 与 北京

版权所有 ,转载请注明出去。