IOS開發—手勢的3個混淆屬性

來源:互聯網
上載者:User

標籤:ios開發   cancelstouchesinview   delaystouchesbegan   delaystouchesended   

手勢的3個混淆屬性

/**

 *  本節介紹tap的3個弄不太林清並且容易混淆的屬性:

   cancelsTouchesInView/delaysTouchesBegan/delaysTouchesEnded

 *   (0)首先要知道的是

        1.這3個屬性是作用於GestureRecognizers(手勢識別)與觸摸事件之間聯絡的屬性。實際應用中好像很少會把它們放到一起,大多都只是運用手勢識別,所以這3個屬性應該很少會用到。

        2.對於觸摸事件,window只會有一個控制項來接收touch。這個控制項是首先接觸到touch的並且重寫了觸摸事件方法(一個即可)的控制項

        3.手勢識別和觸摸事件是兩個獨立的事,只是可以通過這3個屬性互相影響,不要混淆。

 *  (1)在預設情況下(即這3個屬性都處於預設值的情況下),如果觸摸window,首先由window上最先合格控制項(該控制項記為hit-test view)接收到該touch並觸發觸摸事件touchesBegan。同時如果某個控制項的手勢辨識器接收到了該touch,就會進行識別。手勢識別成功之後發送觸摸事件touchesCancelled給hit-testview,hit-test view不再響應touch。

 

 * (2)cancelsTouchesInView:

    預設為YES,這種情況下當手勢辨識器識別到touch之後,會發送touchesCancelled給hit-testview以取消hit-test view對touch的響應,這個時候只有手勢辨識器響應touch。

    當設定成NO時,手勢辨識器識別到touch之後不會發送touchesCancelled給hit-test,這個時候手勢辨識器和hit-test view均響應touch。

 

 * (3)delaysTouchesBegan:

    預設是NO,這種情況下當發生一個touch時,手勢辨識器先捕捉到到touch,然後發給hit-testview,兩者各自做出響應。如果設定為YES,手勢辨識器在識別的過程中(注意是識別過程),不會將touch發給hit-test view,即hit-testview不會有任何觸摸事件。只有在識別失敗之後才會將touch發給hit-testview,這種情況下hit-test view的響應會延遲約0.15ms。

 

 * (4)delaysTouchesEnded:

    預設為YES。這種情況下發生一個touch時,在手勢識別成功後,發送給touchesCancelled訊息給hit-testview,手勢識別失敗時,會延遲大概0.15ms,期間沒有接收到別的touch才會發送touchesEnded。如果設定為NO,則不會延遲,即會立即發送touchesEnded以結束當前觸摸。

 */

示範代碼如下,讀者自行拷貝嘗試

MyViw繼承自UIView:

#import "MyView.h"@implementation MyView- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{    NSLog(@"__begin");}- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event{    NSLog(@"__cancel");}- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{    NSLog(@"__end");}- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{    NSLog(@"__touch");}@end

#import "ViewController.h"#import "MyView.h"@interface ViewController ()@property (nonatomic, strong) MyView *myView;@end@implementation ViewController- (void)viewDidLoad {    [super viewDidLoad];    _myView = [[MyView alloc]initWithFrame:CGRectMake(50, 50, [UIScreen mainScreen].bounds.size.width - 100, 200)];    _myView.backgroundColor = [UIColor yellowColor];    [self.view addSubview:_myView];    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];    tap.numberOfTapsRequired = 2;    [_myView addGestureRecognizer:tap];   }- (void)tapAction:(UITapGestureRecognizer *)tap{    NSLog(@"點擊");} - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{    NSLog(@"begin");}- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event{    NSLog(@"cancel");} - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{    NSLog(@"end");}- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{    NSLog(@"touch");}@end

IOS開發—手勢的3個混淆屬性

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.