(iOS)手勢(shì)識(shí)別的簡(jiǎn)單使用

  • (ios)手勢(shì)包括以下6種
UITapGestureRecognizer //點(diǎn)擊手勢(shì)
UILongPressGestureRecognizer // 長(zhǎng)按手勢(shì)
UISwipeGestureRecognizer    // 輕掃手勢(shì)
UIRotationGestureRecognizer // 旋轉(zhuǎn)手勢(shì)
UIPinchGestureRecognizer    //捏合手勢(shì)
UIPanGestureRecognizer      //拖拽手勢(shì)
  • UIGestureRecognizerDelegate
// 是否允許開始觸發(fā)手勢(shì)
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
    return NO;
}

// 是否允許同時(shí)支持多個(gè)手勢(shì),默認(rèn)是不支持多個(gè)手勢(shì)
// 返回yes表示支持多個(gè)手勢(shì)
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    return YES;
}

// 是否允許接收手指的觸摸點(diǎn)
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
    // 獲取當(dāng)前的觸摸點(diǎn)
    CGPoint curP = [touch locationInView:self.imageView];
    //下面表示當(dāng)觸摸點(diǎn)在self.imageView 在中心點(diǎn)左面點(diǎn)擊不觸發(fā)事件,在右面則觸發(fā)點(diǎn)擊事件
    if (curP.x < self.imageView.bounds.size.width * 0.5) {
        return NO;
    }else{
        return YES;
    }
}
  • 手勢(shì)觸發(fā)事件需要注意的地方

// 創(chuàng)建點(diǎn)按手勢(shì)
- (void)setUpTap
{
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
    
    tap.delegate = self;
  //連續(xù)點(diǎn)擊兩次
    tap.numberOfTapsRequired = 2;
    //需要兩跟手指一起敲擊
    tap.numberOfTouchesRequired =2;
    
    [_imageView addGestureRecognizer:tap];
}

- (void)tap:(UITapGestureRecognizer *)tap
{
    NSLog(@"%s",__func__);
}
//創(chuàng)建長(zhǎng)按手勢(shì)
- (void)setUpLongPress
{
    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
    
    [self.imageView addGestureRecognizer:longPress];
}


- (void)longPress:(UILongPressGestureRecognizer *)longPress
{
    // 默認(rèn)會(huì)觸發(fā)兩次,所以需要加上狀態(tài)去限制
    if (longPress.state == UIGestureRecognizerStateBegan) {
        
        NSLog(@"%s",__func__);
    }
}
//創(chuàng)建輕掃手勢(shì)
- (void)setUpSwipe
{
    // 默認(rèn)輕掃的方向是往右
    UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe)];
    //設(shè)置輕掃手勢(shì)的方向
    swipe.direction = UISwipeGestureRecognizerDirectionUp;
    
    [self.imageView addGestureRecognizer:swipe];
    
    // 如果以后想要一個(gè)控件支持多個(gè)方向的輕掃,必須創(chuàng)建多個(gè)輕掃手勢(shì),一個(gè)輕掃手勢(shì)只支持一個(gè)方向
    // 默認(rèn)輕掃的方向是往右
    UISwipeGestureRecognizer *swipeDown = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe)];
    
    swipeDown.direction = UISwipeGestureRecognizerDirectionDown;
    
    [self.imageView addGestureRecognizer:swipeDown];

    
}

- (void)swipe
{
    NSLog(@"%s",__func__);
}
//創(chuàng)建旋轉(zhuǎn)手勢(shì)
- (void)setUpRotation
{
    UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotation:)];
    rotation.delegate = self;
    [self.imageView addGestureRecognizer:rotation];
}

// 默認(rèn)傳遞的旋轉(zhuǎn)的角度都是相對(duì)于最開始的位置
- (void)rotation:(UIRotationGestureRecognizer *)rotation
{
    
    self.imageView.transform = CGAffineTransformRotate(self.imageView.transform, rotation.rotation);
    // 可以不加下面這句復(fù)位的代碼試一下效果
    // 復(fù)位
    rotation.rotation = 0;
    
    // 獲取手勢(shì)旋轉(zhuǎn)的角度
    NSLog(@"%f",rotation.rotation);
}
//創(chuàng)建捏合手勢(shì)
- (void)setUpPinch
{
    UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinch:)];
    pinch.delegate = self;
    [self.imageView addGestureRecognizer:pinch];
}
//默認(rèn)傳遞的的縮放比例都是相對(duì)于最開始的大小
- (void)pinch:(UIPinchGestureRecognizer *)pinch
{
    self.imageView.transform = CGAffineTransformScale(self.imageView.transform, pinch.scale, pinch.scale);
    
    // 復(fù)位
    pinch.scale = 1;
}
//創(chuàng)建拖拽手勢(shì)
- (void)setUpPan
{
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
    
    
    [self.imageView addGestureRecognizer:pan];
}
//一樣是默認(rèn)是相對(duì)于最初位置
- (void)pan:(UIPanGestureRecognizer *)pan
{
    // 獲取手勢(shì)的觸摸點(diǎn)
   // CGPoint curP = [pan locationInView:self.imageView];
    
    // 移動(dòng)視圖
    // 獲取手勢(shì)的移動(dòng),也是相對(duì)于最開始的位置
    CGPoint transP = [pan translationInView:self.imageView];
    
    self.imageView.transform = CGAffineTransformTranslate(self.imageView.transform, transP.x, transP.y);
    
    // 復(fù)位
    [pan setTranslation:CGPointZero inView:self.imageView];
    
  //  NSLog(@"%@",NSStringFromCGPoint(curP));
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫(kù)、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 15,401評(píng)論 4 61
  • Swift版本點(diǎn)擊這里歡迎加入QQ群交流: 594119878最新更新日期:18-09-17 About A cu...
    ylgwhyh閱讀 26,150評(píng)論 7 249
  • 手勢(shì)識(shí)別器是附加到視圖的對(duì)象,將低級(jí)別事件處理代碼轉(zhuǎn)換為更高級(jí)別的操作,它允許視圖以控件執(zhí)行的方式響應(yīng)操作。 手勢(shì)...
    坤坤同學(xué)閱讀 4,262評(píng)論 0 9
  • 一個(gè)人, 一生中會(huì)遇到很多人, 但總會(huì)有那么一個(gè)人, 讓你愛不得、得不到、忘不了、 放不下、想聯(lián)系、 沒勇氣、想關(guān)...
    雨中的蒲公英閱讀 2,176評(píng)論 0 1
  • 躍遷者的心法 真正的改變都是逆人性的。 時(shí)代新玩法:只做頭部,聯(lián)機(jī)大腦,終身提問(wèn),理解系統(tǒng)。變化更底層的改變?連接...
    PetitRenard閱讀 248評(píng)論 0 1

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