UIGestureRecognizer
为了完成手势识别,必须借助于手势识别器—-UIGestureRecognizer
利用UIGestureRecognizer,能轻松识别用户在某个view上面做的一些常见手势
UIGestureRecognizer是一个抽象类,定义了所有手势的基本行为,使用它的子类才能处理具体的手势
UITapGestureRecognizer //(敲击) UIPinchGestureRecognizer //(捏合,用于缩放) UIPanGestureRecognizer //(拖拽) UISwipeGestureRecognizer //(轻扫) UIRotationGestureRecognizer //(旋转) UILongPressGestureRecognizer //(长按)
敲击 UITapGestureRecognizer
每一个手势识别器的用法都差不多,比如UITapGestureRecognizer的使用步骤如下
创建手势识别器对象 UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] init]; 设置手势识别器对象的具体属性 // 连续敲击2次 tap.numberOfTapsRequired = 2; // 需要2根手指一起敲击 tap.numberOfTouchesRequired = 2; 添加手势识别器到对应的view上 [self.iconView addGestureRecognizer:tap]; 监听手势的触发 [tap addTarget:self action:@selector(tapIconView:)];
敲击实例
@interface GRViewController () <UIGestureRecognizerDelegate> @property (weak, nonatomic) IBOutlet UIImageView *iconView; @end @implementation GRViewController - (void)viewDidLoad { [super viewDidLoad]; [self testTap2]; } - (void)testTap2 { UIGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapView)]; tap.delegate = self; [self.iconView addGestureRecognizer:tap]; } #pragma mark - 代理方法 /** * 当点击view的时候,会先调用这个方法 */ - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch { CGPoint pos = [touch locationInView:touch.view]; if (pos.x <= self.iconView.frame.size.width * 0.5) { return YES; } return NO; } - (void)testTap { // 1.创建手势识别器对象 UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] init]; // 连续敲击2次,手势才能识别成功 tap.numberOfTapsRequired = 2; tap.numberOfTouchesRequired = 2; // 2.添加手势识别器对象到对应的view [self.iconView addGestureRecognizer:tap]; // 3.添加监听方法(识别到了对应的手势,就会调用监听方法) [tap addTarget:self action:@selector(tapView)]; } - (void)tapView { NSLog(@"-----我敲击了屏幕!!"); } @end
长按和轻扫实例
@interface GRViewController () @property (weak, nonatomic) IBOutlet UIView *redView; @end @implementation GRViewController - (void)viewDidLoad { [super viewDidLoad]; UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeView)]; swipe.direction = UISwipeGestureRecognizerDirectionUp; [self.redView addGestureRecognizer:swipe]; } - (void)swipeView { NSLog(@"swipeView"); } - (void)testLongPress { UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] init]; [longPress addTarget:self action:@selector(longPressView)]; // 至少长按2秒 longPress.minimumPressDuration = 2; // 在触发手势之前,50px范围内长按有效 longPress.allowableMovement = 50; [self.redView addGestureRecognizer:longPress]; } - (void)longPressView { NSLog(@"长按了红色的view"); } @end
缩放和旋转实例
@interface GRViewController () <UIGestureRecognizerDelegate> @property (weak, nonatomic) IBOutlet UIImageView *iconView; @end @implementation GRViewController - (void)viewDidLoad { [super viewDidLoad]; [self testPinchAndRotate]; } #pragma mark - 手势识别器的代理方法 /** * 是否允许多个手势识别器同时有效 * Simultaneously : 同时地 */ - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer { return YES; } #pragma mark - 缩放 + 旋转 - (void)testPinchAndRotate { [self testPinch]; [self testRotate]; } #pragma mark - 缩放手势(捏合手势) - (void)testPinch { UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchView:)]; pinch.delegate = self; [self.iconView addGestureRecognizer:pinch]; } - (void)pinchView:(UIPinchGestureRecognizer *)pinch { pinch.view.transform = CGAffineTransformScale(pinch.view.transform, pinch.scale, pinch.scale); pinch.scale = 1; // 这个真的很重要!!!!! } #pragma mark - 旋转手势 - (void)testRotate { UIRotationGestureRecognizer *recognizer = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotateView:)]; recognizer.delegate = self; [self.iconView addGestureRecognizer:recognizer]; } - (void)rotateView:(UIRotationGestureRecognizer *)recognizer { recognizer.view.transform = CGAffineTransformRotate(recognizer.view.transform, recognizer.rotation); recognizer.rotation = 0; // 这个很重要!!!!! } @end
拖拽实例
@interface GRViewController () @property (weak, nonatomic) IBOutlet UIView *purpleView; @end @implementation GRViewController - (void)viewDidLoad { [super viewDidLoad]; UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panView:)]; [self.purpleView addGestureRecognizer:pan]; } - (void)panView:(UIPanGestureRecognizer *)pan { switch (pan.state) { case UIGestureRecognizerStateBegan: // 开始触发手势 break; case UIGestureRecognizerStateEnded: // 手势结束 break; default: break; } // 1.在view上面挪动的距离 CGPoint translation = [pan translationInView:pan.view]; CGPoint center = pan.view.center; center.x += translation.x; center.y += translation.y; pan.view.center = center; // 2.清空移动的距离 [pan setTranslation:CGPointZero inView:pan.view]; } @end
手势识别的状态
typedef NS_ENUM(NSInteger, UIGestureRecognizerState) { // 没有触摸事件发生,所有手势识别的默认状态 UIGestureRecognizerStatePossible, // 一个手势已经开始但尚未改变或者完成时 UIGestureRecognizerStateBegan, // 手势状态改变 UIGestureRecognizerStateChanged, // 手势完成 UIGestureRecognizerStateEnded, // 手势取消,恢复至Possible状态 UIGestureRecognizerStateCancelled, // 手势失败,恢复至Possible状态 UIGestureRecognizerStateFailed, // 识别到手势识别 UIGestureRecognizerStateRecognized = UIGestureRecognizerStateEnded };