你好,歡迎來到IOS教程網

 Ios教程網 >> IOS教程 >> 關於IOS教程 >> iOS開發之觸摸事件以及手勢

iOS開發之觸摸事件以及手勢

編輯:關於IOS教程

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

觸摸事件: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__);
}
 
/**
 * 當觸摸事件被系統事件打斷時調用
 *
 * @param touches <#touches description#>
 * @param event  <#event description#>
 */
- (void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
   
  NSLog(@"%s",__func__);
}

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

說到這幾個觸摸方法,首先要知道UITouch這個對象。當一根手指觸摸屏幕時就會產生一個與之關聯的UITouch對象,一根手指對應一個UITouch對象。這個對象裡面保存著這次觸摸的信息,比如觸摸的位置,時間,階段等,當手指移動時,系統會更新同一個UITouch對象。使其能一直保存該手指所在的觸摸位置信息。當手指離開屏幕時,系統會銷毀對應的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);
}

就是根據UITouch對象中保存的位置信息來實現的。

事件的產生和傳遞:

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

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

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

hitTest方法的底層實現:

- (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;
    }
  }
   
  //循環結束,沒有比自己更合適的view,返回自己
  return self;
   
}

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

具體有以下幾種手勢:

//點按手勢
//  UITapGestureRecognizer *tap = [UITapGestureRecognizer alloc]initWithTarget:<#(nullable id)#> action:<#(nullable SEL)#>
   
  //長按手勢 默認是觸發兩次
//  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 - 手勢代理方法
// 是否允許開始觸發手勢
//- (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
{
  // 創建點按手勢
  UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
   
  tap.delegate = self;
   
  [_imageView addGestureRecognizer:tap];
}
 
- (void)tap:(UITapGestureRecognizer *)tap
{
  NSLog(@"%s",__func__);
}
 
#pragma mark - 長按手勢
// 默認會觸發兩次
- (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];
   
  // 如果以後想要一個控件支持多個方向的輕掃,必須創建多個輕掃手勢,一個輕掃手勢只支持一個方向
  // 默認輕掃的方向是往右
  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觸摸事件以及手勢的相關內容介紹,希望對大家學習iOS程序設計有所幫助。

  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved