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 <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
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容