美文网首页
iOS 定时器01

iOS 定时器01

作者: Smallwolf_JS | 来源:发表于2018-12-24 22:34 被阅读4次
@interface MLProxy ()
@property (nonatomic, weak) id target;
@end

@implementation MLProxy

+ (instancetype)createProxyWeithTarget:(id)target{
   MLProxy *proxy = [self alloc];
    proxy.target = target;
    return proxy;
}

- (id)forwardingTargetForSelector:(SEL)aSelector{
    return self.target;
}

@end

- (void)viewDidLoad {
    [super viewDidLoad];
    self.proxy = [MLProxy createProxyWeithTarget:self];
    // NSTimer
    // 由于iOS的消息机制(objc_msgSend()), 系统会对self.proxy 发送一个scheduledTimerWithTimeInterval的消息由于MLProxyb并没有实现该方法,就会执行Runtime的消息转发机制。
    self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self.proxy selector:@selector(timerAction) userInfo:nil repeats:YES];
}

自己创建的中间项
.h

#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@interface XDProxy : NSProxy
/**
 *  代理的对象
 */
@property (nonatomic,weak)id obj;
@end

NS_ASSUME_NONNULL_END

.m

#import "XDProxy.h"

@implementation XDProxy

- (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector{
    NSMethodSignature *sig = nil;
    sig = [self.obj methodSignatureForSelector:aSelector];
    return sig;
}

/**
 *  NSInvocation封装了NSMethodSignature,通过invokeWithTarget方法将消息转发给其他对象.这里转发给控制器执行。
 */
- (void)forwardInvocation:(NSInvocation *)anInvocation{
    [anInvocation invokeWithTarget:self.obj];
}

@end

相关文章

  • 无标题文章

    iOS NSTimer使用详解-开启、关闭、移除 定时器定时器详解ios定时器关闭定时器NSTimer 1、要使用...

  • iOS进阶-谈谈定时器

    目录 iOS提供定时器API 定时器开发中的坑 一、 iOS提供定时器API 二、定时器开发中的坑 2.1、必须办...

  • 定时器

    定时器 兼容ios

  • iOS中的定时器

    点击这里>> cocoaChina: iOS中的定时器 iOS中定时器有三种,分别是NSTimer、CADispl...

  • GCD定时器使用

    iOS中的常用定时器分为这几类: NSTimer CADisplayLink GCD定时器 选择GCD定时器原因:...

  • iOS:NSTimer的几种创建方式

    在iOS开发中,经常会用到定时器,iOS中常用的定时器有三种:NSTimer,GCD,CADisplayLink。...

  • iOS 定时器01

    自己创建的中间项.h .m

  • iOS Timer

    iOS开发中定时器经常会用到,iOS中常用的定时器有三种,分别是NSTime,CADisplayLink和GCD。...

  • 每日笔记

    1、通过safari打开网页 2、iOS的几种定时器及区别 iOS的几种定时器及区别 3、long long类型 ...

  • iOS三大定时器

    iOS开发中定时器经常会用到,iOS中常用的定时器有三种,分别是NSTime,CADisplayLink和GCD。...

网友评论

      本文标题:iOS 定时器01

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