日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

手势

發布時間:2025/7/14 编程问答 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 手势 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

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

總結

以上是生活随笔為你收集整理的手势的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。