1.在界面中难免会遇到UIImageView 太小了,不好点击所以要扩大它的点击范围。
我使用分类UIView+WJEnlargeArea实现:
这样可以将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