UIGestureRecognizer手勢識(shí)別器
手勢識(shí)別器是特殊的觸摸事件
UIGestureRecognizer是一個(gè)抽象的父類,我們一般不使用這個(gè)類,而是使用它的子類
tap(輕拍),rotation(旋轉(zhuǎn)),swipe(輕掃),pinch(捏合),pan(平移),longPress(長按),screenEdgePan(屏幕邊界平移)
--- tap手勢 ---
// 1.創(chuàng)建手勢并且初始化,使用initWithTarget:action: 方法創(chuàng)建
UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapView:)];
// 2.設(shè)置屬性
// 1) 輕拍次數(shù) (默認(rèn)是1)
tap.numberOfTapsRequired = 2;
// 2) 手指個(gè)數(shù)
tap.numberOfTouchesRequired = 2;
// 3.添加到視圖上 addGestureRecognizer: 方法
[_redView addGestureRecognizer:tap];
// 調(diào)用的方法
- (void)tapView:(UITapGestureRecognizer *)sender{
_redView.backgroundColor = [UIColor randomColor];
}
--- swipe 清掃手勢 ---
UISwipeGestureRecognizer * swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeView:)];
// 設(shè)置輕掃方向 (枚舉)
swipe.direction = UISwipeGestureRecognizerDirectionDown;
[_redView addGestureRecognizer:swipe];
// 調(diào)用的方法
- (void)swipeView:(UISwipeGestureRecognizer *)sender{
_redView.backgroundColor = [UIColor randomColor];
}
--- longPress 長按 ---
UILongPressGestureRecognizer * longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressView:)];
// 最小長按時(shí)間
longPress.minimumPressDuration = 2;
[_redView addGestureRecognizer:longPress];
// 調(diào)用的方法
- (void)longPressView:(UILongPressGestureRecognizer *)sender{
// 判斷手勢狀態(tài) (如果不判斷會(huì)彈出2次窗)
if (sender.state == UIGestureRecognizerStateBegan) {
NSLog(@"長按狀態(tài)");
}
}
--- pan 平移手勢 ---
UIPanGestureRecognizer * pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panView:)];
[_redView addGestureRecognizer:pan];
// 調(diào)用的方法
- (void)panView:(UIPanGestureRecognizer *)sender{
// 方法1
// 計(jì)算偏移量
CGPoint point = [sender translationInView:_redView];
// 平移 x1 = ax + cy + tx; y1 = bx + dy + ty;
// sender.view.transform = CGAffineTransformMake(1, 0, 0, 1, point.x, point.y);
// 方法2
// 每次移動(dòng)都是從原來的位置移動(dòng) 一般都有make
// sender.view.transform = CGAffineTransformMakeTranslation(point.x, point.y);
// 方法3
// 以上次的位置為標(biāo)準(zhǔn) 因?yàn)榘焉洗蔚膖ransform傳進(jìn)來了 所以要重置增量
sender.view.transform = CGAffineTransformTranslate(sender.view.transform, point.x, point.y);
// 增量置為0
[sender setTranslation:CGPointZero inView:sender.view];
}
--- pinch 捏合 ---
UIPinchGestureRecognizer * pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchView:)];
// 添加手勢
[_redView addGestureRecognizer:pinch];
// 調(diào)用的方法
- (void)pinchView:(UIPinchGestureRecognizer *)sender{
// 方法1
// x1 = ax + cy + tx; y1 = bx + dy + ty; 放大只和ax dy有關(guān)系
// scale 是縮放比例
// sender.view.transform = CGAffineTransformMake(sender.scale, 0, 0, sender.scale, 0, 0);
// 方法2
// 每次縮放以原來為標(biāo)準(zhǔn)
// sender.view.transform = CGAffineTransformMakeScale(sender.scale, sender.scale);
// 方法3
// 以上次的為標(biāo)準(zhǔn)
sender.view.transform = CGAffineTransformScale(sender.view.transform, sender.scale, sender.scale);
// 重新設(shè)置縮放比例 (1是正常縮放, <1是縮小, >1是放大)
sender.scale = 1;
}
---rotation 旋轉(zhuǎn)手勢 ---
UIRotationGestureRecognizer * rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationView:)];
[_redView addGestureRecognizer:rotation];
// 調(diào)用的方法
- (void)rotationView:(UIRotationGestureRecognizer *)sender{
// 方法1
// sender.view.transform = CGAffineTransformMake(cos(M_PI_4), sin(M_PI_4), -sin(M_PI_4), cos(M_PI_4), 0, 0);
// 方法2
// rotation 是旋轉(zhuǎn)角度
// 一個(gè)參數(shù),以原來的位置為標(biāo)準(zhǔn)
//sender.view.transform = CGAffineTransformMakeRotation(sender.rotation);
// 方法3
// 兩個(gè)參數(shù) 以上次位置為標(biāo)準(zhǔn)
sender.view.transform = CGAffineTransformRotate(sender.view.transform, sender.rotation); // 清除增量
sender.rotation = 0.0;
}
--- screenEdgePan 屏幕邊界平移 ---
UIScreenEdgePanGestureRecognizer *edgePan = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(edgePanView:)];
// 屬性設(shè)置
// 注意:使用屏幕邊緣平移需要注意兩點(diǎn)
// 1. 視圖的位置(屏幕邊緣)
// 2. 設(shè)置edges屬性 (枚舉)
edgePan.edges = UIRectEdgeLeft;
// 添加手勢
[_redView addGestureRecognizer:edgePan];
// 調(diào)用的方法
- (void)edgePanView:(UIScreenEdgePanGestureRecognizer *)sender{
// 計(jì)算偏移量
CGPoint point = [sender translationInView:_redView];
// 進(jìn)行平移
sender.view.transform = CGAffineTransformMakeTranslation(point.x, point.y);
}