IOS开发UIGestureRecognizer的基本使用方式和代理

1,iPhone上手势基本操作类型

 1.点击(Tap)

 点击作为最常用手势,用于按下或选择一个控件或条目(类似于普通的鼠标点击)、

 2.拖动(Drag)

 拖动用于实现一些页面的滚动,以及对控件的移动功能。

 3.滑动(Flick)

 滑动用于实现页面的快速滚动和翻页的功能。

 4.横扫(Swipe)

 横扫手势用于激活列表项的快捷操作菜单

 5.双击(Double Tap)

 双击放大并居中显示图片,或恢复原大小(如果当前已经放大)。同时,双击能够激活针对文字编辑菜单。

 6.放大(Pinch open)

 放大手势可以实现以下功能:打开订阅源,打开文章的详情。在照片查看的时候,放大手势也可实现放大图片的功能。

 7.缩小(Pinch close)

 缩小手势,可以实现与放大手势相反且对应的功能的功能:关闭订阅源退出到首页,关闭文章退出至索引页。在照片查看的时候,缩小手势也可实现缩小图片的功能。

 8.长按(Touch &Hold)

 在我的订阅页,长按订阅源将自动进入编辑模式,同时选中手指当前按下的订阅源。这时可直接拖动订阅源移动位置。

 针对文字长按,将出现放大镜辅助功能。松开后,则出现编辑菜单。

 针对图片长按,将出现编辑菜单。

 9.摇晃(Shake)

 摇晃手势,将出现撤销与重做菜单。主要是针对用户文本输入的。


2,几种手势基本使用方法

#import "Gueture.h"

@interface Gueture () <UIGestureRecognizerDelegate>

@end

@implementation Gueture


- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor lightGrayColor];
    /** 创建点击一次手势 ************************************************************/
    //1,新建一个手势 并设置手势的目标和触发的事件
    UITapGestureRecognizer *tapGusture = [[UITapGestureRecognizer alloc] initWithTarget:self
                                                                                 action:@selector(tapGusture:)];
    //2, 设置单击几次触发这个事件
    [tapGusture setNumberOfTapsRequired:1]; //默认是1
    //3,设置几个手指触发这个事件
    [tapGusture setNumberOfTouchesRequired:1]; //默认是1
    //4,将这个手势添加到主的view中
    [self.view addGestureRecognizer:tapGusture];
    
    /** 创建单击两次手势 ************************************************************/
    UITapGestureRecognizer *tapGestureTwo = [[UITapGestureRecognizer alloc] initWithTarget:self
                                                                                    action:@selector(tapGustureTwo:)];
    [tapGestureTwo setNumberOfTapsRequired:2];
    [tapGestureTwo setNumberOfTouchesRequired:1];
    [self.view addGestureRecognizer:tapGestureTwo];
    //设置这个之后,两个触摸只能响应一个
    [tapGusture requireGestureRecognizerToFail:tapGestureTwo];
    
    
    /** 创建捏合手势 ************************************************************/
    //实验发现,捏合手势只要两个手指在屏幕上滑动,不管是和还是分还是旋转都会触发这个事件,不停的触发
    UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchGesture:)];
    [self.view addGestureRecognizer:pinchGesture];
    /*scale属性: 可以理解为两手指之间的距离,其实是个比例,相对距离,不是绝对距离
     以刚开始的两个手指对应的两个point的之间的距离为标准,此时scale=1.
     若两手指之间距离减小,则scale不断变小,当两指重合,则变为0
     若两手指之间距离变大,则scale不断增大,没有上限,看屏幕多大
     
     velocity属性: 可以理解为两手指之间的移动速度,其实是个速度比例,相对速度,不是绝对速度
     以刚开始的两个手指对应的两个point的之间的距离为标准,此时velocity=0.
     若两手指之间距离减小,则velocity为负数,从-0开始,随着手指向里捏合的速度越快,负值越大,没有上限,我测试了下,可以到-20.009099,甚至更大 */
    
    
    /** 创建旋转手势 ************************************************************/
    UIRotationGestureRecognizer *rotationGesture = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationGesture:)];
    [self.view addGestureRecognizer:rotationGesture];
    [pinchGesture requireGestureRecognizerToFail:rotationGesture];//这句,当旋转失败时候开始执行捏合的手势
    
    
    /** 创建长按手势 ************************************************************/
    UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressGesture:)];
    longPressGesture.minimumPressDuration = 1;//至少按1S才能触发这个事件
    [self.view addGestureRecognizer:longPressGesture];
    [tapGusture requireGestureRecognizerToFail:longPressGesture];
    
    
    /** 创建快速滑动手势 ************************************************************/
    //为什么不太灵敏
    UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGesture:)];
    [swipeGesture setDirection:UISwipeGestureRecognizerDirectionDown];//设置滑动方向向下
    [self.view addGestureRecognizer:swipeGesture];
    
    
    /** 创建 拖动慢速移动 滑动手势 ************************************************************/
    UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGesture:)];
    [self.view addGestureRecognizer:panGesture];
    [panGesture requireGestureRecognizerToFail:swipeGesture];
    
    
    /*
     [imageview addGestureRecognizer:singleTwo];
     imageview.multipleTouchEnabled = YES;//设置属性使得uiimageview可以响应屏幕事件
     imageview.userInteractionEnabled = YES;
     [self.view addSubview:imageview];
     */
    
    
}
//触摸一次 手势处理
- (void)tapGusture:(UIGestureRecognizer *)gesture
{
    NSLog(@"tapGusture   numberOfTouches : %d", gesture.numberOfTouches);
}
//触摸两次 手势处理
- (void)tapGustureTwo:(UIGestureRecognizer *)gesture
{
    NSLog(@"tapGustureTwo   numberOfTouches : %d", gesture.numberOfTouches);
}
//捏合手势处理
- (void)pinchGesture:(UIPinchGestureRecognizer *)pinchGesture
{
    if (pinchGesture.state == UIGestureRecognizerStateBegan) {
        NSLog(@"pinchGesture start");
    }
    else if(pinchGesture.state == UIGestureRecognizerStateEnded)
    {
        NSLog(@"pinchGesture end");
    }
    else if(pinchGesture.state == UIGestureRecognizerStateChanged)
    {
        NSLog(@"pinchGesture changed");
    }
}
//旋转手势
- (void)rotationGesture:(UIRotationGestureRecognizer *)rotationGesture
{
    NSLog(@"rotationGesture");
}
//长按手势
- (void)longPressGesture:(UILongPressGestureRecognizer *)longPressGesture
{
    NSLog(@"UILongPressGesture");
}
- (void)swipeGesture:(UISwipeGestureRecognizer *)swipeGesture
{
    NSLog(@"UISwipeGesture");
}
- (void)panGesture:(UIPanGestureRecognizer *)panGesture
{
    NSLog(@"UIPanGesture");
}



/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end

3,UIGestureRecognizer 和 UIGestureRecognizerDelegate

//
//  UIGestureRecognizer.h
//  UIKit
//
//  Copyright (c) 2008-2013, Apple Inc. All rights reserved.
//

#import <Foundation/Foundation.h>
#import <CoreGraphics/CoreGraphics.h>
#import <UIKit/UIKitDefines.h>

@protocol UIGestureRecognizerDelegate;
@class UIView, UIEvent, UITouch;

typedef NS_ENUM(NSInteger, UIGestureRecognizerState) {
    UIGestureRecognizerStatePossible,   // the recognizer has not yet recognized its gesture, but may be evaluating touch events. this is the default state
    
    UIGestureRecognizerStateBegan,      // the recognizer has received touches recognized as the gesture. the action method will be called at the next turn of the run loop
    UIGestureRecognizerStateChanged,    // the recognizer has received touches recognized as a change to the gesture. the action method will be called at the next turn of the run loop
    UIGestureRecognizerStateEnded,      // the recognizer has received touches recognized as the end of the gesture. the action method will be called at the next turn of the run loop and the recognizer will be reset to UIGestureRecognizerStatePossible
    UIGestureRecognizerStateCancelled,  // the recognizer has received touches resulting in the cancellation of the gesture. the action method will be called at the next turn of the run loop. the recognizer will be reset to UIGestureRecognizerStatePossible
    
    UIGestureRecognizerStateFailed,     // the recognizer has received a touch sequence that can not be recognized as the gesture. the action method will not be called and the recognizer will be reset to UIGestureRecognizerStatePossible
    
    // Discrete Gestures – gesture recognizers that recognize a discrete event but do not report changes (for example, a tap) do not transition through the Began and Changed states and can not fail or be cancelled
    UIGestureRecognizerStateRecognized = UIGestureRecognizerStateEnded // the recognizer has received touches recognized as the gesture. the action method will be called at the next turn of the run loop and the recognizer will be reset to UIGestureRecognizerStatePossible
};

NS_CLASS_AVAILABLE_IOS(3_2) @interface UIGestureRecognizer : NSObject

// Valid action method signatures:
//     -(void)handleGesture;
//     -(void)handleGesture:(UIGestureRecognizer*)gestureRecognizer;
- (id)initWithTarget:(id)target action:(SEL)action; // default initializer

- (void)addTarget:(id)target action:(SEL)action;    // add a target/action pair. you can call this multiple times to specify multiple target/actions
- (void)removeTarget:(id)target action:(SEL)action; // remove the specified target/action pair. passing nil for target matches all targets, and the same for actions

@property(nonatomic,readonly) UIGestureRecognizerState state;  // the current state of the gesture recognizer

@property(nonatomic,assign) id <UIGestureRecognizerDelegate> delegate; // the gesture recognizer's delegate

@property(nonatomic, getter=isEnabled) BOOL enabled;  // default is YES. disabled gesture recognizers will not receive touches. when changed to NO the gesture recognizer will be cancelled if it's currently recognizing a gesture

// a UIGestureRecognizer receives touches hit-tested to its view and any of that view's subviews
@property(nonatomic,readonly) UIView *view;           // the view the gesture is attached to. set by adding the recognizer to a UIView using the addGestureRecognizer: method

@property(nonatomic) BOOL cancelsTouchesInView;       // default is YES. causes touchesCancelled:withEvent: to be sent to the view for all touches recognized as part of this gesture immediately before the action method is called
@property(nonatomic) BOOL delaysTouchesBegan;         // default is NO.  causes all touch events to be delivered to the target view only after this gesture has failed recognition. set to YES to prevent views from processing any touches that may be recognized as part of this gesture
@property(nonatomic) BOOL delaysTouchesEnded;         // default is YES. causes touchesEnded events to be delivered to the target view only after this gesture has failed recognition. this ensures that a touch that is part of the gesture can be cancelled if the gesture is recognized

// create a relationship with another gesture recognizer that will prevent this gesture's actions from being called until otherGestureRecognizer transitions to UIGestureRecognizerStateFailed
// if otherGestureRecognizer transitions to UIGestureRecognizerStateRecognized or UIGestureRecognizerStateBegan then this recognizer will instead transition to UIGestureRecognizerStateFailed
// example usage: a single tap may require a double tap to fail
- (void)requireGestureRecognizerToFail:(UIGestureRecognizer *)otherGestureRecognizer;

// individual UIGestureRecognizer subclasses may provide subclass-specific location information. see individual subclasses for details
- (CGPoint)locationInView:(UIView*)view;                                // a generic single-point location for the gesture. usually the centroid of the touches involved

- (NSUInteger)numberOfTouches;                                          // number of touches involved for which locations can be queried
- (CGPoint)locationOfTouch:(NSUInteger)touchIndex inView:(UIView*)view; // the location of a particular touch

@end


@protocol UIGestureRecognizerDelegate <NSObject>
@optional
// called when a gesture recognizer attempts to transition out of UIGestureRecognizerStatePossible. returning NO causes it to transition to UIGestureRecognizerStateFailed
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer;

// called when the recognition of one of gestureRecognizer or otherGestureRecognizer would be blocked by the other
// return YES to allow both to recognize simultaneously. the default implementation returns NO (by default no two gestures can be recognized simultaneously)
//
// note: returning YES is guaranteed to allow simultaneous recognition. returning NO is not guaranteed to prevent simultaneous recognition, as the other gesture's delegate may return YES
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer;

// called once per attempt to recognize, so failure requirements can be determined lazily and may be set up between recognizers across view hierarchies
// return YES to set up a dynamic failure requirement between gestureRecognizer and otherGestureRecognizer
//
// note: returning YES is guaranteed to set up the failure requirement. returning NO does not guarantee that there will not be a failure requirement as the other gesture's counterpart delegate or subclass methods may return YES
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRequireFailureOfGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer NS_AVAILABLE_IOS(7_0);
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldBeRequiredToFailByGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer NS_AVAILABLE_IOS(7_0);

// called before touchesBegan:withEvent: is called on the gesture recognizer for a new touch. return NO to prevent the gesture recognizer from seeing this touch
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch;

@end




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值