1.在界面中難免會遇到UIImageView 太小了齐邦,不好點擊所以要擴大它的點擊范圍爬凑。
我使用分類UIView+WJEnlargeArea實現(xiàn):
Paste_Image.png
這樣可以將greenView的點擊范圍加大40.
import "UIView+WJEnlargeArea.h"
import <objc/runtime.h>
@implementation UIView (WJEnlargeArea)
static const char topNameKey;
static const char rightNameKey;
static const char bottomNameKey;
static const char leftNameKey;
- (void) setEnlargeEdge:(CGFloat) edge
{
[self setEnlargeEdgeWithTop:edge right:edge bottom:edge
left:edge];
} - (void) setEnlargeEdgeWithTop:(CGFloat) top right:(CGFloat) right bottom:(CGFloat) bottom left:(CGFloat) left
{
objc_setAssociatedObject(self, &topNameKey, [NSNumber numberWithFloat:top], OBJC_ASSOCIATION_COPY_NONATOMIC);
NSNumber* topEdge = objc_getAssociatedObject(self, &topNameKey);
objc_setAssociatedObject(self, &rightNameKey, [NSNumber numberWithFloat:right], OBJC_ASSOCIATION_COPY_NONATOMIC);
objc_setAssociatedObject(self, &bottomNameKey, [NSNumber numberWithFloat:bottom], OBJC_ASSOCIATION_COPY_NONATOMIC);
objc_setAssociatedObject(self, &leftNameKey, [NSNumber numberWithFloat:left], OBJC_ASSOCIATION_COPY_NONATOMIC);
} - (CGRect) enlargedRect
{
NSNumber* topEdge = objc_getAssociatedObject(self, &topNameKey);
NSNumber* rightEdge = objc_getAssociatedObject(self, &rightNameKey);
NSNumber* bottomEdge = objc_getAssociatedObject(self, &bottomNameKey);
NSNumber* leftEdge = objc_getAssociatedObject(self, &leftNameKey);
if (topEdge && rightEdge && bottomEdge && leftEdge)
{
return CGRectMake(self.bounds.origin.x - leftEdge.floatValue,
self.bounds.origin.y - topEdge.floatValue,
self.bounds.size.width + leftEdge.floatValue + rightEdge.floatValue,
self.bounds.size.height + topEdge.floatValue + bottomEdge.floatValue);
}
else
{
return self.bounds;
}
}
-(BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
{
CGRect rect = [self enlargedRect];
return CGRectContainsPoint(rect, point)?self:nil;
}
@end