By returning self
for the larger area, you can fool the hitTest
logic into thinking that the button is larger than it is. That should cause the button to trigger the action without the need to call sendActionsForControlEvents
in the hitTest
method.
hitTest:withEvent: is calling sendActionsForControlEvents twice
Question
I have a simple UIButton subclass and I just want to extend the touch area of the button without actually increasing the frame (this is because I want the highlight and selected backgrounds to be the size of the original frame).
Here is what I have added to my UIButton subclass:
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
int errorMargin = 20;
CGRect largerFrame = CGRectMake(0 - (errorMargin / 2), 0 - (errorMargin / 2), self.frame.size.width + errorMargin, self.frame.size.height + errorMargin);
if ((CGRectContainsPoint(largerFrame, point)) == 1){
NSLog(@"Sending Action");
[self sendActionsForControlEvents:UIControlEventTouchUpInside];
return self;
}
else{
return nil;
}
}
This works great; however, it is calling the required action twice. So for instance a "tab" button will be tapped once, but tab over twice.
I have found a few questions related to a UIControl and the beganTracking method, but I can't get it to work with a UIButton.
Any ideas on how to properly implement this with a UIButton? Thanks!
La solution
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow