zoukankan      html  css  js  c++  java
  • CADisplayLink使用中的循环引用问题的解决

    解决循环引用的问题,参考学习了ibireme大神的YYFPSLabel的实现,整理以备用
    
    
        // 如果直接用 self 或者 weakSelf,都不能解决循环引用问题
        _link = [CADisplayLink displayLinkWithTarget:[YYWeakProxy proxyWithTarget:self] selector:@selector(tick:)];
    //    _link = [CADisplayLink displayLinkWithTarget:self selector:@selector(tick:)];
    //    __weak typeof(self) weakSelf = self;
    //    _link = [CADisplayLink displayLinkWithTarget:weakSelf selector:@selector(tick:)];
        [_link addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSRunLoopCommonModes];
                                            //NSDefaultRunLoopMode - 标准优先级
                                            //NSRunLoopCommonModes - 高优先级
                                            //UITrackingRunLoopMode - 用于UIScrollView和别的控件的动画
        return self;
    @interface YYWeakProxy : NSProxy
    
    /**
     The proxy target.
     */
    @property (nonatomic, weak, readonly) id target;
    
    /**
     Creates a new weak proxy for target.
     
     @param target Target object.
     
     @return A new proxy object.
     */
    - (instancetype)initWithTarget:(id)target;
    
    /**
     Creates a new weak proxy for target.
     
     @param target Target object.
     
     @return A new proxy object.
     */
    + (instancetype)proxyWithTarget:(id)target;
    
    @end
    
    @implementation YYWeakProxy
    
    - (instancetype)initWithTarget:(id)target {
        _target = target;
        return self;
    }
    
    + (instancetype)proxyWithTarget:(id)target {
        return [[YYWeakProxy alloc] initWithTarget:target];
    }
    
    - (id)forwardingTargetForSelector:(SEL)selector {
        return _target;
    }
    
    - (void)forwardInvocation:(NSInvocation *)invocation {
        void *null = NULL;
        [invocation setReturnValue:&null];
    }
    
    - (NSMethodSignature *)methodSignatureForSelector:(SEL)selector {
        return [NSObject instanceMethodSignatureForSelector:@selector(init)];
    }
    
    - (BOOL)respondsToSelector:(SEL)aSelector {
        return [_target respondsToSelector:aSelector];
    }
    
    - (BOOL)isEqual:(id)object {
        return [_target isEqual:object];
    }
    
    - (NSUInteger)hash {
        return [_target hash];
    }
    
    - (Class)superclass {
        return [_target superclass];
    }
    
    - (Class)class {
        return [_target class];
    }
    
    - (BOOL)isKindOfClass:(Class)aClass {
        return [_target isKindOfClass:aClass];
    }
    
    - (BOOL)isMemberOfClass:(Class)aClass {
        return [_target isMemberOfClass:aClass];
    }
    
    - (BOOL)conformsToProtocol:(Protocol *)aProtocol {
        return [_target conformsToProtocol:aProtocol];
    }
    
    - (BOOL)isProxy {
        return YES;
    }
    
    - (NSString *)description {
        return [_target description];
    }
    
    - (NSString *)debugDescription {
        return [_target debugDescription];
    }
    
    @end

    NSProxy( /BrE ˈprɒksi/)的父类不是NSObject

  • 相关阅读:
    Google Shell Style Guide
    50 Must-have plugins for extending Twitter Bootstrap
    HTTP 请求头中的 X-Forwarded-For
    如何让 PowerPoint 幻灯片「高大上」?
    数据挖掘系列(1)关联规则挖掘基本概念与Aprior算法
    关于大型网站技术演进的思考(三)--存储的瓶颈(3)
    基于 Nginx XSendfile + SpringMVC 进行文件下载
    如何成为全栈工程师?
    Sqlserver通过列名查表名
    animate
  • 原文地址:https://www.cnblogs.com/Jordandan/p/6201853.html
Copyright © 2011-2022 走看看