擴展UIButton的響應區域
引言
通常在iOS開發中通常會遇到產品說按鈕的響應區域不大 而UI給我們的設計是按鈕的面積 而不是按鈕的響應面積
所以在這種情況下需要我們自己去擴展按鈕的響應區域
思考:這時候讓我想起了 響應者鏈條
解決方案:
重寫一個Button類,這個button類繼承與UIButton,
重寫- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent*)event, 作用:判斷下傳入過來的點在不在方法調用者的坐標系上
直接上代碼
#import "JHCustomButton.h" @implementation JHCustomButton // 作用:判斷下傳入過來的點在不在方法調用者的坐標系上 - (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event{ CGRect bounds =CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height); //寬高希望擴展的范圍 CGFloat widthDelta =30; CGFloat heightDelta =20; //相當於bounds 上下左右都增加了10的額外 bounds =CGRectInset(bounds, -0.5*widthDelta, -0.5* heightDelta);//注意這里是負數,擴大了之前的bounds的范圍 //點擊的點是否在這個范圍 return CGRectContainsPoint(bounds, point); } @end
這樣就實現了只擴大了按鈕的響應區域