UI--手势方法介绍

iPhone中处理触摸屏的操作,之前是主要使用的是由UIResponder而来的如下4种方式:

 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
 - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
 - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
 - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event

但是这种方式甄别不同的手势操作实在是麻烦,需要你自己计算做不同的手势分辨。后来。。。
苹果就给出了一个比较简便的方式,就是使用UIGestureRecognizer。
二、UIGestureRecognizer
UIGestureRecognizer基类是一个抽象类,我们主要是使用它的子类:

     UITapGestureRecognizer            //Tap(点一下)
     UIPinchGestureRecognizer          //Pinch(二指往內或往外拨动)
     UIRotationGestureRecognizer       //Rotation(旋转)
     UISwipeGestureRecognizer          //Swipe(滑动,快速移动)
     UIPanGestureRecognizer            //Pan (拖移,慢速移动)
     UILongPressGestureRecognizer      //LongPress(长按)
     UIScreenEdgePanGestureRecognizer  //屏幕边缘识别
- (void)addGestureRecognizer:(UIGestureRecognizer *)gestureRecognizer//添加手势对象
-(instancetype)initWithTarget:(id)target action:(SEL)action//初始化方法
·······
其它方法看API文档
- 

这里我们以切换图片来进行验证
1)UITapGestureRecognizer 其有两个属性

@property(nonatomic) NSUInteger numberOfTapsRequired//点击的次数
@property(nonatomic) NSUInteger numberOfTouchesRequired//手指触摸的手指数目

    UIImage *imag =[UIImage imageNamed:@"2"];
    UIImageView *img = [[UIImageView alloc]initWithFrame:[UIScreen mainScreen].bounds];
    img.image = imag;
    img.tag = 100;
//    UIImage *img1 = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"2" ofType:nil]];
    
    [self.view addSubview:img];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap:)];
    img.userInteractionEnabled = YES;
    [img addGestureRecognizer:tap];
    
    
}

-(void)tap:(UITapGestureRecognizer *)tap{
    UIImageView *t = (UIImageView *)[self.view viewWithTag:100];
    t.image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"6.png" ofType:nil]];//利用沙盒来获取图片的路径,不会产生缓存,如果用imagenamed:来获取图片,只会加载一次,只会程序要用到加载的图片,都会通过缓存来获取,但程序结束的时,缓存中的东西还在,如果图片少量,可以用;如果要加载大量的图片,用NSbundle
    NSLog(@"%@",t.image);
}

效果
这里写图片描述这里写图片描述

(2) UIPinchGestureRecognizer(二指往內或往外拨动)
里面有两个属性1)scale(缩放倍数)
2)velocity(速度)

    UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinch:)];
    [img addGestureRecognizer:pinch];

    -(void)pinch:(UIPinchGestureRecognizer *)pin{
   UIImageView *t = (UIImageView *)[self.view viewWithTag:100];
    //CGAffineTransform a = t.transform;//获取当前的视图的矩阵
    //t.transform = CGAffineTransformMakeScale(0.5, 0.6);
    //t.transform = CGAffineTransformTranslate(t.transform, pin.scale, pin.scale);
    t.transform = CGAffineTransformScale(t.transform, pin.scale, pin.scale);//这个是在原有矩阵上进行一个缩放
    pin.scale = 1;
}

效果图
这里写图片描述这里写图片描述

(3)UIRotationGestureRecognizer //Rotation(旋转)

里面有两个属性1)rotation(旋转值)
2)velocity(速度)

    UIRotationGestureRecognizer *rote = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rote:)];
    [img addGestureRecognizer:rote];

-(void)rote:(UIRotationGestureRecognizer *)ro{
    UIImageView *t = (UIImageView *)[self.view viewWithTag:100];
    t.transform = CGAffineTransformRotate(t.transform, ro.rotation);
    //t.transform = CGAffineTransformScale(t.transform, pin.scale, pin.scale);
    
}

结果
这里写图片描述这里写图片描述

 UISwipeGestureRecognizer          //Swipe(滑动,快速移动)
 UIPanGestureRecognizer            //Pan (拖移,慢速移动)
 UILongPressGestureRecognizer      //LongPress(长按)
 UIScreenEdgePanGestureRecognizer  //屏幕边缘识别
 这三个方法的实现同第一个

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

图解AI

你的鼓励是我最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值