• 源码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
  • 相关阅读:
    spring scope prototype与singleton区别
    JAVA中的内存们
    汉诺塔问题
    关于设置tomcat端口为80的事
    struts2表单提单细节处理
    volatile的作用
    Java并发编程:Thread类的使用
    git的几个小技巧
    UEdit插件使用
    MySQL优化必须调整的10项配置
  • 原文地址:https://www.cnblogs.com/laugh/p/6587365.html
Copyright © 2020-2023  润新知