- 手勢的創(chuàng)建及方法的實(shí)現(xiàn)
1缓待、點(diǎn)擊手勢
//創(chuàng)建點(diǎn)擊手勢
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
//添加代理
tap.delegate = self;
//添加手勢
[self.imageView addGestureRecognizer:tap];
#pragma mark -
#pragma mark - 點(diǎn)擊手勢實(shí)現(xiàn)的方法
-(void)tapAction:(UITapGestureRecognizer *)tap{
NSLog(@"點(diǎn)擊了");
}
2、輕掃手勢
UISwipeGestureRecognizer *swip = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipAction:)];
swip.direction = UISwipeGestureRecognizerDirectionUp;
[self.imageView addGestureRecognizer:swip];
#pragma mark -
#pragma mark - 輕掃手勢實(shí)現(xiàn)方法
-(void)swipAction:(UISwipeGestureRecognizer *)swip{
NSLog(@"清掃了");
}
3渠牲、長按手勢
UILongPressGestureRecognizer *longPres = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longAction:)];
[self.imageView addGestureRecognizer:longPres];
#pragma mark -
#pragma mark - 長按手勢實(shí)現(xiàn)方法
//默認(rèn)長按會有兩次觸發(fā)效果旋炒,即點(diǎn)擊時和取消點(diǎn)擊時都會調(diào)用實(shí)現(xiàn)的方法
-(void)longAction:(UILongPressGestureRecognizer *)longPres{
//設(shè)置點(diǎn)擊時處理
if (longPres.state == UIGestureRecognizerStateBegan) {
NSLog(@"長按了");
}
}
4、捏合手勢
UIPinchGestureRecognizer *pin = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinAction:)];
[self.imageView addGestureRecognizer:pin];
#pragma mark -
#pragma mark - 捏合手勢
- (void)pinAction:(UIPinchGestureRecognizer *)pin{
self.imageView.transform = CGAffineTransformScale(self.imageView.transform, pin.scale, pin.scale);
//復(fù)位
pin.scale = 1;
}
5签杈、旋轉(zhuǎn)手勢
UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotationAction:)];
rotation.delegate = self;
[self.imageView addGestureRecognizer:rotation];
#pragma mark -
#pragma mark - 旋轉(zhuǎn)手勢
-(void)rotationAction:(UIRotationGestureRecognizer *)rotation{
self.imageView.transform = CGAffineTransformRotate(self.imageView.transform, rotaion.rotation);
//復(fù)位
rotaion.rotation = 0;
}
6瘫镇、拖拽手勢
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panAction:)];
[self.imageView addGestureRecognizer:pan];
#pragma mark -
#pragma mark - 拖拽手勢
-(void)panAction:(UIPanGestureRecognizer *)pan{
//獲取手勢的移動,也是相對于最開始的位置
CGPoint transP = [pan translationInView:self.imageView];
self.imageView.transform = CGAffineTransformTranslate(self.imageView.transform, transP.x, transP.y);
//復(fù)位
[pan setTranslation:CGPointZero inView:self.imageView];
}
- 常用代理方法
//是否同時支持多種手勢
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
return YES;
}
//是否允許開始點(diǎn)擊
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{
return YES;
}
//設(shè)置點(diǎn)擊的范圍
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
//獲取當(dāng)前的觸摸點(diǎn)
CGPoint curp = [touch locationInView:self.imageView];
if (curp.x <= self.imageView.bounds.size.width*0.5) {
return NO;
}else{
return YES;
}
}
代理方法都是可選的,想通過代理方法實(shí)現(xiàn)手勢的某個效果 铣除,就把該手勢設(shè)置代理谚咬。