文章搬运来源:blog.csdn.net/Calvin_zhou…面试
做者:PGzxcmarkdown
对iOS开发感兴趣,能够看一下做者的iOS交流群:812157648,你们能够在里面吹水、交流相关方面的知识,群里还有我整理的有关于面试的一些资料,欢迎你们加群,你们一块儿开车oop
IOS3.2以后,苹果推出了手势识别功能(Guesture Recognizer),在触摸事件处理方面,大大简化了开发者的开发难度ui
UIGestureRecognizer是一个抽象类,定义了全部手势的基本行为,使用它的子类才能处理具体的手势spa
UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap:)];
//点按多少次才能触发
tap.numberOfTapsRequired=2;
//必须多少个手指触摸才能触发手势
//tap.numberOfTouchesRequired=2;
tap.delegate=self;
[_imageView addGestureRecognizer:tap];
复制代码
-(void)tap:(UITapGestureRecognizer *)tap
{
NSLog(@"tap");
}
复制代码
UILongPressGestureRecognizer *longPress=[[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPress:)];
[_imageView addGestureRecognizer:longPress];
复制代码
-(void)longPress:(UILongPressGestureRecognizer *)longPress
{
if (longPress.state==UIGestureRecognizerStateBegan) {
NSLog(@"longPress");
}
}
复制代码
//swip 一个手势只能识别一个方向
UISwipeGestureRecognizer *swipe=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swip:)];
swipe.direction=UISwipeGestureRecognizerDirectionRight;
[_imageView addGestureRecognizer:swipe];
复制代码
-(void)swip:(UISwipeGestureRecognizer *)swipe
{
NSLog(@"swipe");
}
复制代码
UIRotationGestureRecognizer *rotation=[[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotation:)];
[_imageView addGestureRecognizer:rotation];
复制代码
-(void)rotation:(UIRotationGestureRecognizer *)rotation
{
NSLog(@"%f",rotation.rotation);
//_imageView.transform=CGAffineTransformMakeRotation(rotation.rotation);
_imageView.transform=CGAffineTransformRotate(_imageView.transform, rotation.rotation);
rotation.delegate=self;
rotation.rotation=0;
}
复制代码
UIPinchGestureRecognizer *pinch=[[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinch:)];
[_imageView addGestureRecognizer:pinch];
pinch.delegate=self;
[self addRotation];
复制代码
-(void)pinch:(UIPinchGestureRecognizer *)pinch
{
_imageView.transform=CGAffineTransformScale(_imageView.transform, pinch.scale, pinch.scale);
//复位
pinch.scale=1;
}
复制代码
UIPanGestureRecognizer *pan=[[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(pan:)];
[_imageView addGestureRecognizer:pan];
复制代码
-(void)pan:(UIPanGestureRecognizer *)pan
{
CGPoint trans=[pan translationInView:_imageView];
_imageView.transform=CGAffineTransformTranslate(_imageView.transform, trans.x, trans.y);
//复位
[pan setTranslation:CGPointZero inView:_imageView];
NSLog(@"%@",NSStringFromCGPoint(trans));
}
复制代码