1、呼應者鏈
以UIResponder作為超類的任何類都是呼應者。UIView和UIControl是UIReponder的子類,是以一切視圖和一切控件都是呼應者。
1、初始響應器
事宜起首會傳遞給UIApplication對象,接上去會傳遞給運用法式的UIWindow,UIWindow會選擇一個初始響應器來處置事宜。初始呼應器會選擇上面的方法選擇1、關於觸摸事宜,UIWindow會肯定用戶觸摸的視圖,然後將事宜交給注冊了這個視圖的手勢辨認器或則注冊視圖層級更高的手勢辨認器。只需存在能處置事宜的辨認器,就不會再持續找了。假如沒有的話,被觸摸視圖就是初始響應器,事宜也會傳遞給它。
2、關於用戶搖擺裝備發生的或許是來自長途遙控裝備事宜,將會傳遞給第一呼應器
假如初始呼應器不處置時光,它會將事宜傳遞給它的父視圖(假如存在的話),或許傳遞給視圖掌握器(假如此視圖是視圖掌握器的視圖)。假如視圖掌握器不處置事宜,它將沿著呼應器鏈的層級持續傳給父視圖掌握器(假如存在的話)。
假如在全部視圖層級中都沒與能處置事宜的視圖或掌握器,事宜就會被傳遞給運用法式的窗口。假如窗口不克不及處置事宜,而運用拜托是UIResponder的子類,UIApplication對象就會將其傳遞給運用法式拜托。最初,假如運用拜托不是UIResponder的子類,或許不處置這個事宜,那末這個事宜將會被拋棄。
4個手勢告訴辦法
#pragma mark - Touch Event Methods // 用戶第一次觸摸屏幕時被挪用 - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { } // 當產生某些事宜(如來電呼喚)招致手勢中止時被挪用 - (void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { } // 當用戶手指分開屏幕時被挪用 - (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { } // 當用戶手指挪動時觸發 - (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { }
2、檢測掃描事宜
1、手動檢測
// // ViewController.m // Swipes // // Created by Jierism on 16/8/4. // Copyright © 2016年 Jierism. All rights reserved. // #import "ViewController.h" // 設置檢測規模 static CGFloat const kMinimmGestureLength = 25; static CGFloat const kMaximmVariance = 5; @interface ViewController () @property (weak, nonatomic) IBOutlet UILabel *label; @property (nonatomic) CGPoint gestureStartPoint; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { UITouch *touch = [touches anyObject]; self.gestureStartPoint = [touch locationInView:self.view]; } - (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { UITouch *touch = [touches anyObject]; CGPoint currentPosition = [touch locationInView:self.view]; // 前往一個float的相對值 CGFloat deltaX = fabsf(self.gestureStartPoint.x - currentPosition.x); CGFloat deltaY = fabsf(self.gestureStartPoint.y - currentPosition.y); // 取得兩個增量後,斷定用戶在兩個偏向上挪動過的間隔,檢測用戶能否在一個偏向上挪動得足夠遠但在另外一個偏向挪動得不敷來構成輕掃舉措 if (deltaX >= kMinimmGestureLength && deltaY <= kMaximmVariance) { self.label.text = @"Horizontal swipe detected"; // 2s後擦除文本 dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ self.label.text = @""; }); }else if (deltaY >= kMinimmGestureLength && deltaX <= kMaximmVariance){ self.label.text = @"Vertical swipe detected"; dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ self.label.text = @""; }); } } @end
2、辨認器檢測
// // ViewController.m // Swipes // // Created by Jierism on 16/8/4. // Copyright © 2016年 Jierism. All rights reserved. // #import "ViewController.h" @interface ViewController () @property (weak, nonatomic) IBOutlet UILabel *label; @property (nonatomic) CGPoint gestureStartPoint; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. //創立兩個手勢辨認器 // 1、程度偏向辨認器 UISwipeGestureRecognizer *horizontal = [[UISwipeGestureRecognizer alloc] initWithtarget:self action:@selector(reportHorizontalSwipe:)]; horizontal.direction = UISwipeGestureRecognizerDirectionLeft | UISwipeGestureRecognizerDirectionRight; [self.view addGestureRecognizer:horizontal]; // 2、垂直偏向辨認器 UISwipeGestureRecognizer *vertical = [[UISwipeGestureRecognizer alloc] initWithtarget:self action:@selector(reportVerticalSwipe:)]; vertical.direction = UISwipeGestureRecognizerDirectionUp | UISwipeGestureRecognizerDirectionDown; [self.view addGestureRecognizer:vertical]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (void)reportHorizontalSwipe:(UIGestureRecognizer *)recognizer { self.label.text = @"Horizontal swipe detected"; // 2s後擦除文本 dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ self.label.text = @""; }); } - (void)reportVerticalSwipe:(UIGestureRecognizer *)recognizer { self.label.text = @"Vertical swipe detected"; // 2s後擦除文本 dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ self.label.text = @""; }); } @end
3、完成多指輕掃
// // ViewController.m // Swipes // // Created by Jierism on 16/8/4. // Copyright © 2016年 Jierism. All rights reserved. // #import "ViewController.h" @interface ViewController () @property (weak, nonatomic) IBOutlet UILabel *label; @property (nonatomic) CGPoint gestureStartPoint; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. for (NSUInteger touchCount = 1; touchCount <= 5; touchCount++) { //創立兩個手勢辨認器 // 1、程度偏向辨認器 UISwipeGestureRecognizer *horizontal = [[UISwipeGestureRecognizer alloc] initWithtarget:self action:@selector(reportHorizontalSwipe:)]; horizontal.direction = UISwipeGestureRecognizerDirectionLeft | UISwipeGestureRecognizerDirectionRight; [self.view addGestureRecognizer:horizontal]; // 2、垂直偏向辨認器 UISwipeGestureRecognizer *vertical = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(reportVerticalSwipe:)]; vertical.direction = UISwipeGestureRecognizerDirectionUp | UISwipeGestureRecognizerDirectionDown; [self.view addGestureRecognizer:vertical]; } } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (NSString *)descriptionForTouchCount:(NSUInteger)touchCount { switch (touchCount) { case 1: return @"Single"; case 2: return @"Double"; case 3: return @"Triple"; case 4: return @"Quadruple"; case 5: return @"Quintuple"; default: return @""; } } - (void)reportHorizontalSwipe:(UIGestureRecognizer *)recognizer { self.label.text = [NSString stringWithFormat:@"%@ Horizontal swipe detected",[self descriptionForTouchCount:[recognizer numberOfTouches]]]; // 2s後擦除文本 dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ self.label.text = @""; }); } - (void)reportVerticalSwipe:(UIGestureRecognizer *)recognizer { self.label.text = [NSString stringWithFormat:@"%@ Vertical swipe detected",[self descriptionForTouchCount:[recognizer numberOfTouches]]]; // 2s後擦除文本 dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ self.label.text = @""; }); } @end
4、檢測屢次輕點
// // ViewController.m // TapTaps // // Created by Jierism on 16/8/4. // Copyright © 2016年 Jierism. All rights reserved. // #import "ViewController.h" @interface ViewController () @property (weak, nonatomic) IBOutlet UILabel *singleLabel; @property (weak, nonatomic) IBOutlet UILabel *doubleLabel; @property (weak, nonatomic) IBOutlet UILabel *tripleLabel; @property (weak, nonatomic) IBOutlet UILabel *quadrupleLabel; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. // 創立4個點擊手勢辨認器 UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTap)]; singleTap.numberOfTapsRequired = 1; singleTap.numberOfTouchesRequired = 1; // 附加到視圖 [self.view addGestureRecognizer:singleTap]; UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doubleTap)]; doubleTap.numberOfTapsRequired = 2; doubleTap.numberOfTouchesRequired = 1; [self.view addGestureRecognizer:doubleTap]; // 當doubleTap呼應“掉敗”才運轉singleTap [singleTap requireGestureRecognizerToFail:doubleTap]; UITapGestureRecognizer *tripleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tripleTap)]; tripleTap.numberOfTapsRequired = 3; tripleTap.numberOfTouchesRequired = 1; [self.view addGestureRecognizer:tripleTap]; [doubleTap requireGestureRecognizerToFail:tripleTap]; UITapGestureRecognizer *quadrupleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(quadrupleTap)]; quadrupleTap.numberOfTapsRequired = 4; quadrupleTap.numberOfTouchesRequired = 1; [self.view addGestureRecognizer:quadrupleTap]; [tripleTap requireGestureRecognizerToFail:quadrupleTap]; } - (void)singleTap { self.singleLabel.text = @"Single Tap Detected"; dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ self.singleLabel.text = @""; }); } - (void)doubleTap { self.doubleLabel.text = @"Double Tap Detected"; dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ self.doubleLabel.text = @""; }); } - (void)tripleTap { self.tripleLabel.text = @"Triple Tap Detected"; dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ self.tripleLabel.text = @""; }); } - (void)quadrupleTap { self.quadrupleLabel.text = @"Quadruple Tap Detected"; dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ self.quadrupleLabel.text = @""; }); } @end
5、檢測捏合和扭轉
#import <UIKit/UIKit.h> @interface ViewController : UIViewController<UIGestureRecognizerDelegate> @end
// // ViewController.m // PinchMe // // Created by Jierism on 16/8/4. // Copyright © 2016年 Jierism. All rights reserved. // #import "ViewController.h" @interface ViewController () @property (strong,nonatomic) UIImageView *imageView; @end @implementation ViewController // 以後縮放比例,先前縮放比例 CGFloat scale,previousScale; // 以後扭轉角度,先前扭轉角度 CGFloat rotation,previousRotation; - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. previousScale = 1; UIImage *image = [UIImage imageNamed:@"yosemite-meadows"]; self.imageView = [[UIImageView alloc] initWithImage:image]; // 對圖象啟用交互功效 self.imageView.userInteractionEnabled = YES; self.imageView.center = self.view.center; [self.view addSubview:self.imageView]; // 樹立捏合手勢辨認器 UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(doPinch:)]; pinchGesture.delegate = self; [self.imageView addGestureRecognizer:pinchGesture]; // 樹立扭轉手勢辨認器 UIRotationGestureRecognizer *rotationGesture = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(doRorate:)]; rotationGesture.delegate = self; [self.imageView addGestureRecognizer:rotationGesture]; } - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer { // 許可捏合手勢和扭轉手勢同時任務。不然,先開端的手勢辨認器會屏障另外一個 return YES; } // 依據手勢辨認器中取得的縮放比例和扭轉角度對圖象停止變換 - (void)transformImageView { CGAff.netransform t = CGAff.netransformMakeScale(scale * previousScale, scale * previousScale); t = CGAff.netransformRotate(t, rotation + previousRotation); self.imageView.transform = t; } - (void)doPinch:(UIPinchGestureRecognizer *)gesture { scale = gesture.scale; [self transformImageView]; if (gesture.state == UIGestureRecognizerStateEnded) { previousScale = scale * previousScale; scale = 1; } } - (void)doRorate:(UIRotationGestureRecognizer *)gesture { rotation = gesture.rotation; [self transformImageView]; if (gesture.state == UIGestureRecognizerStateEnded) { previousRotation = rotation + previousRotation; rotation = 0; } } @end
6、自界說手勢
// // CheckMarkRecognizer.m // CheckPlease // // Created by Jierism on 16/8/4. // Copyright © 2016年 Jierism. All rights reserved. // #import "CheckMarkRecognizer.h" #import "CGPointUtils.h" #import <UIKit/UIGestureRecognizerSubclass.h> // 一個主要目標是使手勢辨認器的state屬性可寫,子類將應用這個機制斷言我們所不雅察的手勢已勝利完成 // 設置檢測規模 static CGFloat const kMinimunCheckMarkAngle = 80; static CGFloat const kMaximumCheckMarkAngle = 100; static CGFloat const kMinimumCheckMarkLength = 10; @implementation CheckMarkRecognizer{ // 前兩個實例變量供給之前的線段 CGPoint lastPreviousPoint; CGPoint lastCurrentPoint; // 畫出的線段長度 CGFloat lineLengthSoFar; } // 用lastPreviousPoint和lastCurrentPoint構成第一條線段,跟第二條線段構成角度去完成手勢 - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { [super touchesBegan:touches withEvent:event]; UITouch *touch = [touches anyObject]; CGPoint point = [touch locationInView:self.view]; lastPreviousPoint = point; lastCurrentPoint = point; lineLengthSoFar = 0.0; } - (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { [super touchesMoved:touches withEvent:event]; UITouch *touch = [touches anyObject]; CGPoint previousPoint = [touch previousLocationInView:self.view]; CGPoint currentPoint = [touch locationInView:self.view]; CGFloat angle = angleBetweenLines(lastPreviousPoint, lastCurrentPoint, previousPoint, currentPoint); if (angle >= kMinimunCheckMarkAngle && angle <= kMaximumCheckMarkAngle && lineLengthSoFar > kMinimumCheckMarkLength) { self.state = UIGestureRecognizerStateRecognized; } lineLengthSoFar += distanceBetweenPoints(previousPoint, currentPoint); lastPreviousPoint = previousPoint; lastCurrentPoint = currentPoint; } @end
// // ViewController.m // CheckPlease // // Created by Jierism on 16/8/4. // Copyright © 2016年 Jierism. All rights reserved. // #import "ViewController.h" #import "CheckMarkRecognizer.h" @interface ViewController () @property (weak, nonatomic) IBOutlet UIImageView *imageView; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. CheckMarkRecognizer *check = [[CheckMarkRecognizer alloc] initWithTarget:self action:@selector(doCheck:)]; [self.view addGestureRecognizer:check]; self.imageView.hidden = YES; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (void)doCheck:(CheckMarkRecognizer *)check { self.imageView.hidden = NO; dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ self.imageView.hidden = YES; }); } @end
以上就是本文的全體內容,願望對年夜家的進修有所贊助,也願望年夜家多多支撐本站。
【iOS輕點、觸摸和手勢代碼開辟】的相關資料介紹到這裡,希望對您有所幫助! 提示:不會對讀者因本文所帶來的任何損失負責。如果您支持就請把本站添加至收藏夾哦!