代码之家  ›  专栏  ›  技术社区  ›  Andrew M

UIView上的触摸事件

  •  30
  • Andrew M  · 技术社区  · 15 年前

    6 回复  |  直到 10 年前
        1
  •  83
  •   Cœur Gustavo Armenta    6 年前

    获取接触的一种非常通用的方法是在自定义UIView子类中重写这些方法:

    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
    - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;
    - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;
    - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
    

    这些方法的官方文件如下 在里面 the UIResponder class docs (UIView继承了这些方法,因为它是UIResponder的一个子类)。越来越多的介绍性文档可以在 Event Handling Guide for iOS .

    如果您只想检测到一个轻触(触按,然后在您的视图中触按),则添加一个 UIButton 作为视图的子视图,并添加自己的自定义方法作为该按钮的目标/操作对。默认情况下,自定义按钮不可见,因此不会影响视图的外观。

    如果你在寻找更高级的互动,了解 the UIGestureRecognizer class

        2
  •  16
  •   itsji10dra Anuj J    10 年前

    使用UIView上的UIAnimation创建触摸事件,您可以在任何地方管理UIView上的触摸。

    以下代码:这里self.finalScore是一个UIView,cup是一个UIImageView。我来处理

    UIImageView上的touch事件,它出现在UIView中。

    - (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    
    UITouch *touch1 = [touches anyObject];
    CGPoint touchLocation = [touch1 locationInView:self.finalScore];
    CGRect startRect = [[[cup layer] presentationLayer] frame];
    CGRectContainsPoint(startRect, touchLocation);
    
    [UIView animateWithDuration:0.7 
                          delay:0.0 
                        options:UIViewAnimationCurveEaseOut 
                     animations:^{cup.transform = CGAffineTransformMakeScale(1.25, 0.75);} 
                     completion:^(BOOL finished) {  
                         [UIView animateWithDuration:2.0 
                                               delay:2.0 
                                             options:0
                                          animations:^{cup.alpha = 0.0;} 
                                          completion:^(BOOL finished) {
                                              [cup removeFromSuperview];
                                              cup = nil;}];                 
                     }];
    
    }
    

    UITapGestureRecognizer *touchOnView = [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(releaseButAction)] autorelease];
    
    // Set required taps and number of touches
    [touchOnView setNumberOfTapsRequired:1];
    [touchOnView setNumberOfTouchesRequired:1];
    
    // Add the gesture to the view
    [[self view] addGestureRecognizer:touchOnView];
    
        3
  •  5
  •   Brock Adams    14 年前

    创建自己的自定义 UIView 从继承创建的子类 重写子类中的以下方法,

    (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    
    }
    
        4
  •  3
  •   Cœur Gustavo Armenta    6 年前

    使用手势识别器,很容易捕捉触摸事件。

    enter image description here

    例如,将轻触手势识别器拖到视图中。它不会显示在视图UI中。您可以在故事板左侧面板的文档大纲中找到它:

    enter image description here

    最后一步是(通过控制拖动)将其作为代理链接到监视触摸的视图,并将控制拖动作为操作链接到视图控制器类。这是链接连接的图片。

    enter image description here

    以及行动代码:

    @IBAction func tapAction(_ sender: Any) {
        // touch is captured here.
    }
    
        5
  •  2
  •   Zoeb S    11 年前

    与其使用手势捕获触摸,我建议将UIView父类更改为UIControl,以便它可以处理touchUpInside事件

    发件人:

    @interface MyView : UIView
    

    @interface MyView : UIControl
    

    然后为视图添加这一行-

        [self addTarget:self action:@selector(viewTapped:) forControlEvents:UIControlEventTouchUpInside]; 
    

    因为手势可能会在滚动时产生问题。

        6
  •  1
  •   DazChong    10 年前

    UIView 到 UIControl 在InterfaceBuilderIdentityInspector中初始化,然后ConnectionsInspector将立即为挂钩准备好触摸事件。

    零代码更改。