觸摸事件處理


//- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
//{
//    return [super hitTest:point withEvent:event];
//}

/**
 告訴系統(tǒng):觸摸點(diǎn)point是否在這個(gè)UI控件身上
 如果該對(duì)象返回NO,那么不會(huì)調(diào)用該對(duì)象的上面的方法hitTest方法
 */
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
{
    // 初始化矩形框
    [self setupSpecialRects];

    // 根據(jù)觸摸點(diǎn)獲得被觸摸的特殊字符串
    LZSpecial *special = [self touchingSpecialWithPoint:point];

    if (special) {
        return YES;
    } else {
        return NO;
    }
}

// 觸摸事件的處理
// 1.判斷觸摸點(diǎn)在誰(shuí)身上: 調(diào)用所有UI控件的- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
// 2.pointInside返回YES的控件就是觸摸點(diǎn)所在的UI控件
// 3.由觸摸點(diǎn)所在的UI控件選出處理事件的UI控件: 調(diào)用- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event



// 把Main.storyboard里面的view,換成greenView
// LZGreenView.h
#import <UIKit/UIKit.h>

@interface LZGreenView : UIView

@end

// LZGreenView.m
#import "LZGreenView.h"
#import "LZRedView.h"

@interface LZGreenView()
@property (nonatomic, weak) UIButton *button;
@property (nonatomic, weak) LZRedView *redView;
@end

@implementation LZGreenView

- (void)awakeFromNib
{
    self.backgroundColor = [UIColor greenColor];

//    UIButton *button = [UIButton buttonWithType:UIButtonTypeContactAdd];
//    button.center = CGPointMake(100, 100);
//    [button addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];
//    [self addSubview:button];
//    self.button = button;

    LZRedView *redView = [[LZRedView alloc] init];
    redView.frame = CGRectMake(100, 100, 100, 100);
    [self addSubview:redView];
    self.redView = redView;
}

//- (void)click
//{
//    NSLog(@"點(diǎn)擊了按鈕");
//}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@"HMGreenView----touchesBegan");
}

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
    return self.redView;
}
@end


// LZRedView.h
#import <UIKit/UIKit.h>

@interface LZRedView : UIView

@end

// LZRedView.m
#import "LZRedView.h"

@implementation LZRedView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor redColor];
    }
    return self;
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@"LZRedView****----touchesBegan");
}
@end

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容