手势
UIkit框架中絕大多數(shù)的控件都是繼承自,UIResponder類,UIResponder 類有強(qiáng)大的處理觸摸事件的能力。假如一個(gè)UIview 收到一個(gè)觸摸事件,那么這個(gè)觸摸事件就會(huì)去進(jìn)行尋找相應(yīng)的響應(yīng)事件,如果在該UIview 中找不到,就尋找UIView的對象去處理,如果UIView對象沒有權(quán)利處理,就往當(dāng)前的上一層UIViewController去尋找,如果找不到就再尋找 UIViewController 的對象去處理,如果這個(gè)對象仍然不能處理,就再往上層 UIWindow 對象去處理,如果它熱不能解決觸摸事件的響應(yīng),那該觸摸事件就會(huì)被傳遞到 UIApplication 代理對象,如果該代理對象仍不能解決,那就交給系統(tǒng)回收。
總結(jié)一下:相當(dāng)于在村里發(fā)生一件事,村長不能決定,這時(shí)就一級一級上報(bào),可是一直沒有得到處理,直到最后有個(gè)很大權(quán)力的人拍板決定,才算結(jié)束。要不就一直往上報(bào)。
系統(tǒng)將事件封裝到 UIEvent 類中,然后由系統(tǒng)去處理。ios 將事件分為三種:觸摸事件、動(dòng)作事件、外部控制事件。動(dòng)作事件:就是用戶對手機(jī)進(jìn)行的特定的動(dòng)做,比如搖一搖;外部控制事件:就是控制手機(jī)連上手機(jī)設(shè)備時(shí)候的事件;觸摸事件:就是用戶與手機(jī)屏幕的相關(guān)事件。
每一個(gè)用戶交互對象 UIResponder 都有一組響應(yīng)事件函數(shù)。通常我們都要重寫這組函數(shù)。以供我們使用相應(yīng)的邏輯。
關(guān)于概念的知識 參考
代碼實(shí)現(xiàn)功能:打印出鼠標(biāo)的手勢事件
建一個(gè) 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:@"點(diǎn)擊跳轉(zhuǎn)" 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: 在一個(gè)有 UITableView ?的頁面,在view 上添加一個(gè)手勢,要實(shí)現(xiàn)輕拍 非 UITableView 的地方關(guān)閉頁面,這時(shí)候發(fā)現(xiàn)點(diǎn)擊了UITableVIew 也會(huì)關(guān)閉該頁面,因?yàn)闀r(shí)間的傳遞導(dǎo)致了改結(jié)果,如何解決,只需要實(shí)現(xiàn)下面的手勢協(xié)議即可,協(xié)議內(nèi)部代碼如下:
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {if ([NSStringFromClass([touch.view class]) isEqualToString:@"UITableViewCellContentView"]) {return NO;}return YES; }?
轉(zhuǎn)載于:https://www.cnblogs.com/benpaobadaniu/p/4926227.html
總結(jié)
- 上一篇: 软件测试流程参考一
- 下一篇: 【读书笔记】程序员的自我修养总结(七)