文章搬運來源: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));
}
複製代碼