• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

swift基础小结02--VFL约束、属性的get和set方法、懒加载、方法替换 ...

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
一、属性的get和set方法
    
    1、自定义属性的set和get方法
    
    private(set) var _image:UIImage?
    //自定义属性get,set
    var image : UIImage?{
        get{
            return _image
        }
        set(newValue){
            _image = newValue
            self.imageView?.image = _image
        }
    }
    2、重写父类属性的set和get方法
 
//重写父类属性get,set
    override var tag:Int{
       
        get{
           return super.tag
        }
        set(newValue){
            super.tag = newValue
            self.button?.tag = newValue
        }
    
 
 
二、VFL添加约束
 
    和objective-c中相似,代码如下
let tabbarHeight:CGFloat = 49.0
func setupBaseUI(){
        self.contentView.translatesAutoresizingMaskIntoConstraints = false
        self.lineView.translatesAutoresizingMaskIntoConstraints = false
        self.tabBar.translatesAutoresizingMaskIntoConstraints = false
        let views = self.ConstraintDictionWithArray(nameArray: [self.contentView,self.lineView,self.tabBar,self.view], object: self);
        NSLayoutConstraint.activate(NSLayoutConstraint.constraints(withVisualFormat: "H:|[contentView]|", options: NSLayoutFormatOptions.alignAllLeft, metrics: nil, views: views))
       
        NSLayoutConstraint.activate(NSLayoutConstraint.constraints(withVisualFormat: "H:|[tabBar]|", options: NSLayoutFormatOptions.alignAllLeft, metrics: nil, views: views))
      
        NSLayoutConstraint.activate(NSLayoutConstraint.constraints(withVisualFormat: "V:|[contentView][tabBar(tabbarHeight)]|", options: NSLayoutFormatOptions.alignAllLeft, metrics: ["tabbarHeight":(self.isIphoneX() ? tabbarHeight_iphoneX : tabbarHeight)], views: views))
      
        NSLayoutConstraint.activate(NSLayoutConstraint.constraints(withVisualFormat: "H:|[lineView]|", options: NSLayoutFormatOptions.alignAllLeft, metrics: nil, views: views))
        NSLayoutConstraint.activate(NSLayoutConstraint.constraints(withVisualFormat: "V:|[lineView(0.5)]", options: NSLayoutFormatOptions.alignAllLeft, metrics: nil, views: views))
    }
 
//MARK: 相当于OC中的NSBinding(因为oc中没有宏定义,没有NSBinding这个宏定义,所以写一个方法代替这个方法)
    func ConstraintDictionWithArray(nameArray:Array<UIView>,object:AnyObject) -> Dictionary<String,AnyObject> {
       
        var dict:Dictionary<String,AnyObject> = [:]
       
        var count:UInt32 = 0
       
        let ivars = class_copyIvarList(object.classForCoder, &count)
       
        for i in 0...Int(count) {
           
            let obj = object_getIvar(object, ivars![i])
           
            if let temp = obj as? UIView {
               
                if nameArray.contains(temp){
                   
                    let name = String.init(cString: ivar_getName(ivars![i])!)
                   
                    dict[name] = temp
                }
               
            }
        }
        free(ivars)
       
        return dict
    }
 
三、懒加载
    
    1、懒加载创建label
    
//MARK: 懒加载创建label
    lazy var lazyLabel: UILabel = self.addLabel_d(title: "");
    func addLabel_d(title:String) -> UILabel {
        let label = UILabel(frame: CGRect.init(x: 0, y: 0, width: 200, height: 40))
        label.center = self.view.center
        label.backgroundColor = UIColor.gray
        label.textColor = UIColor.white
        label.textAlignment = NSTextAlignment.center
        label.numberOfLines = 0
        label.lineBreakMode = NSLineBreakMode.byWordWrapping
        label.text = title
        label.font = UIFont.systemFont(ofSize: 17)
        return label;
    }
    
    2、懒加载数组
//lazy
    lazy var datasource:NSArray = {
       
        var tmpdatasource = NSArray(objects: ["title":"dispatch使用","content":["sync -- 同步","async -- 异步","delay -- 延迟执行三秒","main -- 主线程","global -- 全局并发队列"]],["title":"网络请求","content":["GET -- 请求","POST -- 请求","下载图片"]],["title":"自定义组件","content":["toast","。。。"]])
       
        return tmpdatasource
    }()
 
四、方法替换
 
extension UIViewController{
   
    public class func initializeMethod() {
        let originalSelector = #selector(UIViewController.viewWillAppear(_:))
        let swizzledSelector = #selector(UIViewController.dealWithJF_Tabbar(animated:))
        let originalMethod = class_getInstanceMethod(self, originalSelector)
        let swizzledMethod = class_getInstanceMethod(self, swizzledSelector)
        //在进行 Swizzling 的时候,需要用 class_addMethod 先进行判断一下原有类中是否有要替换方法的实现
        let didAddMethod: Bool = class_addMethod(self, originalSelector, method_getImplementation(swizzledMethod!), method_getTypeEncoding(swizzledMethod!))
        //如果 class_addMethod 返回 yes,说明当前类中没有要替换方法的实现,所以需要在父类中查找,这时候就用到 method_getImplemetation 去获取 class_getInstanceMethod 里面的方法实现,然后再进行 class_replaceMethod 来实现 Swizzing
        if didAddMethod {
            class_replaceMethod(self, swizzledSelector, method_getImplementation(originalMethod!), method_getTypeEncoding(originalMethod!))
           
        } else {
            method_exchangeImplementations(originalMethod!, swizzledMethod!)
           
        }
       
    }
    @objc func dealWithJF_Tabbar(animated: Bool) {
      
        self.dealWithJF_Tabbar(animated: animated)
         print("处理自定义tabbar隐藏问题")
        self.jf_tabBarController?.tabBar.isHidden = self.jf_hidesBottomBarWhenPushed;
    }
}
 
然后在appdelegate中使用下面方法:
 
    UIViewController.initializeMethod()
 
 

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Swift学习笔记(扩展和泛型)发布时间:2022-07-13
下一篇:
swift--使用URLSession异步加载图片发布时间:2022-07-13
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap