你好,歡迎來到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 = CGAff.netransformTranslate(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 = CGAff.netransformRotate(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 = CGAff.netransformScale(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法式設計有所贊助。

【iOS開辟之觸摸事宜和手勢】的相關資料介紹到這裡,希望對您有所幫助! 提示:不會對讀者因本文所帶來的任何損失負責。如果您支持就請把本站添加至收藏夾哦!

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