【问题标题】:How to determine if UIView is currently being touched?如何确定当前是否正在触摸 UIView?
【发布时间】:2012-02-28 07:35:29
【问题描述】:

有没有一种方法可以查询 UIView 以确定它当前是否被触摸?我问的原因是因为我使用“touchedBegan”、“touchesEnded”和“touchesMoved”来保持用户手指下的 UIView。但是,如果用户非常快地移动他/她的手指并设法“逃脱”了我想删除该窗口的窗口。
我在想我可以使用计时器来定期测试每个视图以确定它当前是否被触摸,如果不是,我将删除它。 “IsTouchedProperty”将是完美的。 有什么想法吗?

【问题讨论】:

    标签: uiview touch


    【解决方案1】:

    我遇到了类似的问题,并通过使用 tracking 属性解决了它。来自文档:

    一个布尔值,指示接收者当前是否在 跟踪与事件相关的触摸。 (只读)

    这是 UIControl 上的一个方法,但你不是要创建一个吗?

    您还可以hitTest 观看。检查the apple docs

    【讨论】:

    • 请注意,这并不完全正确。 isTouchInside 和 isTracking 不是 UIView 的一部分。他们来自 UIControl
    • isTracking 也是 UIScrollView 的一部分
    【解决方案2】:

    由于 UIView 不继承自 UIControl,您需要继承 UIView 并使用触摸事件滚动您自己的 isTouching 属性。比如:

    // MyView.h
    @interface MyView : UIView
    @property (nonatomic, assign) BOOL isTouching;
    @end
    
    
    // MyView.m
    ...
    
    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
        [super touchesBegan:touches withEvent:event];
        self.isTouching = YES;
    }
    
    - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
        [super touchesCancelled:touches withEvent:event];
        self.isTouching = NO;
    }
    
    - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
        [super touchesEnded:touches withEvent:event];
        self.isTouching = NO;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-11
      • 1970-01-01
      • 2016-04-24
      • 1970-01-01
      相关资源
      最近更新 更多