美文网首页
iOS 扩大button的响应范围

iOS 扩大button的响应范围

作者: YSH_110 | 来源:发表于2021-04-17 10:44 被阅读0次
    • 最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
    

    相关文章

      网友评论

          本文标题:iOS 扩大button的响应范围

          本文链接:https://www.haomeiwen.com/subject/qhvxuctx.html