I'm using few gesture recognizers on some views, but sometimes views are too small and it's hard to hit it. Using recognizers is necessary, so how can I enlarge hit area?
If you are doing this for a custom UIView
, you should be able to override the hitTest:withEvent:
method:
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
CGRect frame = CGRectInset(self.bounds, -20, -20);
return CGRectContainsPoint(frame, point) ? self : nil;
}
The above code will add a 20 point border around the view. Tapping anywhere in that area (or on the view itself) will indicate a hit.
Swift version of @rmaddy answer:
override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
let frame = self.bounds.insetBy(dx: -20, dy: -20);
return frame.contains(point) ? self : nil;
}
If you're using a UIImageView
as a button, you can use the following extension (Swift 3.0):
extension UIImageView {
open override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
if self.isHidden || !self.isUserInteractionEnabled || self.alpha < 0.01 { return nil }
let minimumHitArea = CGSize(width: 50, height: 50)
let buttonSize = self.bounds.size
let widthToAdd = max(minimumHitArea.width - buttonSize.width, 0)
let heightToAdd = max(minimumHitArea.height - buttonSize.height, 0)
let largerFrame = self.bounds.insetBy(dx: -widthToAdd / 2, dy: -heightToAdd / 2)
// perform hit test on larger frame
return (largerFrame.contains(point)) ? self : nil
}
}
Similar to the UIButton
extension here
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.