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 //屏幕边缘识别
这三个方法的实现同第一个