zoukankan      html  css  js  c++  java
  • iOS Coding项目片段记录(四)

    iOS-检测UI主线程小工具(转)

    在iOS开发中需要保证所有UI操作一定是在主线程进行,通过 hook UIView的-setNeedsLayout,-setNeedsDisplay,-setNeedsDisplayInRect三个方法,确保它们都是在主线程执行。

    #import "UIView+NBUIKitMainThreadGuard.h"
    #import <objc/runtime.h>
    
    static inline void swizzling_exchangeMethod(Class clazz ,SEL originalSelector, SEL swizzledSelector){
        Method originalMethod = class_getInstanceMethod(clazz, originalSelector);
        Method swizzledMethod = class_getInstanceMethod(clazz, swizzledSelector);
    
        BOOL success = class_addMethod(clazz, originalSelector, method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod));
        if (success) {
            class_replaceMethod(clazz, swizzledSelector, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod));
        } else {
            method_exchangeImplementations(originalMethod, swizzledMethod);
        }
    }
    
    @implementation UIView (NBUIKitMainThreadGuard)
    
    +(void)load
    {
        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{
    
            SEL needsLayoutOriginalSelector = @selector(setNeedsLayout);
            SEL needsLayoutSwizzleSelector  = @selector(guard_setNeedsLayout);
            swizzling_exchangeMethod(self, needsLayoutOriginalSelector,needsLayoutSwizzleSelector);
    
            SEL needsDisplaOriginalSelector = @selector(setNeedsDisplay);
            SEL needsDisplaSwizzleSelector  = @selector(guard_setNeedsDisplay);
            swizzling_exchangeMethod(self, needsDisplaOriginalSelector,needsDisplaSwizzleSelector);
    
            SEL needsDisplayInRectOriginalSelector = @selector(setNeedsDisplayInRect:);
            SEL needsDisplayInRectSwizzleSelector  = @selector(guard_setNeedsDisplayInRect:);
            swizzling_exchangeMethod(self, needsDisplayInRectOriginalSelector,needsDisplayInRectSwizzleSelector);
    
        });
    }
    
    - (void)guard_setNeedsLayout
    {
        [self UIMainThreadCheck];
        [self guard_setNeedsLayout];
    }
    
    - (void)guard_setNeedsDisplay
    {
        [self UIMainThreadCheck];
        [self guard_setNeedsDisplay];
    }
    
    
    - (void)guard_setNeedsDisplayInRect:(CGRect)rect
    {
        [self UIMainThreadCheck];
        [self guard_setNeedsDisplayInRect:rect];
    }
    
    - (void)UIMainThreadCheck
    {
        NSString *desc = [NSString stringWithFormat:@"%@", self.class];
        NSAssert([NSThread isMainThread], desc);
    }
    
    @end
  • 相关阅读:
    Remove Element
    C++ 一些STL
    Two Pointers/hash/3Sum/4Sum类题目
    动态规划
    UVa 12657 双向链表
    并行运行环境
    多线程编程
    HTML XML CSS JS 迅速学习
    UVa 11988 数组模拟链表
    静态链表
  • 原文地址:https://www.cnblogs.com/741162830qq/p/6030852.html
Copyright © 2011-2022 走看看