UIGestureRecognizer
2019-07-25 本文已影响0人
习惯了_就好
iOS 3.2之后,苹果推出了手势识别功能(Gesture Recognizer),
利用UIGestureRecognizer,能轻松识别用户在某个view上面做的一些常见手势
UIGestureRecognizer是一个抽象类,定义了所有手势的基本行为,使用它的子类才能处理具体的手势
UITapGestureRecognizer(敲击)
UIPinchGestureRecognizer(捏合,用于缩放)
UIPanGestureRecognizer(拖拽)
UISwipeGestureRecognizer(轻扫)
UIRotationGestureRecognizer(旋转)
UILongPressGestureRecognizer(长按)
#import "ViewController.h"
@interface ViewController ()<UIGestureRecognizerDelegate>
@property (weak, nonatomic) IBOutlet UIImageView *myImageView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// [self tapTest];
// [self tapTest2];
// [self longPressTest];
// [self swipeTest];
// [self panTest];
// [self pinchTest];
// [self rotateTest];
[self rotateAndPinchTest];
}
-(void)rotateAndPinchTest
{
// 旋转和缩放手势识别器同时使用,需要设置代理
UIRotationGestureRecognizer * rotationGestureRecognizer = [[UIRotationGestureRecognizer alloc] init];
[rotationGestureRecognizer addTarget:self action:@selector(onRotationListener:)];
rotationGestureRecognizer.delegate = self;
[self.myImageView addGestureRecognizer:rotationGestureRecognizer];
UIPinchGestureRecognizer * pinchGestureRecognizer = [[UIPinchGestureRecognizer alloc] init];
[pinchGestureRecognizer addTarget:self action:@selector(onPinchListener:)];
pinchGestureRecognizer.delegate = self;
[self.myImageView addGestureRecognizer:pinchGestureRecognizer];
}
/**
是否允许多个手势识别器同时使用
*/
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}
-(void)rotateTest
{
//旋转手势识别器
UIRotationGestureRecognizer * rotationGestureRecognizer = [[UIRotationGestureRecognizer alloc] init];
[rotationGestureRecognizer addTarget:self action:@selector(onRotationListener:)];
[self.myImageView addGestureRecognizer:rotationGestureRecognizer];
}
-(void)pinchTest
{
//缩放手势识别器
UIPinchGestureRecognizer * pinchGestureRecognizer = [[UIPinchGestureRecognizer alloc] init];
[pinchGestureRecognizer addTarget:self action:@selector(onPinchListener:)];
[self.myImageView addGestureRecognizer:pinchGestureRecognizer];
}
-(void)panTest
{
//初始化拖拽手势识别器
UIPanGestureRecognizer * panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(onPanListener:)];
[self.myImageView addGestureRecognizer:panGestureRecognizer];
}
-(void)swipeTest
{
//创建手势识别器
UISwipeGestureRecognizer * swipGestureRecognizer = [[UISwipeGestureRecognizer alloc] init];
//给手势识别器添加对应的回调
[swipGestureRecognizer addTarget:self action:@selector(onSwipeListener)];
//设置监听滑动反向
swipGestureRecognizer.direction = UISwipeGestureRecognizerDirectionDown;
//给view添加手势
[self.myImageView addGestureRecognizer:swipGestureRecognizer];
}
-(void)longPressTest
{
//创建长按手势识别器
UILongPressGestureRecognizer * longPressGestureRecognizer = [[UILongPressGestureRecognizer alloc] init];
//给手势识别器添加对应的回调
[longPressGestureRecognizer addTarget:self action:@selector(onLongClickListener)];
//给view添加手势识别器
[self.myImageView addGestureRecognizer:longPressGestureRecognizer];
}
-(void)tapTest2
{
//创建点击手势识别器
UITapGestureRecognizer * tapGestureRecognizer = [[UITapGestureRecognizer alloc] init];
//设置代理可以对点击事件进行特殊处理
tapGestureRecognizer.delegate = self;
//给手势识别器添加对应的回调
[tapGestureRecognizer addTarget:self action:@selector(onTapClickListener)];
//给view添加手势识别器
[self.myImageView addGestureRecognizer:tapGestureRecognizer];
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
CGPoint point = [touch locationInView:touch.view];
if (point.x < self.myImageView.frame.size.width * 0.5) {
return YES;
}
return NO;
}
-(void)tapTest
{
//创建点击手势识别器
UITapGestureRecognizer * tapGestureRecognizer = [[UITapGestureRecognizer alloc] init];
//给手势识别器添加对应的回调
[tapGestureRecognizer addTarget:self action:@selector(onTapClickListener)];
//给view添加手势识别器
[self.myImageView addGestureRecognizer:tapGestureRecognizer];
}
-(void)onTapClickListener
{
NSLog(@"点一下");
}
-(void)onLongClickListener
{
NSLog(@"长按了uyihui");
}
-(void)onSwipeListener
{
NSLog(@"滑动了");
}
-(void)onPanListener:(UIPanGestureRecognizer *)pan
{
CGPoint translation = [pan translationInView:pan.view];
CGPoint center = pan.view.center;
center.x += translation.x;
center.y += translation.y;
pan.view.center = center;
//每次相对于上一次
[pan setTranslation:CGPointZero inView:pan.view];
}
-(void)onPinchListener:(UIPinchGestureRecognizer *)pinch
{
pinch.view.transform = CGAffineTransformScale(pinch.view.transform, pinch.scale, pinch.scale);
//每次都相对于上一次缩放
pinch.scale = 1;
}
-(void)onRotationListener:(UIRotationGestureRecognizer *)rotation
{
rotation.view.transform = CGAffineTransformRotate(rotation.view.transform, rotation.rotation);
//每次相对于上一次旋转
rotation.rotation = 0;
}
@end