This site is designed to take advantage of CSS. If you are seeing this, CSS must not be available or enabled in your browser. Everything should still work, but won't be as pretty. :)
Note: this is an updated post based on an older one, due to renaming the project (was "DSView") and moving it from Subversion to GitHub.
In Tweeps I have a button that I wanted to behave like the Delete button in a table view. You know, when you tap the delete toggle to the left of a cell, a red Delete button appears. And tapping anywhere other than that button will hide it without doing anything else:
I couldn't see any obvious way to do it, so asked on the iPhone Developer forums, and got a helpful reply suggesting a
UIWindow subclass, overriding
I tried implementing that, but what I really wanted was to override
-hitTest:withEvent:, since I wanted to block taps on views other than a specific button, and the documentation says one should always invoke the superclass of
Then I noticed that
-hitTest:withEvent: is actually defined in
UIView, and further experimenting with the table Delete feature showed that it appears to be implemented
UITableView, since the cancel tap behavior only occurs in the table, not the navigation bar or toolbar. Besides, implementing in a
UIView subclass is more focal, so a better choice.
So here is my
UIView subclass to do this. It uses a delegate approach, with a protocol to declare the method:
@protocol DejalViewDelegate <NSObject>
- (UIView *)view:(DejalView *)view hitTest:(CGPoint)point withEvent:(UIEvent *)event hitView:(UIView *)hitView;
And the actual subclass interface:
@interface DejalView : UIView
@property (nonatomic, weak) id <DejalViewDelegate> viewDelegate;
With the implementation just overriding the hit test method. It simply invokes the superclass then gives the delegate a chance to change it (or perform some other action) if it implements the delegate protocol method:
@synthesize viewDelegate = dejalViewDelegate;
Overrides this method to add support for the -view:hitTest:withEvent:hitView view delegate behavior.
Written by DJS 2009-09.
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event;
UIView *hitView = [super hitTest:point withEvent:event];
if ([self.viewDelegate respondsToSelector:@selector(view:hitTest:withEvent:hitView:)])
return [self.viewDelegate view:self hitTest:point withEvent:event
To use this, simply change a container
DejalView in the view hierarchy, then set the delegate property to your view controller (via code or IB):
self.view.viewDelegate = self;
Then implement the
-view:hitTest:withEvent:hitView: delegate method in your view controller, e.g. as follows — this will cause a tap on some special control (or if that control is hidden) to go through as normal, but tapping anywhere else in the view will hide the special control, without passing the tap on to whatever was actually tapped:
- (UIView *)view:(DSView *)view hitTest:(CGPoint)point
withEvent:(UIEvent *)event hitView:(UIView *)hitView;
if (someSpecialControl.hidden || hitView == someSpecialControl)
someSpecialControl.hidden = YES;
I hope this is useful to someone else too.
You can get the code and more information from the Dejal Open Source page.