views:

24

answers:

1

Hey all, can anyone please explain how I can subclass UIButton and override some method so that when the user drags off a button it comes up right away? The problem is that when I drag out of the button frame it remains active and down. I want it to stop as soon as the finger leaves the button frame. Any ideas?

(Cocoa Touch)

A: 

If anyone ever has this problem, the following code allows for extremely accurate edge sensing while dragging. If you drag out of the button, it wont extend past the button's edge like normal.

(I subclassed UIButton and made the following:)

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    touchBlocker = TRUE;
    self.highlighted = TRUE;
    NSLog(@"Touch Began");
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [[event allTouches] anyObject];
    CGPoint location = [touch locationInView:self];
    if (touchBlocker) {
        if (!CGRectContainsPoint([self bounds], location)) {
            touchBlocker =FALSE;
            self.highlighted = FALSE;
            NSLog(@"Touch Exit");
        }   
    } else if (CGRectContainsPoint([self bounds], location)) {
        touchBlocker = TRUE;
        self.highlighted = TRUE;
        NSLog(@"Touch Enter");
    }

}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    touchBlocker = FALSE;
    self.highlighted = FALSE;
    NSLog(@"Touch Ended");
}
XenElement