总结

作者: hxxxs | 来源:发表于2018-12-17 17:04 被阅读0次

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

  • A->a 对象会执行C中的方法实现
    在A类的methodLists中C类会插入一条相同名称不同实现的新方法(栈顶),在a对象执行方法的时候会从methodLists顶部取出对应名称的方法去实现

  • B->b对象会执行B中的方法实现
    因为B重写了A中的方法实现,相当于在B的方法列表中修改SEL对应的IMP地址

#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;
}

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

相关文章

网友评论

      本文标题:总结

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