iOS手勢總結(jié)

手勢并發(fā)執(zhí)行的方法

1. 設(shè)置手勢的delegate;實(shí)現(xiàn)代理方法
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer;

手勢的優(yōu)先級設(shè)定

當(dāng)識別為gesture01時(shí)不會立刻觸發(fā)gesture01,而是檢測gesture02是否成功履澳,如果gesture02檢測失敗后再觸發(fā)gesture01,達(dá)到解決沖突的結(jié)果哑子。
[gesture01 requireGestureRecognizerToFail:gesture02];

點(diǎn)按手勢 UITapGestureRecognizer

1. 創(chuàng)建手勢
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
2. 設(shè)置手勢屬性(可省略)
tap.numberOfTapsRequired = 1; 需要點(diǎn)擊的次數(shù),默認(rèn)是1.
tap.numberOfTouchesRequired = 1; 需要點(diǎn)擊的手指數(shù),默認(rèn)是1.
[tap requireGestureRecoginzerToFail:doubleTap]; doubleTap是自定義的一個(gè)雙擊事件.使用這個(gè)方法可以取消掉單擊和雙擊事件并存的情況.
3. 添加手勢
[self.imgView addGestureRecognizer:tap];
4. 實(shí)現(xiàn)方法
- (void)tap:(UITapGestureRecoginzer *)tapGestureRecognizer{
    // 點(diǎn)擊后執(zhí)行的方法
}

長按手勢 UILongPressGestureRecognizer

1. 創(chuàng)建手勢
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
2. 設(shè)置屬性
longPress.minimumPressDuration = 0.5; 至少0.5秒后觸發(fā)
3. 添加手勢
[self.imgView addGestureRecognizer:longPress];
4. 實(shí)現(xiàn)方法 與狀態(tài)配合實(shí)現(xiàn)
- (void)longPress:(UILongPressGestureRecognizer *)longPressGesture {
    if(longPressGesture.state == UIGestureRecognizerStateBegan) {
        // 長按手勢開始
    }
    if(longPressGesture.state == UIGestureRecognizerStateChanged) {
        // 長按手勢 手指位置變化
    }
    if(longPressGesture.state == UIGestureRecognizerStateFailed) {
      // 手勢識別失敗,恢復(fù)默認(rèn)狀態(tài)
    }
    if(longPressGesture.state == UIGestureRecognizerStateCancelled) {
        // 手勢取消乒疏,恢復(fù)默認(rèn)狀態(tài)
    }
    if(longPressGesture.state == UIGestureRecognizerStateRecognized) {
        // 手勢狀態(tài)已響應(yīng)
    }
    if(longPressGesture.state == UIGestureRecognizerStateEnded) {
        // 長按手勢 狀態(tài)結(jié)束
    }
}

輕掃手勢盖高,滑動(dòng) UISwipeGestureRecognizer

創(chuàng)建手勢,滑動(dòng)手勢有四個(gè)方向听想,如果處理方法相同則可以使用“按位或 | ”設(shè)置滑動(dòng)方向,若處理方法不同則需要?jiǎng)?chuàng)建多個(gè)對象添加
UISwipeGestureRecognizer *swipe01 = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe:)];
swipe01.direction = UISwipeGestureRecognizerDirectionDown;
設(shè)置手勢的優(yōu)先級用來解決沖突
[swipe requireGestureRecoginzerToFail:pan];
[self.imgView addGestureRecognizer: swipe01];

UISwipeGestureRecognizer *swipe02 = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe:)];
swipe02.direction = UISwipeGestureRecognizerDirectionUp;
[self.imgView addGestureRecognizer: swipe02];

UISwipeGestureRecognizer *swipe03 = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe:)];
swipe03.direction = UISwipeGestureRecognizerDirectionLeft | UISwipeGestureRecognizerDirectionRight;
[self.imgView addGestureRecognizer: swipe03];

實(shí)現(xiàn)方法
typedef NS_OPTIONS(NSUInteger, UISwipeGestureRecognizerDirection) {
    UISwipeGestureRecognizerDirectionRight = 1 << 0,
    UISwipeGestureRecognizerDirectionLeft  = 1 << 1,
    UISwipeGestureRecognizerDirectionUp    = 1 << 2,
    UISwipeGestureRecognizerDirectionDown  = 1 << 3
};
- (void)swipe:(UISwipeGestureRecognizer *)swipeGesture {
    // 判斷方向?qū)崿F(xiàn)不同方法 向左向右不能執(zhí)行因?yàn)闋顟B(tài)為:3;
    if(swipeGesture.direction == UISwipeGestureRecognizerDirectionLeft) {
        // 向左滑動(dòng)
    }
    if(swipeGesture.direction == UISwipeGestureRecognizerDirectionRight) {
        // 向右滑動(dòng)
    }
    if(swipeGesture.direction == UISwipeGestureRecognizerDirectionUp) {
        // 向上滑動(dòng)
    }
    if(swipeGesture.direction == UISwipeGestureRecognizerDirectionDown) {
        // 向下滑動(dòng)
    }
}

拖動(dòng)手勢 UIPanGestureRecognizer

1. 創(chuàng)建手勢
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
2. 添加手勢
[self.imgView addGestureRecognizer:pan];
3. 實(shí)現(xiàn)方法
- (void)pan:(UIPanGestureRecognizer *)pan {
    1. localInView 是觸摸點(diǎn)在View的位置
    2. translationInView 是點(diǎn)位移的位置
    3. 手勢.view 是手勢添加在View身上
    CGPoint translation = [pan translationInView:pan.view];
    CGPoint center = pan.view.center;
    center.x = center.x + translation.x;
    center.y = center.y + translation.y;
    pan.view.center = center;
    
    // 復(fù)位
    [pan setTranslation:CGPointZero inView:pan.view];
}

旋轉(zhuǎn)手勢 UIRotationGestureRecognizer

1. 創(chuàng)建手勢
UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotation:)];
2. 添加手勢
[self.imgView addGestureRecognizer:rotation];
3. 實(shí)現(xiàn)方法
- (void)rotation:(UIRotationGestureRecognizer *)rotationGesture {
    //獲取旋轉(zhuǎn)的角度
    CGFloat scale = rotationGesture.rotation;
    // 設(shè)置view的角度,使用transform設(shè)置
    rotationGesture.view.transform = CGAffineTransformRotate(rotationGesture.view.transform, scale);
    
    // 復(fù)位
    rotationGesture.rotation = 0;
}

捏合手勢 縮放 UIPinchGestureRecognizer

1. 創(chuàng)建手勢
UIPinchGestureRecognizer *pinch = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(pinch:)];
2. 添加手勢
[self.imgView addGestureRecognizer:pinch];
3. 實(shí)現(xiàn)方法
- (void)pinch:(UIPinchGestureRecognizer *)pinchGesture {
    //獲取縮放值
    CGFloat scale = pinchGesture.scale;
    // 設(shè)置值
    pinchGesture.view.transform = CGAffineTransformScale(pinchGesture.view.transform, scale, scale);
    // 復(fù)位
    pinchGesture.scale = 1;
}

屏幕邊緣滑動(dòng)手勢 UIScreenEdgePanGestureRecognizer

screenEdgePan是UIPanGestureRecognizer的子類贱鼻,用法相同宴卖,不過screenEdgePan是專用于響應(yīng)從屏幕邊緣滑動(dòng)的效果。
可設(shè)置四個(gè)方向的效果.
screenEdgePan.edges = UIRectEdgeAll;
screenEdgePan.edges = UIRectEdgeLeft | UIRectEdgeRight;
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末邻悬,一起剝皮案震驚了整個(gè)濱河市症昏,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌父丰,老刑警劉巖肝谭,帶你破解...
    沈念sama閱讀 216,470評論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異蛾扇,居然都是意外死亡攘烛,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,393評論 3 392
  • 文/潘曉璐 我一進(jìn)店門镀首,熙熙樓的掌柜王于貴愁眉苦臉地迎上來坟漱,“玉大人,你說我怎么就攤上這事更哄∮蟪荩” “怎么了须眷?”我有些...
    開封第一講書人閱讀 162,577評論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長沟突。 經(jīng)常有香客問我,道長捕传,這世上最難降的妖魔是什么惠拭? 我笑而不...
    開封第一講書人閱讀 58,176評論 1 292
  • 正文 為了忘掉前任,我火速辦了婚禮庸论,結(jié)果婚禮上职辅,老公的妹妹穿的比我還像新娘。我一直安慰自己聂示,他們只是感情好域携,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,189評論 6 388
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著鱼喉,像睡著了一般秀鞭。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上扛禽,一...
    開封第一講書人閱讀 51,155評論 1 299
  • 那天锋边,我揣著相機(jī)與錄音,去河邊找鬼编曼。 笑死豆巨,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的掐场。 我是一名探鬼主播往扔,決...
    沈念sama閱讀 40,041評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼熊户!你這毒婦竟也來了萍膛?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 38,903評論 0 274
  • 序言:老撾萬榮一對情侶失蹤嚷堡,失蹤者是張志新(化名)和其女友劉穎卦羡,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體麦到,經(jīng)...
    沈念sama閱讀 45,319評論 1 310
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡绿饵,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,539評論 2 332
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了瓶颠。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片拟赊。...
    茶點(diǎn)故事閱讀 39,703評論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖粹淋,靈堂內(nèi)的尸體忽然破棺而出吸祟,到底是詐尸還是另有隱情瑟慈,我是刑警寧澤,帶...
    沈念sama閱讀 35,417評論 5 343
  • 正文 年R本政府宣布屋匕,位于F島的核電站葛碧,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏过吻。R本人自食惡果不足惜进泼,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,013評論 3 325
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望纤虽。 院中可真熱鬧乳绕,春花似錦、人聲如沸逼纸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,664評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽杰刽。三九已至菠发,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間贺嫂,已是汗流浹背雷酪。 一陣腳步聲響...
    開封第一講書人閱讀 32,818評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留涝婉,地道東北人哥力。 一個(gè)月前我還...
    沈念sama閱讀 47,711評論 2 368
  • 正文 我出身青樓,卻偏偏與公主長得像墩弯,于是被迫代替她去往敵國和親吩跋。 傳聞我的和親對象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,601評論 2 353

推薦閱讀更多精彩內(nèi)容