【问题标题】:How to make 'touchesBegan' method work for a specific view?如何使“touchesBegan”方法适用于特定视图?
【发布时间】:2013-07-04 10:26:09
【问题描述】:

在我的添加联系人页面中,我有一个视图和一个滚动视图,还有一个视图。在最后一个视图中,我有文本框等。我给出了“touchesBegan”方法,但它只在底部的视图中调用。如何将该方法指向另一个视图,即顶部的视图?

-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
 [self.AddView endEditing:YES];
}   

【问题讨论】:

    标签: iphone ios uiview touchesbegan


    【解决方案1】:

    这是你可以做到的一种方式:

    -(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    {
        UITouch *touch= [touches anyObject];
        if ([touch view] == image1)
        {
            //Action
        }
    
    }
    

    请注意:当您使用 UIScrollView 时,您可能无法获得 UIScrollView 的 touches 方法。在这种情况下,您可能必须使用 UIGesture。

    【讨论】:

    • 点赞表示它不适用于UIScrollView :)
    • 是的,这就像对问题更深一步:)
    【解决方案2】:

    这是 Swift 中的答案:

    override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
        let touch = touches.first as! UITouch
        if(touch.view == myView){
            // Code
        }
    }
    

    【讨论】:

    • touches.first是什么意思
    • @iOSDeveloper 这是用户第一次开始触摸屏幕的地方。当用户在屏幕上拖动手指时,可能会有多个这样的触摸事件。最后一个是 touches.last。
    【解决方案3】:

    首先检查整个控件的属性UserInteractionEnabled并设置为YES

    检查您的底视图框架后,该视图尚未结束

    在你可以用下面的条件检查它并用特定的控件触摸事件做一些事情之后..

    -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    {
        UITouch *touch = [touches anyObject];
        [touch locationInView:viewBoard];
        if([touch.view isKindOfClass:[UIImageView class]])
        {
           UIImageView *tempImage=(UIImageView *) touch.view;
           if (tempImage.tag  == yourImageTag) 
           {
              /// write your code here
           }
         }
    }
    

    【讨论】:

      【解决方案4】:

      试试这个:

        - (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
              UITouch *touch1 = [touches anyObject];
              CGPoint touchLocation = [touch1 locationInView:self.finalScore];
      
               if(CGRectContainsPoint(YourView.frame, touchLocation));
               {
                  //Do stuff.
               }
      
      
        }
      

      【讨论】:

        【解决方案5】:

        试试

        -(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
        {
            UITouch *touch = [[event touchesForView:vTouch] anyObject];
            if(!touch)
                return;
        
            CGPoint pointNow = [touch locationInView:otherView];
            {
                // code here
            }
        }
        

        【讨论】:

          【解决方案6】:

          如果您的视图是父视图,则可以使用以下代码:

          if let touch = touches.first {
                  let position = touch.location(in: yourView)
          
          
                  let pnt: CGPoint = CGPoint(x: position.x, y: position.y)
          
                  if (yourView.bounds.contains(pnt)) {
                     //You can use: yourView.frame.contains(pnt)
          
          
                      //OK.
          
          
                  }
              }
          

          【讨论】:

            【解决方案7】:

            Referring to the Apple documentation on UIResponder,所有的UIView对象(包括UIWindow)、UIApplication对象、UIViewController对象都是UIResponder的实例。要处理特定类型的事件,响应者必须重写相应的方法。

            在我们的例子中,touches 是我们的事件类型。所以我们的响应者应该实现以下方法。 touchesBegan(:with:), touchesMoved(:with:), touchesEnded(:with:) 和 touchesCancelled(:with:)

            由于我们只想知道用户何时触摸了特定视图,因此我们只需要实现touchesBegan(_:with:)。由于我们没有覆盖其他方法,我们必须调用super.touchesBegan(touches, with: event)。如果我们覆盖 ALL 的其他方法,则不需要调用 super

            touchesBegan(_:with:)参数touches是一组UITouch实例。每个实例代表事件开始阶段的触摸,由参数event 表示。

            对于视图中的触摸,该集合默认仅包含一次触摸。因此touches.first 是集合中唯一的 UITouch 实例。然后我们访问属性view,它表示发生触摸的视图或窗口。最后,我们将已触摸的视图与您想要的视图进行比较。

            应该注意,如果您希望接收多次触摸,您必须将视图的isMultipleTouchEnabled 属性设置为true。然后touches 的集合将有多个 UITouch 实例,您必须相应地处理它。

                override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
                    super.touchesBegan(touches, with: event)
            
                    if let touch = touches.first, touch.view == myView {
                        // Do something
                    }
                }
            

            【讨论】:

            • 虽然这段代码 sn-p 可以解决问题,including an explanation 确实有助于提高您的帖子质量。请记住,您是在为将来的读者回答问题,而这些人可能不知道您提出代码建议的原因。
            • @DangNguyen 感谢您的建议。我已经继续并给出了更详细的答案。
            猜你喜欢
            • 1970-01-01
            • 2023-03-03
            • 1970-01-01
            • 1970-01-01
            • 2021-11-09
            • 2018-08-21
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多