如题,捏合手势并没有提供判断横向还是竖向的判定方式。
但是手势中有提供这个方法:
- (CGPoint)locationOfTouch:(NSUInteger)touchIndex inView:(nullable UIView*)view;
「返回一个给定视图发生手势的点(为了在多个手势中捕获位置)」
可以根据捏合手势手指起止点做差比较判定捏合手势方向:
方式一:
如果两个捏合点X的差值大于Y的差值变化,那么就是横向捏合;反之,为竖向捏合。搭配手势提供的scale,可判断为捏合放大还是捏合缩小。
方式二:
只根据两个捏合点之间的角度,估算捏合方向;
简单实现代码如下:
#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
网友评论