iOS 扩大button的响应范围

  • 最low的办法,在button上加一个button
  • 重写view方法
    -(BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event;
    这个方法就是穿你点击的那个点,判断是否在view上

需要添加一个view或者button的拓展类

@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 扩大button的响应范围)