PleaseHelp
PleaseHelp

Reputation: 315

How to determine if UIView is currently being touched?

Is there a way I can query a UIView to determine if it currently being touched? The reason I ask is because I am using "touchedBegan", "touchesEnded", and "touchesMoved" to keep a UIView under a user's finger. However, if the user moves his/her finger really fast and manages to "escape" the window I want to remove that window.
I was thinking I could use a timer to periodically test each view to determine if it is currently being touched, if it is not, I will remove it. An "IsTouchedProperty" would be perfect. Any thoughts?

Upvotes: 2

Views: 1303

Answers (2)

taber
taber

Reputation: 3240

Since UIView doesn't inherit from UIControl you would need to subclass UIView and roll your own isTouching property using touch events. Something like:

// 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;
}

Upvotes: 3

Tom
Tom

Reputation: 2714

I had a similar problem and solved it by using the tracking property. From the documentation:

A Boolean value that indicates whether the receiver is currently tracking touches related to an event. (read-only)

This is a method on UIControl, but aren't you trying to create one?

You can also hitTest views. Check the apple docs

Upvotes: 4

Related Questions