手勢--UIGestureRecognizer

監(jiān)聽觸摸事件的做法:

1.如果想監(jiān)聽一個view上面的觸摸事件,之前的做法是:
1>自定義一個view
2>實現(xiàn)view的touches的方法,在方法內(nèi)部實現(xiàn)具體處理代碼

2.通過touches方法監(jiān)聽view觸摸事件,有很明顯的幾個缺點
1>必須得自定義view
2>由于是在view內(nèi)部的touches方法中監(jiān)聽觸摸事件,默認(rèn)情況下,是無法讓其他外界對象監(jiān)聽view的觸摸事件
3>不容易區(qū)分用戶的具體手勢行為

iOS 3.2之后,蘋果退出了手勢識別功能(Gesture Recognizer),在觸摸手勢處理方面,大大簡化了開發(fā)者的開發(fā)難度

UIGestureRecognizer---手勢識別器

UIGestureRecognizer是一個抽象類,定義了所有手勢的基本行為,使用它的子類才能處理具體手勢行為
1.UITapGestureRecognizer 點擊行為
2.UIPinchGestureRecognizer 捏合行為,用于縮放
3.UIPanGestureRecognizer 拖拽行為
4.UISwipeGestureRecognizer 輕掃行為
5.UIRotationGestureRecognizer 旋轉(zhuǎn)行為
6.UILongPressGestureRecognizer 長按行為

手勢識別器的用法:以UITapGestureRecognizer為例

1.創(chuàng)建手勢識別器對象 (可以直接添加代理,和執(zhí)行的方法)
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]init] [tap addTarget:self action:]
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector()]

2.設(shè)置手勢識別器對象的具體屬性(一般不設(shè)置)

//連續(xù)敲擊2次
tap.numberOfTapsRequired = 2;
//需要2根手指一起敲擊
tap.numberOfTouchesRequired = 2;

3.添加手勢識別器到對應(yīng)的view上
[self.view addGestureRecognizer: tap]

手勢識別的狀態(tài)
typedef NS_ENUM(NSInteger,UIGestureRecognizerState)
{
    //沒有觸摸事件發(fā)生,所有手勢識別的默認(rèn)狀態(tài)
    UIGestureRecognizerStatePossible,
    //一個手勢已經(jīng)開始但尚未改變或者完成時
    UIGestureRecognizerStateBegan,
    //手勢狀態(tài)改變
    UIGestureRecognizerStateChanged,
    //手勢完成
    UIGestureRecognizerStateEnded,
    //手勢取消,恢復(fù)至Possible狀態(tài)
    UIGestureRecognizerStateCancelled,
    //手勢失敗,恢復(fù)至Possible狀態(tài)
    UIGestureRecognizerStateFailed,
}

Snip20151007_8.png

手勢的應(yīng)用(點擊手勢)

- (void)viewDidLoad {
    [super viewDidLoad];
  
    // 建立點擊手勢,并制定target ,并要求實現(xiàn)tap方法
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap)];
    // 將點擊手勢,加入_imageView的處理中, 記得一定要選擇交互  ,  因為默認(rèn)情況下 imageView的交互為no
    [_imageView addGestureRecognizer:tap];
    // 設(shè)置點擊手勢的代理,可以做一些特定的事情
    tap.delegate = self;
}

// 點擊手勢的范圍縮減, 只接受圖片一半的尺寸。
- (BOOL)gestureRecognizer:(nonnull UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(nonnull UITouch *)touch
{
     CGPoint tapP = [touch locationInView:_imageView];
    if (tapP.x < _imageView.frame.size.width * 0.5) {
        return YES;
    }
    return NO;

}

// 實現(xiàn)點擊手勢,方法
- (void)tap{

    NSLog(@"%s",__func__);

}

- (BOOL)gestureRecognizer:(nonnull UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(nonnull UITouch *)touch此方法可以設(shè)置允許手勢的區(qū)域 ,默認(rèn)為YES.

長按和輕掃手勢.

#pragma mark - <輕掃手勢>
- (void)setupSwipe
{

    // 多加幾個輕掃手勢,便能夠?qū)崿F(xiàn)多個方向的輕掃。
    UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipe)];
    
    [_imageView addGestureRecognizer:swipe];
    // 修改輕掃的的方向
    swipe.direction = UISwipeGestureRecognizerDirectionLeft;
    
    UISwipeGestureRecognizer *swipe1 = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipe)];
    
    [_imageView addGestureRecognizer:swipe1];
    // 修改輕掃的的方向
    swipe1.direction = UISwipeGestureRecognizerDirectionRight;
    

}
- (void)swipe
{

    NSLog(@"%s",__func__);
}


#pragma mark - <長按點擊>
- (void)setupLongPress
{
    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPress:)];
    
    [_imageView addGestureRecognizer:longPress];
    longPress.delegate = self;
}
- (void)longPress:(UILongPressGestureRecognizer *)longPress
{
    
    // 傳入?yún)?shù), 當(dāng)前的手勢,  手勢狀態(tài),決定事件
    if (longPress.state == UIGestureRecognizerStateBegan) {
     NSLog(@"%s",__func__);
    }
}

捏合,拖拽手勢
- (BOOL)gestureRecognizer:(nonnull UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(nonnull UIGestureRecognizer *)otherGestureRecognizer
默認(rèn)為NO, 返回YES是允許多重手勢.

// 允許組合手勢
- (BOOL)gestureRecognizer:(nonnull UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(nonnull UIGestureRecognizer *)otherGestureRecognizer
{
    
    return YES;


}

#pragma mark - <旋轉(zhuǎn)>
- (void)setupRotation
{

    UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotation:)];
    
    [_imageView addGestureRecognizer:rotation];
    
    rotation.delegate = self;

}

- (void)rotation:(UIRotationGestureRecognizer*)rotation
{
    CGFloat rotationF = rotation.rotation;
    
    _imageView.transform = CGAffineTransformRotate(_imageView.transform, rotationF);
    
    
    rotation.rotation = 0;


}


#pragma mark - <捏合>
- (void)setupPinch
{
    UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinch:)];
    
    [_imageView addGestureRecognizer:pinch];
    
    pinch.delegate = self;

}

- (void)pinch:(UIPinchGestureRecognizer *)pinch
{
    // 獲取捏合比例
    CGFloat scale = pinch.scale;
    
    _imageView.transform = CGAffineTransformScale(_imageView.transform, scale, scale);
    // 捏合后保持原比例,負(fù)責(zé)會變得不見得
    pinch.scale = 1;
    
}


#pragma mark - <拖拽>
- (void)setupPan
{

    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(pan:)];
    
    
    [_imageView addGestureRecognizer:pan];
    

}
- (void)pan:(UIPanGestureRecognizer *)pan
{
    CGPoint panP = [pan translationInView:_imageView];
    
    NSLog(@"%@",NSStringFromCGPoint(panP));
    
    // 如果不去讓pan每次的tranlation為0 的話, 則再下次拖拽時 ,圖片會直接回到初始位置,因為上一次的pan的translation沒有清掉。
//    _imageView.transform = CGAffineTransformMakeTranslation(panP.x, panP.y);
    
    _imageView.transform = CGAffineTransformTranslate(_imageView.transform, panP.x, panP.y);

    // 每次拖拽,translation 設(shè)定為0  ,可以每次根據(jù)上一次的情況定位,去實現(xiàn)拖拽。
    [pan setTranslation:CGPointZero inView:_imageView];

}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容