UIGestureRecognizer基類
是一個抽象類,我們主要是使用它的子類
列舉一下我常用的幾種手勢識別
UIPanGestureRecognizer拖拽手勢
UIPinchGestureRecognizer捏合縮放手勢
UIRotationGestureRecognizer旋轉手勢
UITapGestureRecognizer點擊手勢
UILongPressGestureRecognizer長按手勢
我用工程代碼簡單的說一下這幾種手勢識別器的用法在ViewDidLoad中寫一個ImageView,並給它加手勢識別
@interface ViewController ()<UIGestureRecognizerDelegate>
掛上代理
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 200, 200)];
imageView.center = self.view.center;
imageView.userInteractionEnabled = YES;
[imageView setImage:[UIImage imageNamed:@"1.png"]];
[self.view addSubview:imageView];
}
如果你想要多個手勢同時起作用,記得實現這個方法
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}
第一種手勢
1.Pan拖拽手勢
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panImageView:)];
[imageView addGestureRecognizer:panGesture];
實現拖拽的方法
#pragma mark--拖拽
- (void)panImageView:(UIPanGestureRecognizer *)pan
{
CGPoint point = [pan translationInView:self.view];
pan.view.center = CGPointMake(pan.view.center.x+point.x, pan.view.center.y+point.y);
[pan setTranslation:CGPointZero inView:self.view];
}
效果就是圖片可以隨意拖動,手勢停止圖片馬上停止在拖動最後停止的位置,如果覺得動作太生硬,可以添加一下代碼,使圖片在拖動停止後滑動一小段距離
/*
代碼實現解析:
計算速度向量的長度。
如果速度向量小於200,那就會得到一個小於的小數,那麼滑行會很短
基於速度和速度因素計算一個終點
確保終點不會跑出父View的邊界
使用UIView動畫使view滑動到終點
*/
#pragma mark--拖拽
- (void)panImageView:(UIPanGestureRecognizer *)pan
{
CGPoint point = [pan translationInView:self.view];
pan.view.center = CGPointMake(pan.view.center.x+point.x, pan.view.center.y+point.y);
[pan setTranslation:CGPointZero inView:self.view];
//想要拖動後有滑行的效果
if (pan.state == UIGestureRecognizerStateEnded) {
CGPoint velocity = [pan velocityInView:self.view];
CGFloat magnitude = sqrtf((velocity.x * velocity.x) + (velocity.y * velocity.y));
CGFloat slideMult = magnitude / 200;
float slideFactor = 0.1 * slideMult; // Increase for more of a slide
CGPoint finalPoint = CGPointMake(pan.view.center.x + (velocity.x * slideFactor),
pan.view.center.y + (velocity.y * slideFactor));
finalPoint.x = MIN(MAX(finalPoint.x, 0), self.view.bounds.size.width);
finalPoint.y = MIN(MAX(finalPoint.y, 0), self.view.bounds.size.height);
[UIView animateWithDuration:slideFactor*2 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
pan.view.center = finalPoint;
} completion:nil];
}
}
第二種手勢
Pinch捏合縮放手勢
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchView:)];
pinch.delegate = self;
[imageView addGestureRecognizer:pinch];
實現捏合手勢方法
#pragma mark--捏合
- (void)pinchView:(UIPinchGestureRecognizer *)pinch
{
pinch.view.transform = CGAffineTransformScale(pinch.view.transform, pinch.scale, pinch.scale);
pinch.scale = 1; // scale表示縮放比例
}
第三種手勢
Rotation旋轉手勢
UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotationImageView:)];
[imageView addGestureRecognizer:rotation];
旋轉手勢實現方法
#pragma mark--旋轉
- (void)rotationImageView:(UIRotationGestureRecognizer *)rotation
{
rotation.view.transform = CGAffineTransformRotate(rotation.view.transform, rotation.rotation);
rotation.rotation = 0;
}
第四種手勢
Tap單擊手勢
UITapGestureRecognizer *SingleTapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapImageView:)];
[imageView addGestureRecognizer:SingleTapGesture];
Tap雙擊手勢
UITapGestureRecognizer *DoubleTapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapImageView:)];
<span style="color:#cc33cc;">DoubleTapGesture.numberOfTapsRequired = 2;</span>
[imageView addGestureRecognizer:DoubleTapGesture];
點擊手勢實現方法,這裏爲了區別單擊和雙擊效果,我在點擊的時候有文本提示
#pragma mark--點擊
- (void)tapImageView:(UITapGestureRecognizer *)tap
{
UILabel *lable = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 50, 30)];
[lable setFont:[UIFont systemFontOfSize:14]];
if (tap.numberOfTapsRequired == 1) {
[lable setText:@"單擊"];
}else{
[lable setText:@"雙擊"];
}
CGPoint point = [tap locationInView:self.view];
lable.center = point;
[self.view addSubview:lable];
[self performSelector:@selector(hideLable) withObject:self afterDelay:1];
}
在此處順便提一下手勢之間的依賴關係,比如說我現在有了單擊手勢了,我又想要雙擊手機,當我雙擊的時候也會相應單擊手勢的方法,這樣就不好了,所有有了這個[A requireGestureRecognizerToFail:B]函數,它可以指定當A手勢發生時,即便A已經滿足條件了,也不會立刻觸發,會等到指定的手勢B確定失敗之後才觸發。
所以可以這樣寫
[SingleTapGesturerequireGestureRecognizerToFail:DoubleTapGesture];
第五種手勢
Long長按手勢
UILongPressGestureRecognizer *LongGesture = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(LongGesture:)];
[imageView addGestureRecognizer:LongGesture];
長按手勢的實現方法
#pragma mark--長按
- (void)LongGesture:(UILongPressGestureRecognizer *)longGesture
{
if (longGesture.state == UIGestureRecognizerStateBegan) {
UILabel *lable = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 70, 30)];
[lable setFont:[UIFont systemFontOfSize:14]];
[lable setText:@"長按手勢啦"];
CGPoint point = [longGesture locationInView:self.view];
lable.center = point;
[self.view addSubview:lable];
[self performSelector:@selector(hideLable) withObject:self afterDelay:1];
}
}
運行的效果: