国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > iOS > 正文

iOS開發(fā)之觸摸事件以及手勢

2020-07-26 03:23:01
字體:
來源:轉載
供稿:網(wǎng)友

iOS中的事件分為三類:觸摸事件、加速計事件、遠程控制事件。只有繼承了UIResponder的對象才能接收并處理事件,稱之為“響應者對象”。UIApplication、UIViewController、UIView都繼承自UIResponder。UIResponder內(nèi)部提供的方法來處理事件:

觸摸事件:touchesBegan、touchesMoved、touchesEnded、touchesCancelled

加速計事件:motionBegan、motionEnded、motionCancelled

遠程控制事件:remoteControlReceivedWithEvent

UIVeiw的觸摸事件處理過程:

/** * 當手指開始觸摸view時調用 * * @param touches <#touches description#> * @param event  <#event description#> */- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {     NSLog(@"%s",__func__);} /** * 當手指在view上移動時調用 * * @param touches <#touches description#> * @param event  <#event description#> */- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {  NSLog(@"%s",__func__);} /** * 當手指離開view時調用 * * @param touches <#touches description#> * @param event  <#event description#> */- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {     NSLog(@"%s",__func__);} /** * 當觸摸事件被系統(tǒng)事件打斷時調用 * * @param touches <#touches description#> * @param event  <#event description#> */- (void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {     NSLog(@"%s",__func__);}

一次觸摸動作必然會調用touchesBeagn、touchesMoved和touchesEnded這三個方法。

說到這幾個觸摸方法,首先要知道UITouch這個對象。當一根手指觸摸屏幕時就會產(chǎn)生一個與之關聯(lián)的UITouch對象,一根手指對應一個UITouch對象。這個對象里面保存著這次觸摸的信息,比如觸摸的位置,時間,階段等,當手指移動時,系統(tǒng)會更新同一個UITouch對象。使其能一直保存該手指所在的觸摸位置信息。當手指離開屏幕時,系統(tǒng)會銷毀對應的UITouch對象。

@interface UITouch : NSObject @property(nonatomic,readonly) NSTimeInterval   timestamp;@property(nonatomic,readonly) UITouchPhase    phase;@property(nonatomic,readonly) NSUInteger     tapCount;  // touch down within a certain point within a certain amount of time // majorRadius and majorRadiusTolerance are in points// The majorRadius will be accurate +/- the majorRadiusTolerance@property(nonatomic,readonly) CGFloat majorRadius NS_AVAILABLE_IOS(8_0);@property(nonatomic,readonly) CGFloat majorRadiusTolerance NS_AVAILABLE_IOS(8_0); @property(nullable,nonatomic,readonly,strong) UIWindow            *window;@property(nullable,nonatomic,readonly,strong) UIView             *view;@property(nullable,nonatomic,readonly,copy)  NSArray <UIGestureRecognizer *> *gestureRecognizers NS_AVAILABLE_IOS(3_2); //獲取當前位置- (CGPoint)locationInView:(nullable UIView *)view;//獲取上一個觸摸點的位置- (CGPoint)previousLocationInView:(nullable UIView *)view; // Force of the touch, where 1.0 represents the force of an average touch@property(nonatomic,readonly) CGFloat force NS_AVAILABLE_IOS(9_0);// Maximum possible force with this input mechanism@property(nonatomic,readonly) CGFloat maximumPossibleForce NS_AVAILABLE_IOS(9_0); @end


eg:讓一個view隨著手指的移動而移動

/** * 當手指在view上移動時調用 * * @param touches <#touches description#> * @param event  <#event description#> */- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {  NSLog(@"%s",__func__);     //獲取UITouch對象  UITouch *touch = [touches anyObject];     //獲取當前點的位置  CGPoint curP = [touch locationInView:self];     //獲取上一個點的位置  CGPoint preP = [touch previousLocationInView:self];     //計算x的偏移量  CGFloat offsetX = curP.x - preP.x;     //計算y的偏移量  CGFloat offsetY = curP.y = preP.y;     //修改view的位置  self.transform = CGAffineTransformTranslate(self.transform, offsetX, offsetY);}


就是根據(jù)UITouch對象中保存的位置信息來實現(xiàn)的。

事件的產(chǎn)生和傳遞:

當觸摸事件產(chǎn)生后,系統(tǒng)會將該事件添加到一個由UIApplication管理的事件隊列中去。UIApplication會從隊列中取出最前面的事件,發(fā)送給應用程序的主窗口的處理。主窗口會在視圖層次結構中,找一個最合適的視圖并調用touches方法來處理觸摸事件。觸摸事件的傳遞是從父控件傳遞到子控件。如果父控件不能接收到觸摸事件,那么子控件就不可能 接收到觸摸事件。

如何找到最合適的控件來處理事件?首先判斷自己是否能接收觸摸事件?觸摸點是否在自己身上?從后往前遍歷子控件,重復之前的兩個步驟,如果沒有符合條件的子控件,那么就自己最合適處理。

控件用hitTest:withEvent:方法來尋找最合適的view,用pointInside這個方法判斷這個點在不在方法調用者即控件身上。

hitTest方法的底層實現(xiàn):

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {     //判斷當前控件是否能接收觸摸事件  if (self.userInteractionEnabled == NO || self.hidden == YES || self.alpha <= 0.01) {    return nil;  }     //判斷觸摸點是否在當前控件上  if ([self pointInside:point withEvent:event] == NO) {    return nil;  }     //從后往前遍歷自己的子控件  NSInteger count = self.subviews.count;  for (NSInteger i = count - 1; i >= 0; i--) {    UIView *childView = self.subviews[i];         //把當前控件上的坐標系轉換成子控件上的坐標系    CGPoint childPoint = [self convertPoint:point toView:childView];         //遞歸調用hitTest方法尋找最合適的view    UIView *fitView = [childView hitTest:childPoint withEvent:event];         if (fitView) {      return fitView;    }  }     //循環(huán)結束,沒有比自己更合適的view,返回自己  return self;   }

然而使用touches方法監(jiān)聽觸摸事件是有缺點的,比如要自定義view,所以iOS3.2之后蘋果推出了手勢識別功能UIGestureRecognizer。UIGestureRecognizer是一個抽象類,它的子類才能處理具體的某個手勢。

具體有以下幾種手勢:

//點按手勢//  UITapGestureRecognizer *tap = [UITapGestureRecognizer alloc]initWithTarget:<#(nullable id)#> action:<#(nullable SEL)#>     //長按手勢 默認是觸發(fā)兩次//  UILongPressGestureRecognizer *longP = [UILongPressGestureRecognizer alloc]initWithTarget:<#(nullable id)#> action:<#(nullable SEL)#>     //輕掃手勢 默認方向是往右//  UISwipeGestureRecognizer *swipe = [UISwipeGestureRecognizer alloc]initWithTarget:<#(nullable id)#> action:<#(nullable SEL)#>     //旋轉手勢//  UIRotationGestureRecognizer *rotation = [UIRotationGestureRecognizer alloc]initWithTarget:<#(nullable id)#> action:<#(nullable SEL)#>   //捏合手勢//  UIPinchGestureRecognizer *pinch = [UIPinchGestureRecognizer alloc]initWithTarget:<#(nullable id)#> action:<#(nullable SEL)#>     //拖拽手勢//  UIPanGestureRecognizer *pan = [UIPanGestureRecognizer alloc]initWithTarget:<#(nullable id)#> action:<#(nullable SEL)#>

實際運用:

@interface ViewController ()<UIGestureRecognizerDelegate>@property (weak, nonatomic) IBOutlet UIImageView *imageView; @end @implementation ViewController - (void)viewDidLoad {  [super viewDidLoad];   [self setUpPinch];     [self setUpRotation];   [self setUpPan];   }#pragma mark - 手勢代理方法// 是否允許開始觸發(fā)手勢//- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer//{//  return NO;//} // 是否允許同時支持多個手勢,默認是不支持多個手勢// 返回yes表示支持多個手勢- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{  return YES;} // 是否允許接收手指的觸摸點//- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{//  // 獲取當前的觸摸點//  CGPoint curP = [touch locationInView:self.imageView];//  //  if (curP.x < self.imageView.bounds.size.width * 0.5) {//    return NO;//  }else{//    return YES;//  }//}  #pragma mark - 點按手勢 - (void)setUpTap{  // 創(chuàng)建點按手勢  UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];     tap.delegate = self;     [_imageView addGestureRecognizer:tap];} - (void)tap:(UITapGestureRecognizer *)tap{  NSLog(@"%s",__func__);} #pragma mark - 長按手勢// 默認會觸發(fā)兩次- (void)setUpLongPress{  UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];     [self.imageView addGestureRecognizer:longPress];}  - (void)longPress:(UILongPressGestureRecognizer *)longPress{     if (longPress.state == UIGestureRecognizerStateBegan) {         NSLog(@"%s",__func__);  }} #pragma mark - 輕掃- (void)setUpSwipe{  // 默認輕掃的方向是往右  UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe)];     swipe.direction = UISwipeGestureRecognizerDirectionUp;     [self.imageView addGestureRecognizer:swipe];     // 如果以后想要一個控件支持多個方向的輕掃,必須創(chuàng)建多個輕掃手勢,一個輕掃手勢只支持一個方向  // 默認輕掃的方向是往右  UISwipeGestureRecognizer *swipeDown = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe)];     swipeDown.direction = UISwipeGestureRecognizerDirectionDown;     [self.imageView addGestureRecognizer:swipeDown];    } - (void)swipe{  NSLog(@"%s",__func__);} #pragma mark - 旋轉手勢- (void)setUpRotation{  UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotation:)];  rotation.delegate = self;  [self.imageView addGestureRecognizer:rotation];} // 默認傳遞的旋轉的角度都是相對于最開始的位置- (void)rotation:(UIRotationGestureRecognizer *)rotation{     self.imageView.transform = CGAffineTransformRotate(self.imageView.transform, rotation.rotation);     // 復位  rotation.rotation = 0;     // 獲取手勢旋轉的角度  NSLog(@"%f",rotation.rotation);} #pragma mark - 捏合- (void)setUpPinch{  UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinch:)];  pinch.delegate = self;  [self.imageView addGestureRecognizer:pinch];} - (void)pinch:(UIPinchGestureRecognizer *)pinch{  self.imageView.transform = CGAffineTransformScale(self.imageView.transform, pinch.scale, pinch.scale);     // 復位     pinch.scale = 1;} #pragma mark - 拖拽- (void)setUpPan{  UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];        [self.imageView addGestureRecognizer:pan];} - (void)pan:(UIPanGestureRecognizer *)pan{  // 獲取手勢的觸摸點  // CGPoint curP = [pan locationInView:self.imageView];     // 移動視圖  // 獲取手勢的移動,也是相對于最開始的位置  CGPoint transP = [pan translationInView:self.imageView];     self.imageView.transform = CGAffineTransformTranslate(self.imageView.transform, transP.x, transP.y);     // 復位  [pan setTranslation:CGPointZero inView:self.imageView];    // NSLog(@"%@",NSStringFromCGPoint(curP));} @end

以上就是iOS觸摸事件以及手勢的相關內(nèi)容介紹,希望對大家學習iOS程序設計有所幫助。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 淮滨县| 红河县| 济南市| 宿州市| 贡觉县| 麻城市| 莆田市| 天等县| 阳泉市| 漳浦县| 蒲城县| 平阳县| 云霄县| 鄂尔多斯市| 阿图什市| 城步| 赤壁市| 根河市| 仙居县| 沾化县| 全椒县| 长岛县| 日照市| 视频| 乌什县| 文山县| 永寿县| 志丹县| 磐石市| 安龙县| 信宜市| 潮州市| 曲沃县| 吉林省| 洛宁县| 墨竹工卡县| 高邑县| 永平县| 潞城市| 繁昌县| 闻喜县|