iOS如何修改view的响应范围

       工作中总是会遇到需要按钮的点击范围比实际的fram大的情况,原来一般都是在上边添加一个范围更大的按钮,这样确实能够实现效果,但是每次都这样写会很low。
       最近细细研究这块终于发现可以重写view的

- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event;

的这个方法就可以实现。其实这个方法就是传个你点击的点 然后你去判断这个点是否在视图上。

下边直接贴上自己写的uiview的拓展类

@interface UIView (ChangeScope)

- (void)changeViewScope:(UIEdgeInsets)changeInsets;

@end

#import "UIView+ChangeScope.h"

#import

@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

你可能感兴趣的:(iOS如何修改view的响应范围)