总结

2018-12-17  本文已影响0人  hxxxs

A,B,C三个类,已知A是B的基类,C是A的分类,现在三类中都实现了同一个方法,问A,B类实例后的对象会分别执行哪个类中的方法,为什么?

#import "Person.h"
#import <objc/runtime.h>

@implementation Person

+ (void)load {
    NSLog(@"person load");
    
    unsigned int outCount = 0;
    
    Method *methodList = class_copyMethodList(self.class, &outCount);
    
    for (int i = 0; i < outCount; i++) {
        Method method = methodList[i];
        NSLog(@"SEL = %@", NSStringFromSelector(method_getName(method)));
        NSLog(@"IMP = %p", method_getImplementation(method));
    }
    
    NSLog(@"---------");
}

- (void)run {
    NSLog(@"person %@ %p", NSStringFromSelector(_cmd), class_getMethodImplementation(self.class, _cmd));
}

@end

#import "Student.h"
#import <objc/runtime.h>

@implementation Student

+ (void)load {
    NSLog(@"student load");
    
    unsigned int outCount = 0;
    
    Method *methodList = class_copyMethodList(self.class, &outCount);
    
    for (int i = 0; i < outCount; i++) {
        Method method = methodList[i];
        NSLog(@"SEL = %@", NSStringFromSelector(method_getName(method)));
        NSLog(@"IMP = %p", method_getImplementation(method));
    }
    
    NSLog(@"---------");
}

- (void)run {
    NSLog(@"student run");
}

@end

#import "Person+Add.h"
#import <objc/runtime.h>

@implementation Person (Add)

- (void)run {
    NSLog(@"category %@ %p",NSStringFromSelector(_cmd), class_getMethodImplementation(self.class, _cmd));
}

@end

已知两个视图分别为区域A、B,重叠区域为C,如何实现点击区域C响应A区域对应视图中的监听方法,且B区域对应视图监听方法在除C区域中都可以正常响应

WX20181218-191335.png

重写B视图中- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event方法,因为该方法会递归调用- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event方法进行判断点击视图是否可以响应

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
    //  设置重叠区域frame
    CGRect frame = CGRectMake(0, 0, 100, 100);
    //  判断点击位置是否包含在目标区域
    if (CGRectContainsPoint(frame, point)) {
        return nil;
    }
    return [super hitTest:point withEvent:event];
}

- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event {
    BOOL value = [super pointInside:point withEvent:event];
    NSLog(@"%d", value);
    return value;
}

仅作为个人学习使用,侵权请告知

上一篇下一篇

猜你喜欢

热点阅读