【问题标题】:how identify touch in a particular view如何在特定视图中识别触摸
【发布时间】:2011-03-04 10:53:16
【问题描述】:

如何接触特定视图。

我正在使用

CGPoint Location = [[touches anyObject] locationInView:self.view ];

但希望仅在单击特定子视图时触发操作。
如何做到这一点。

【问题讨论】:

    标签: iphone uitouch


    【解决方案1】:

    我自己得到了答案...但感谢其他帮助我的人

    这里是

    UITouch *touch ;
    touch = [[event allTouches] anyObject];
    
    
        if ([touch view] == necessarySubView)
    {
    //Do what ever you want
    }
    

    【讨论】:

      【解决方案2】:

      试试这个

      //here enable the touch
      - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
          // get touch event
          UITouch *touch = [[event allTouches] anyObject];
          CGPoint touchLocation = [touch locationInView:self.view];
      
          if (CGRectContainsPoint(yoursubview_Name.frame, touchLocation)) {
              //Your logic
              NSLog(@" touched");
          }
      }
      

      【讨论】:

        【解决方案3】:

        您应该创建一个 UIView 的子类(或创建一个类别)并覆盖

        - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
        

        将消息重定向到适当的委托。

        【讨论】:

        • 不,你不需要这样做...检查我自己几分钟前找到的答案...
        • 我好像误解了你的问题。
        • +1 ..很好的替代品,如果要在子视图上完成大量工作,谢谢
        【解决方案4】:
        // here Tiles is a separate class inherited from UIView Class
        - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
            UITouch *touch = [touches anyObject];
            if ([[touch view] isKindOfClass:[Tiles class]]) {
                NSLog(@"[touch view].tag = %d", [touch view].tag);
            }
        }
        

        这样你可以找到视图或子视图被触摸

        【讨论】:

          【解决方案5】:

          这是一个没有多点触控的 swift3 版本:

          override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
              if let touch = touches.first, self.bounds.contains(touch.location(in: self)) {
                  // Your code
              }
          }
          

          【讨论】:

            【解决方案6】:

            你试过了吗

            CGPoint Location = [[touches anyObject] locationInView:necessarySubView ];
            

            【讨论】:

              猜你喜欢
              • 2011-11-21
              • 2018-09-27
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多