1. 程式人生 > >子檢視A超出父檢視B範圍點選觸發子檢視A中的點選事件

子檢視A超出父檢視B範圍點選觸發子檢視A中的點選事件

Demo

在這裡插入圖片描述
解決示例
1、定義子檢視

// .h檔案
#import <UIKit/UIKit.h>

@interface ButtonView : UIView

@property (nonatomic, copy) void (^buttonClick)(UIButton *button);

@end

// .m檔案
#import "ButtonView.h"

@interface ButtonView ()

@property (nonatomic, strong) UIButton *subButton;

@end

@implementation ButtonView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setUI];
    }
    return self;
}

- (void)setUI
{
    //
    UIButton *button = [[UIButton alloc] initWithFrame:self.bounds];
    [self addSubview:button];
    button.backgroundColor = [UIColor greenColor];
    [button addTarget:self action:@selector(btn1Click:) forControlEvents:UIControlEventTouchUpInside];
    button.tag = 1;
    //
    self.subButton = [[UIButton alloc] initWithFrame:CGRectMake(100, -40, 80, 80)];
    [self addSubview:self.subButton];
    self.subButton.backgroundColor = [UIColor redColor];
    [self.subButton setTitle:@"click" forState:UIControlStateNormal];
    [self.subButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.subButton setTitleColor:[UIColor yellowColor] forState:UIControlStateHighlighted];
    [self.subButton addTarget:self action:@selector(btn2Click:) forControlEvents:UIControlEventTouchUpInside];
    self.subButton.layer.cornerRadius = 40;
    self.subButton.layer.masksToBounds = YES;
    self.subButton.tag = 2;
}

- (void)btn1Click:(UIButton *)button
{
    if (self.buttonClick) {
        self.buttonClick(button);
    }
}

- (void)btn2Click:(UIButton *)button
{
    if (self.buttonClick) {
        self.buttonClick(button);
    }
}

// 在view中重寫以下方法,其中self.button就是那個希望被觸發點選事件的按鈕
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
    UIView *view = [super hitTest:point withEvent:event];
    if (view == nil) {
        // 轉換座標系
        CGPoint newPoint = [self.subButton convertPoint:point fromView:self];
        // 判斷觸控點是否在button上
        if (CGRectContainsPoint(self.subButton.bounds, newPoint)) {
            view = self.subButton;
        }
    }
    return view;
}

@end

2、使用

ButtonView *buttonView = [[ButtonView alloc] initWithFrame:CGRectMake(20, 120, (self.view.frame.size.width - 40), 200)];
    [self.view addSubview:buttonView];
    buttonView.buttonClick = ^(UIButton *button) {
        NSString *message = [NSString stringWithFormat:@"點選了按鈕 %ld", button.tag];
        [[[UIAlertView alloc] initWithTitle:nil message:message delegate:nil cancelButtonTitle:nil otherButtonTitles:@"知道了", nil] show];
    };

效果圖
在這裡插入圖片描述

Untitled.gif
注意,主要是實現該方法

// 在view中重寫以下方法,其中self.button就是那個希望被觸發點選事件的按鈕
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
    UIView *view = [super hitTest:point withEvent:event];
    if (view == nil) {
        // 轉換座標系
        CGPoint newPoint = [self.subButton convertPoint:point fromView:self];
        // 判斷觸控點是否在button上
        if (CGRectContainsPoint(self.subButton.bounds, newPoint)) {
            view = self.subButton;
        }
    }
    return view;
}