如題哥艇,捏合手勢并沒有提供判斷橫向還是豎向的判定方式。
但是手勢中有提供這個方法:
- (CGPoint)locationOfTouch:(NSUInteger)touchIndex inView:(nullable UIView*)view;
「返回一個給定視圖發(fā)生手勢的點(為了在多個手勢中捕獲位置)」
可以根據(jù)捏合手勢手指起止點做差比較判定捏合手勢方向:
方式一:
如果兩個捏合點X的差值大于Y的差值變化诡宗,那么就是橫向捏合玄糟;反之磷籍,為豎向捏合。搭配手勢提供的scale挤渐,可判斷為捏合放大還是捏合縮小苹享。
方式二:
只根據(jù)兩個捏合點之間的角度,估算捏合方向浴麻;
簡單實現(xiàn)代碼如下:
#import "ViewController.h"
#define UI_SCREEN_HEIGHT ([[UIScreen mainScreen] bounds].size.height)
#define UI_SCREEN_WIDTH [[UIScreen mainScreen] bounds].size.width
@interface ViewController ()
@property (nonatomic, assign) CGPoint pointOne;
@property (nonatomic, assign) CGPoint pointTwo;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
UIView *panView = [[UIView alloc]initWithFrame:CGRectMake((UI_SCREEN_WIDTH - 300) / 2.0, (UI_SCREEN_HEIGHT - 300) / 2.0, 300, 300)];
panView.backgroundColor = [UIColor redColor];
[self.view addSubview:panView];
panView.userInteractionEnabled = YES;
UIPinchGestureRecognizer *pin = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinAction:)];
[panView addGestureRecognizer:pin];
}
- (void)pinAction:(UIPinchGestureRecognizer *)recognizer {
if (recognizer.numberOfTouches == 2) {
CGPoint locationOne = [recognizer locationOfTouch:0 inView: recognizer.view];
CGPoint locationTwo = [recognizer locationOfTouch:1 inView: recognizer.view];
//方式一:比較X-Y差值變化
CGFloat difference_X = fabs((locationOne.x - locationTwo.x) - (self.pointOne.x - self.pointTwo.x));
CGFloat difference_Y = fabs((locationOne.y - locationTwo.y) - (self.pointOne.y - self.pointTwo.y));
CGFloat scale = recognizer.scale;
if (difference_X > difference_Y){
NSLog(@"橫向捏合");
//簡單邊界處理
if (recognizer.view.frame.size.width * scale > UI_SCREEN_WIDTH - 40 || recognizer.view.frame.size.width * scale < 100) {
return;
}
recognizer.view.transform = CGAffineTransformScale(recognizer.view.transform, scale, 1);
} else {
NSLog(@"豎向捏合");
//簡單邊界處理
if (recognizer.view.frame.size.height * scale > UI_SCREEN_HEIGHT - 40 || recognizer.view.frame.size.height * scale < 100) {
return;
}
recognizer.view.transform = CGAffineTransformScale(recognizer.view.transform, 1, scale);
}
recognizer.scale = 1.0;
self.pointOne = [recognizer locationOfTouch:0 inView:recognizer.view];
self.pointTwo = [recognizer locationOfTouch:1 inView:recognizer.view];
//方式二:計算角度得问,估算捏合方向
// CGFloat abSlope = ABS((locationTwo.y - locationOne.y) / (locationTwo.x - locationOne.x));
// CGFloat scale = recognizer.scale;
// if (abSlope < 1.0) {
// //簡單邊界處理
// if (recognizer.view.frame.size.width * scale > UI_SCREEN_WIDTH - 40 || recognizer.view.frame.size.width * scale < 100) {
// return;
// }
// recognizer.view.transform = CGAffineTransformScale(recognizer.view.transform, scale, 1);
// }else {
// //簡單邊界處理
// if (recognizer.view.frame.size.height * scale > UI_SCREEN_HEIGHT - 40 || recognizer.view.frame.size.height * scale < 100) {
// return;
// }
// recognizer.view.transform = CGAffineTransformScale(recognizer.view.transform, 1, scale);
// }
// recognizer.scale = 1.0;
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end