判斷點擊、觸摸區(qū)域是否屬于當前UIView

簡介

UIView提供了一個pointInside:withEvent:方法,用于判斷用戶點擊的點是否屬于當前這個視圖,其定義如下:

@interface UIView
// default returns YES if point is in bounds
- (BOOL)pointInside:(CGPoint)point withEvent:(nullable UIEvent *)event;
@end

用法示例

比如說美工給我們提供了一張圓形的底色透明的png圖片,如下所示:


?圓形透明圖片.png

現在要求點擊圖片上圓形部分可以觸發(fā)單擊事件,點擊圖片的其它區(qū)域不做任何反應,這里有2種方案可以實現:

方案1

把圖片做成UIButton,并設置UIButton的layer.cornerRadius為圓形的半徑:

UIImage *image = [UIImage imageNamed:@"圓形透明圖片.png"];

UIButton *btnView = [UIButton buttonWithType:UIButtonTypeCustom];
[btnView setImage:image forState:UIControlStateNormal];
btnView.frame = CGRectMake(0, 0, image.size.width, image.size.height);
// 設置UIButton為圓形,并且半徑與圖片半徑一致
btnView.layer.cornerRadius = image.size.width / 2.0;
btnView.clipsToBounds = YES;
[btnView addTarget:self action:@selector(buttonTapped) 
              forControlEvents:UIControlEventTouchUpInside];
方案2

pointInside:withEvent:來實現
先為UIButton定義一個擴展UIButton (Circle),用于設置圓形圖片半徑,并重寫pointInside:withEvent:方法

#import <UIKit/UIKit.h>
@interface UIButton (Circle)
// 設置圖片的圓角半徑
- (void)setCornerRadius:(CGFloat)cornerRadius;
@end


#import "UIButton+Circle.h"
#import "objc/runtime.h"
static char cornerRadiusKey;

@implementation UIButton (Circle)
- (void)setCornerRadius:(CGFloat)cornerRadius
{
    objc_setAssociatedObject(self, &cornerRadiusKey, [NSString stringWithFormat:@"%f", cornerRadius], OBJC_ASSOCIATION_COPY_NONATOMIC);
}
- (CGFloat)getCornerRadius
{
    NSString *str = objc_getAssociatedObject(self, &cornerRadiusKey);
    return (str && str.length) ? [str floatValue] : 0;
}
/**
 * 計算point點與center點的距離,
 * 如果 <= cornerRadius,則表示點擊了圖片的內容區(qū)域,視為有有效點擊
 * 如果 > cornerRadius, 則表示點擊了圖片的空白區(qū)域,視為無效點擊
 */
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
{
    CGPoint center = CGPointMake(self.bounds.size.width / 2.0, self.bounds.size.height / 2.0);
    CGFloat distance = sqrt(pow(point.x - center.x, 2) + pow(point.y - center.y, 2));
    return distance <= [self getCornerRadius];
}
@end

下面是使用方式,:

@implementation ViewController
- (void)viewDidLoad
{
    UIImage *image = [UIImage imageNamed:@"圓形透明圖片.png"];
    UIButton *btnView = [UIButton buttonWithType:UIButtonTypeCustom];
    [btnView setImage:image forState:UIControlStateNormal];
    btnView.frame = CGRectMake(100, 100, image.size.width, image.size.height);
    // 設置半徑
    [btnView setCornerRadius:image.size.width / 2.0];
    [btnView addTarget:self action:@selector(buttonTapped)
                  forControlEvents:UIControlEventTouchUpInside];
   
    [self.view addSubview:btnView];
}
- (void)buttonTapped
{
    NSLog(@"button tapped");
}
@end

運行后可以看到:

  • 當點擊了圖片內容區(qū)域,則會觸發(fā)buttonTapped方法
  • 當點擊了圖片的空白區(qū)域,沒有任何反應。

總結

方案1 ?方案2
優(yōu)點 代碼簡單,適用廣 處理比較靈活
缺點 有些特殊情況處理不了 稍顯復雜,適用于一些特殊情況
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • 好奇觸摸事件是如何從屏幕轉移到APP內的?困惑于Cell怎么突然不能點擊了?糾結于如何實現這個奇葩響應需求?亦或是...
    Lotheve閱讀 58,072評論 51 603
  • 在iOS開發(fā)中經常會涉及到觸摸事件。本想自己總結一下,但是遇到了這篇文章,感覺總結的已經很到位,特此轉載。作者:L...
    WQ_UESTC閱讀 6,133評論 4 26
  • -- iOS事件全面解析 概覽 iPhone的成功很大一部分得益于它多點觸摸的強大功能,喬布斯讓人們認識到手機其實...
    翹楚iOS9閱讀 3,008評論 0 13
  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 173,422評論 25 708
  • 《小飛俠彼得潘》: 一開始還以為是狼叔的那個彼得潘,原來不是……好吧,搞錯了。彼得潘也是被拍爛的一個題材,很適合小...
    老揚閱讀 781評論 0 1