???????工作中總是會遇到需要按鈕的點擊范圍比實際的fram大的情況,原來一般都是在上邊添加一個范圍更大的按鈕,這樣確實能夠實現效果,但是每次都這樣寫會很low。
???????最近細細研究這塊終于發現可以重寫view的
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event;
的這個方法就可以實現。其實這個方法就是傳個你點擊的點 然后你去判斷這個點是否在視圖上。
下邊直接貼上自己寫的uiview的拓展類
@interface UIView (ChangeScope)
- (void)changeViewScope:(UIEdgeInsets)changeInsets;
@end
#import "UIView+ChangeScope.h"
#import <objc/runtime.h>
@implementation UIView (ChangeScope)
static char *changeScopeKey;
- (void)setChangeScope:(NSString *)changeScope
{
objc_setAssociatedObject(self, &changeScopeKey, changeScope, OBJC_ASSOCIATION_COPY_NONATOMIC);
}
- (NSString *)changeScope
{
return objc_getAssociatedObject(self, &changeScopeKey);
}
- (void)changeViewScope:(UIEdgeInsets)changeInsets
{
self.changeScope = NSStringFromUIEdgeInsets(changeInsets);
}
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
{
UIEdgeInsets changeInsets = UIEdgeInsetsFromString(self.changeScope);
if (changeInsets.left != 0 || changeInsets.top != 0 || changeInsets.right != 0 || changeInsets.bottom != 0) {
CGRect myBounds = self.bounds;
myBounds.origin.x = myBounds.origin.x + changeInsets.left;
myBounds.origin.y = myBounds.origin.y + changeInsets.top;
myBounds.size.width = myBounds.size.width - changeInsets.left - changeInsets.right;
myBounds.size.height = myBounds.size.height - changeInsets.top - changeInsets.bottom;
return CGRectContainsPoint(myBounds, point);
} else {
return CGRectContainsPoint(self.bounds,point);
}
}
@end