zoukankan      html  css  js  c++  java
  • 源码0503-02-掌握-RunLoop实践

    //  ViewController.m
    //  02-掌握-RunLoop实践
    #import "ViewController.h"
    #import "XMGThread.h"
    
    @interface ViewController ()
    @property (weak, nonatomic) IBOutlet UIImageView *imageView;
    /** 线程对象 */
    @property (nonatomic, strong) XMGThread *thread;
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        
        self.thread = [[XMGThread alloc] initWithTarget:self selector:@selector(run) object:nil];
        [self.thread start];
    }
    
    - (void)run
    {
        NSLog(@"----------run----%@", [NSThread currentThread]);
        
        while (1) {
            [[NSRunLoop currentRunLoop] run];
        }
        
        NSLog(@"---------");
    }
    
    /**
     * 这种方式虽然能保住线程的命,但是这条线程就无法处理其他行为(事件)
     */
    //- (void)run
    //{
    //    NSLog(@"----------run----%@", [NSThread currentThread]);
    //    
    //    while (1); // 当前线程永远在处理这行代码
    //    
    //    NSLog(@"---------");
    //}
    
    //- (void)run
    //{
    //    NSLog(@"----------run----%@", [NSThread currentThread]);
    //    
    //    [[NSRunLoop currentRunLoop] addPort:[NSPort port] forMode:NSDefaultRunLoopMode];
    //    [[NSRunLoop currentRunLoop] run];
    //    
    //    NSLog(@"---------");
    ////    [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
    ////    [[NSRunLoop currentRunLoop] runUntilDate:[NSDate distantFuture]];
    //}
    ???????????????
    - (void)test
    {
        NSLog(@"----------test----%@", [NSThread currentThread]);
    }
    
    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    {
        [self performSelector:@selector(test) onThread:self.thread withObject:nil waitUntilDone:NO];
    }
    
    - (void)useImageView
    {
        // 只在NSDefaultRunLoopMode模式下显示图片
        [self.imageView performSelector:@selector(setImage:) withObject:[UIImage imageNamed:@"placeholder"] afterDelay:3.0 inModes:@[NSDefaultRunLoopMode]];
    }
    
    @end

    自定义线程对象

    //  XMGThread.h
    //  02-掌握-RunLoop实践
    #import <Foundation/Foundation.h>
    
    @interface XMGThread : NSThread
    
    @end
    //  XMGThread.m
    //  02-掌握-RunLoop实践
    #import "XMGThread.h"
    
    @implementation XMGThread
    - (void)dealloc
    {
        NSLog(@"%@ --- dealloc", self);
    }
    @end

    02-掌握-RunLoop实践

    //  ViewController.m
    //  02-掌握-RunLoop实践
    #import "ViewController.h"
    #import "XMGThread.h"
    
    @interface ViewController ()
    @property (weak, nonatomic) IBOutlet UIImageView *imageView;
    /** 线程对象 */
    @property (nonatomic, strong) XMGThread *thread;
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        
        self.thread = [[XMGThread alloc] initWithTarget:self selector:@selector(execute) object:nil];
        [self.thread start];
    }
    
    - (void)execute
    {
        while (1) {
            [[NSRunLoop currentRunLoop] run];
        }
    }
    
    //- (void)execute
    //{
    //    [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(test) userInfo:nil repeats:YES];
    //    
    //    [[NSRunLoop currentRunLoop] run];
    //}
    
    //- (void)execute
    //{
    //    NSLog(@"----------run----%@", [NSThread currentThread]);
    //    
    //    while (1) {
    //        [[NSRunLoop currentRunLoop] run];
    //        
    //        NSLog(@"--------2222");
    //    }
    //    
    //    NSLog(@"---------");
    //}
    
    //- (void)execute
    //{
    //    NSLog(@"----------run----%@", [NSThread currentThread]);
    //    
    //    [[NSRunLoop currentRunLoop] addPort:[NSPort port] forMode:NSDefaultRunLoopMode];
    //    [[NSRunLoop currentRunLoop] run];
    //
    //    
    //    NSLog(@"---------");
    //}
    
    - (void)test
    {
        NSLog(@"----------test----%@", [NSThread currentThread]);
    }
    
    - (void)test2
    {
        NSLog(@"***********test2*******%@", [NSThread currentThread]);
        
        NSLog(@"%@", [NSRunLoop currentRunLoop]);
    }
    
    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    {
        [self performSelector:@selector(test2) onThread:self.thread withObject:nil waitUntilDone:NO];
    }
    
    @end
    本人无商业用途,仅仅是学习做个笔记,特别鸣谢小马哥,学习了IOS,另日语学习内容有需要文本和音频请关注公众号:riyuxuexishuji
  • 相关阅读:
    路由系统
    flask_sqlalchemy的使用
    input()输入语句
    注释
    Python 2017.1.5
    关于object网页播放器参数的设置,推荐博客系列
    LRU缓存,大神写的,值得借鉴
    object,网页播放器的相关属性设置
    js的apply和call方法
    count()函数在count()中参数的讨论
  • 原文地址:https://www.cnblogs.com/laugh/p/6587365.html
Copyright © 2011-2022 走看看