iOS 增加button的点击范围

 

继承或者扩展button。重写下面两个方法的其中之一就行

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event

{

    //设置点击范围

    CGRect rectBig = CGRectMake(self.bounds.origin.x-20, self.bounds.origin.y-20, self.bounds.size.width+60, self.bounds.size.height+60);

    if (CGRectContainsPoint(rectBig, point)) {

        return self;

    }else{

        return nil;

    }

 

    return self;

}

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

{

  CGRect rectBig = CGRectMake(self.bounds.origin.x, self.bounds.origin.y-20, self.bounds.size.width+100, self.bounds.size.height+200);

    return CGRectContainsPoint(rectBig, point);

}

扩大某一view上的某一个button的点击范围,重写view的hit方法

-(UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event{

CGRect frame = CGRectMake(self.operationButton.frame.origin.x-40,self.operationButton.frame.origin.y-40 ,self.operationButton.bounds.size.width+80 , self.operationButton.bounds.size.height+80);

if (CGRectContainsPoint(frame, point)) {

return self.operationButton;

}

return nil;

}

你可能感兴趣的:(iOS)