手势
UIkit框架中絕大多數的控件都是繼承自,UIResponder類,UIResponder 類有強大的處理觸摸事件的能力。假如一個UIview 收到一個觸摸事件,那么這個觸摸事件就會去進行尋找相應的響應事件,如果在該UIview 中找不到,就尋找UIView的對象去處理,如果UIView對象沒有權利處理,就往當前的上一層UIViewController去尋找,如果找不到就再尋找 UIViewController 的對象去處理,如果這個對象仍然不能處理,就再往上層 UIWindow 對象去處理,如果它熱不能解決觸摸事件的響應,那該觸摸事件就會被傳遞到 UIApplication 代理對象,如果該代理對象仍不能解決,那就交給系統回收。
總結一下:相當于在村里發生一件事,村長不能決定,這時就一級一級上報,可是一直沒有得到處理,直到最后有個很大權力的人拍板決定,才算結束。要不就一直往上報。
系統將事件封裝到 UIEvent 類中,然后由系統去處理。ios 將事件分為三種:觸摸事件、動作事件、外部控制事件。動作事件:就是用戶對手機進行的特定的動做,比如搖一搖;外部控制事件:就是控制手機連上手機設備時候的事件;觸摸事件:就是用戶與手機屏幕的相關事件。
每一個用戶交互對象 UIResponder 都有一組響應事件函數。通常我們都要重寫這組函數。以供我們使用相應的邏輯。
關于概念的知識 參考
代碼實現功能:打印出鼠標的手勢事件
建一個 UIView 的文件命名為?TouchView 在視圖控制器里寫上
#import "RootViewController.h" #import "TouchView.h"@interface RootViewController ()@end@implementation RootViewController- (void)viewDidLoad {[super viewDidLoad];[self setTouchView]; }-(void)setTouchView{TouchView * touchView = [[TouchView alloc]initWithFrame:CGRectMake(50, 50, 100, 100)];touchView.backgroundColor = [UIColor redColor];[self.view addSubview:touchView];UIButton * Button = [UIButton buttonWithType:UIButtonTypeCustom];Button.frame = CGRectMake(20, 200, 280, 30);Button.backgroundColor = [UIColor grayColor];[Button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];[Button setTitle:@"點擊跳轉" forState:UIControlStateNormal];[self.view addSubview:Button];pinchView * View = [[pinchView alloc]initWithFrame:CGRectMake(50, 300, 100, 100)];View.backgroundColor = [UIColor blackColor];[self.view addSubview:View];}-(void)buttonAction:(UIButton *)sender{SecondViewController * SVC = [[SecondViewController alloc]init];[self.navigationController pushViewController:SVC animated:YES]; }- (void)didReceiveMemoryWarning {[super didReceiveMemoryWarning];// Dispose of any resources that can be recreated. }?
在 UIView.m文件里寫上
#import "TouchView.h"@implementation TouchView-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{[self updateInfor:[touches anyObject] withMethodName:@"touchesBegin"]; } -(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event{[self updateInfor:[touches anyObject] withMethodName:@"touchesCancelled"]; } -(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{[self updateInfor:[touches anyObject] withMethodName:@"touchesEnded"]; } -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{[self updateInfor:[touches anyObject] withMethodName:@"touchesMoved"]; }-(void)updateInfor:(UITouch *)aTouch withMethodName:(NSString *)aMethodName{NSString * strPhase = @"";switch (aTouch.phase) {case UITouchPhaseBegan:strPhase = @"UITouchPhaseBegan";break;case UITouchPhaseEnded:strPhase = @"UITouchPhaseEnded";break;case UITouchPhaseCancelled:strPhase = @"UITouchPhaseCancelled";break;case UITouchPhaseMoved:strPhase = @"UITouchPhaseMoved";break;default:break;}NSLog(@"操作事件是 %@",strPhase); } @end?觸摸事件沖突,eg: 在一個有 UITableView ?的頁面,在view 上添加一個手勢,要實現輕拍 非 UITableView 的地方關閉頁面,這時候發現點擊了UITableVIew 也會關閉該頁面,因為時間的傳遞導致了改結果,如何解決,只需要實現下面的手勢協議即可,協議內部代碼如下:
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {if ([NSStringFromClass([touch.view class]) isEqualToString:@"UITableViewCellContentView"]) {return NO;}return YES; }?
轉載于:https://www.cnblogs.com/benpaobadaniu/p/4926227.html
總結
- 上一篇: 软件测试流程参考一
- 下一篇: 【读书笔记】程序员的自我修养总结(七)