美文网首页
Swift 小结

Swift 小结

作者: 韩小醋 | 来源:发表于2017-11-24 15:51 被阅读12次

    Selector

    Selector("xxx") 习惯用来调用私有api,不做编译检查
    #selector(self.action(id:,id:)
    

    Extension

    extension Object {
        
        //用结构体创建关联key ,可以有效避免 命名空间的污染
        private struct AssociatedKeys {
            static var firstKey = "firstKey"
        }
        
        var firstProperty: Bool? {
            set {
                objc_setAssociatedObject(self, &AssociatedKeys.firstKey, NSNumber.init(value: newValue!), objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC)
            }
            get {
                return (objc_getAssociatedObject(self, &AssociatedKeys.firstKey) as? NSNumber)?.boolValue
            }
        }
    }
    
    extension String {
        
        func pinYin() -> String {
            let mutableString = NSMutableString.init(string: self)
            //转换成拉丁字符
            CFStringTransform(mutableString, nil, kCFStringTransformToLatin, false)
            //去除音标
            CFStringTransform(mutableString, nil, kCFStringTransformStripDiacritics, false)
            let string = String(mutableString)
            //去除空格
            return string.replacingOccurrences(of: " ", with: "")
        }
    }
    

    CoreText

    CTFrame
    CTLine
    CTRun: 是每一个相同属性字符串 ,但是不会隔行。

     // 获得要绘制区域的高度
    let framesetter = CTFramesetterCreateWithAttributedString(mutableStr)
    let restrictSize = CGSize.init(width: SCREEN_WIDTH, height: CGFloat.greatestFiniteMagnitude)
    let coreTextSize = CTFramesetterSuggestFrameSizeWithConstraints(framesetter, CFRangeMake(0, 0) , nil, restrictSize, nil)    
    

    Core Graphics

    绘制图片
    绘制文本
    混合图层绘制
    渐变绘制
    上下文变换

    linkView.png

    Runloop

        
        func addObserver() {
            let runloop = CFRunLoopGetCurrent()
            let runMode = CFRunLoopMode.defaultMode
            
            let observer = CFRunLoopObserverCreateWithHandler(kCFAllocatorDefault, CFRunLoopActivity.beforeWaiting.rawValue, true, 0) { (observer, _) in
                if (true) {//达到一定的条件就移除此监听
                    CFRunLoopRemoveObserver(runloop, observer, runMode)
                }
                self.perform(#selector(self.timeAction), on: Thread.main, with: nil, waitUntilDone: false, modes: [RunLoopMode.defaultRunLoopMode.rawValue])
            }
            CFRunLoopAddObserver(runloop, observer, runMode)
        }
        
        @objc func timeAction() {}
    

    相关文章

      网友评论

          本文标题:Swift 小结

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