美文网首页
触摸事件处理

触摸事件处理

作者: Z了个L | 来源:发表于2016-11-13 10:13 被阅读15次
    
    //- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
    //{
    //    return [super hitTest:point withEvent:event];
    //}
    
    /**
     告诉系统:触摸点point是否在这个UI控件身上
     如果该对象返回NO,那么不会调用该对象的上面的方法hitTest方法
     */
    - (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
    {
        // 初始化矩形框
        [self setupSpecialRects];
    
        // 根据触摸点获得被触摸的特殊字符串
        LZSpecial *special = [self touchingSpecialWithPoint:point];
    
        if (special) {
            return YES;
        } else {
            return NO;
        }
    }
    
    // 触摸事件的处理
    // 1.判断触摸点在谁身上: 调用所有UI控件的- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
    // 2.pointInside返回YES的控件就是触摸点所在的UI控件
    // 3.由触摸点所在的UI控件选出处理事件的UI控件: 调用- (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(@"点击了按钮");
    //}
    
    - (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
    
    

    相关文章

      网友评论

          本文标题:触摸事件处理

          本文链接:https://www.haomeiwen.com/subject/svccpttx.html