使用SnapKit遇到的問題

来源:https://www.cnblogs.com/shenyuiOS/archive/2018/09/26/9706832.html
-Advertisement-
Play Games

最近在使用snapkit過程中遇到一個問題,在github上搜索之後發現另外一個有趣的問題 問題鏈接 看起來很理所當然的,明顯不可以這樣寫,但是具體是什麼原因呢,明明沒有報任何錯誤和警告,但是.multipliedBy()方法卻沒有效果,那我們來看一下snapkit源碼。 1.首先點進equalTo ...


最近在使用snapkit過程中遇到一個問題,在github上搜索之後發現另外一個有趣的問題

frameImageContainer.snp.makeConstraints({ (make) in 
    make.width.equalTo(295).multipliedBy(0.2) 
    make.height.equalTo(355).multipliedBy(0.2) 
    make.top.equalToSuperview().offset(self.view.frame.height/8) 
    make.centerX.equalToSuperview(); 
})

看起來很理所當然的,明顯不可以這樣寫,但是具體是什麼原因呢,明明沒有報任何錯誤和警告,但是.multipliedBy()方法卻沒有效果,那我們來看一下snapkit源碼。

1.首先點進equalTo()方法,代碼是這樣的:

@discardableResult
    public func equalTo(_ other: ConstraintRelatableTarget, _ file: String = #file, _ line: UInt = #line) -> ConstraintMakerEditable {
        return self.relatedTo(other, relation: .equal, file: file, line: line)
    }

再點進relatedTo()方法:

internal func relatedTo(_ other: ConstraintRelatableTarget, relation: ConstraintRelation, file: String, line: UInt) -> ConstraintMakerEditable {
        let related: ConstraintItem
        let constant: ConstraintConstantTarget
        
        if let other = other as? ConstraintItem {
            guard other.attributes == ConstraintAttributes.none ||
                  other.attributes.layoutAttributes.count <= 1 ||
                  other.attributes.layoutAttributes == self.description.attributes.layoutAttributes ||
                  other.attributes == .edges && self.description.attributes == .margins ||
                  other.attributes == .margins && self.description.attributes == .edges else {
                fatalError("Cannot constraint to multiple non identical attributes. (\(file), \(line))");
            }
            
            related = other
            constant = 0.0
        } else if let other = other as? ConstraintView {
            related = ConstraintItem(target: other, attributes: ConstraintAttributes.none)
            constant = 0.0
        } else if let other = other as? ConstraintConstantTarget {
            related = ConstraintItem(target: nil, attributes: ConstraintAttributes.none)
            constant = other
        } else if #available(iOS 9.0, OSX 10.11, *), let other = other as? ConstraintLayoutGuide {
            related = ConstraintItem(target: other, attributes: ConstraintAttributes.none)
            constant = 0.0
        } else {
            fatalError("Invalid constraint. (\(file), \(line))")
        }
        
        let editable = ConstraintMakerEditable(self.description)
        editable.description.sourceLocation = (file, line)
        editable.description.relation = relation
        editable.description.related = related
        editable.description.constant = constant
        return editable
    }

可以看到上面紅色部分,此時other可以轉換為ConstraintConstantTarget類型,設置related的target為nil,attributes為none,constant設置為other,最後將這些變數賦值給description屬性保存。

2.multipliedBy()方法:

@discardableResult
    public func multipliedBy(_ amount: ConstraintMultiplierTarget) -> ConstraintMakerEditable {
        self.description.multiplier = amount
        return self
    }

可以看到,multipliedBy方法中只是簡單的賦值,將amout值複製到description中保存。

3.再來看一下makeConstraints()方法:

internal static func makeConstraints(item: LayoutConstraintItem, closure: (_ make: ConstraintMaker) -> Void) {
        let maker = ConstraintMaker(item: item)
        closure(maker)
        var constraints: [Constraint] = []
        for description in maker.descriptions {
            guard let constraint = description.constraint else {
                continue
            }
            constraints.append(constraint)
        }
        for constraint in constraints {
            constraint.activateIfNeeded(updatingExisting: false)
        }
    }

首先將要添加約束的對象封裝成ConstraintMaker對象,再把它作為參數調用closure,開始添加約束。closure中的每條語句會先被解析ConstraintDescription對象,添加到maker的descriptions屬性中。 在第一個for迴圈中可以看到,每次迴圈從descriptions中取出一條description,判斷是否改description是否有constraint屬性;

constraint屬性使用的懶載入,值為:

internal lazy var constraint: Constraint? = {
        guard let relation = self.relation,
              let related = self.related,
              let sourceLocation = self.sourceLocation else {
            return nil
        }
        let from = ConstraintItem(target: self.item, attributes: self.attributes)
        
        return Constraint(
            from: from,
            to: related,
            relation: relation,
            sourceLocation: sourceLocation,
            label: self.label,
            multiplier: self.multiplier,
            constant: self.constant,
            priority: self.priority
        )
    }()

先判斷relation,related,sourceLocation的值是否為nil,若為nil則返回nil,否則就根據description屬性創建一個Constraint對象並返回。

Constraint的構造方法:

    internal init(from: ConstraintItem,
                  to: ConstraintItem,
                  relation: ConstraintRelation,
                  sourceLocation: (String, UInt),
                  label: String?,
                  multiplier: ConstraintMultiplierTarget,
                  constant: ConstraintConstantTarget,
                  priority: ConstraintPriorityTarget) {
        self.from = from
        self.to = to
        self.relation = relation
        self.sourceLocation = sourceLocation
        self.label = label
        self.multiplier = multiplier
        self.constant = constant
        self.priority = priority
        self.layoutConstraints = []

        // get attributes
        let layoutFromAttributes = self.from.attributes.layoutAttributes
        let layoutToAttributes = self.to.attributes.layoutAttributes

        // get layout from
        let layoutFrom = self.from.layoutConstraintItem!

        // get relation
        let layoutRelation = self.relation.layoutRelation

        for layoutFromAttribute in layoutFromAttributes {
            // get layout to attribute
            let layoutToAttribute: LayoutAttribute
            #if os(iOS) || os(tvOS)
                if layoutToAttributes.count > 0 {
                    if self.from.attributes == .edges && self.to.attributes == .margins {
                        switch layoutFromAttribute {
                        case .left:
                            layoutToAttribute = .leftMargin
                        case .right:
                            layoutToAttribute = .rightMargin
                        case .top:
                            layoutToAttribute = .topMargin
                        case .bottom:
                            layoutToAttribute = .bottomMargin
                        default:
                            fatalError()
                        }
                    } else if self.from.attributes == .margins && self.to.attributes == .edges {
                        switch layoutFromAttribute {
                        case .leftMargin:
                            layoutToAttribute = .left
                        case .rightMargin:
                            layoutToAttribute = .right
                        case .topMargin:
                            layoutToAttribute = .top
                        case .bottomMargin:
                            layoutToAttribute = .bottom
                        default:
                            fatalError()
                        }
                    } else if self.from.attributes == self.to.attributes {
                        layoutToAttribute = layoutFromAttribute
                    } else {
                        layoutToAttribute = layoutToAttributes[0]
                    }
                } else {
                    if self.to.target == nil && (layoutFromAttribute == .centerX || layoutFromAttribute == .centerY) {
                        layoutToAttribute = layoutFromAttribute == .centerX ? .left : .top
                    } else {
                        layoutToAttribute = layoutFromAttribute
                    }
                }
            #else
                if self.from.attributes == self.to.attributes {
                    layoutToAttribute = layoutFromAttribute
                } else if layoutToAttributes.count > 0 {
                    layoutToAttribute = layoutToAttributes[0]
                } else {
                    layoutToAttribute = layoutFromAttribute
                }
            #endif

            // get layout constant
            let layoutConstant: CGFloat = self.constant.constraintConstantTargetValueFor(layoutAttribute: layoutToAttribute)

            // get layout to
            var layoutTo: AnyObject? = self.to.target

            // use superview if possible
            if layoutTo == nil && layoutToAttribute != .width && layoutToAttribute != .height {
                layoutTo = layoutFrom.superview
            }

            // create layout constraint
            let layoutConstraint = LayoutConstraint(
                item: layoutFrom,
                attribute: layoutFromAttribute,
                relatedBy: layoutRelation,
                toItem: layoutTo,
                attribute: layoutToAttribute,
                multiplier: self.multiplier.constraintMultiplierTargetValue,
                constant: layoutConstant
            )

            // set label
            layoutConstraint.label = self.label

            // set priority
            layoutConstraint.priority = LayoutPriority(rawValue: self.priority.constraintPriorityTargetValue)

            // set constraint
            layoutConstraint.constraint = self

            // append
            self.layoutConstraints.append(layoutConstraint)
        }
    }

重點看紅色部分,遍歷layoutAttributes,並根據layoutAttribute的值生成一個LayoutConstraint對象添加到layoutConstraints數組中。LayoutConstraint繼承自系統類NSLayoutConstraint。

4.最後再看3中的第二個for迴圈,使用activeIfNeeded()方法激活約束:

 internal func activateIfNeeded(updatingExisting: Bool = false) {
        guard let item = self.from.layoutConstraintItem else {
            print("WARNING: SnapKit failed to get from item from constraint. Activate will be a no-op.")
            return
        }
        let layoutConstraints = self.layoutConstraints

        if updatingExisting {
            var existingLayoutConstraints: [LayoutConstraint] = []
            for constraint in item.constraints {
                existingLayoutConstraints += constraint.layoutConstraints
            }

            for layoutConstraint in layoutConstraints {
                let existingLayoutConstraint = existingLayoutConstraints.first { $0 == layoutConstraint }
                guard let updateLayoutConstraint = existingLayoutConstraint else {
                    fatalError("Updated constraint could not find existing matching constraint to update: \(layoutConstraint)")
                }

                let updateLayoutAttribute = (updateLayoutConstraint.secondAttribute == .notAnAttribute) ? updateLayoutConstraint.firstAttribute : updateLayoutConstraint.secondAttribute
                updateLayoutConstraint.constant = self.constant.constraintConstantTargetValueFor(layoutAttribute: updateLayoutAttribute)
            }
        } else {
            NSLayoutConstraint.activate(layoutConstraints)
            item.add(constraints: [self])
        }
    }

當updatingExisting為false時,進入else語句,使用的系統類NSLayoutConstraint的方法激活約束:

/* Convenience method that activates each constraint in the contained array, in the same manner as setting active=YES. This is often more efficient than activating each constraint individually. */
    @available(iOS 8.0, *)
    open class func activate(_ constraints: [NSLayoutConstraint])

並將設置過的約束添加到item的constraintSet這個私有屬性中:

internal var constraints: [Constraint] {
        return self.constraintsSet.allObjects as! [Constraint]
    }
    
    internal func add(constraints: [Constraint]) {
        let constraintsSet = self.constraintsSet
        for constraint in constraints {
            constraintsSet.add(constraint)
        }
    }
    
    internal func remove(constraints: [Constraint]) {
        let constraintsSet = self.constraintsSet
        for constraint in constraints {
            constraintsSet.remove(constraint)
        }
    }
    
    private var constraintsSet: NSMutableSet {
        let constraintsSet: NSMutableSet
        
        if let existing = objc_getAssociatedObject(self, &constraintsKey) as? NSMutableSet {
            constraintsSet = existing
        } else {
            constraintsSet = NSMutableSet()
            objc_setAssociatedObject(self, &constraintsKey, constraintsSet, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
        }
        return constraintsSet
        
    }

5.通過這個過程不難發現,使用make.width.equalTo(295).multipliedBy(0.2) 這種方式不能得到想要的結果。在3中Constraint的構造方法的紅色部分,其實構造LayoutConstraint對象時調用的NSLayoutConstraint的便利構造器方法:

    /* Create constraints explicitly.  Constraints are of the form "view1.attr1 = view2.attr2 * multiplier + constant" 
     If your equation does not have a second view and attribute, use nil and NSLayoutAttributeNotAnAttribute.
     */
    public convenience init(item view1: Any, attribute attr1: NSLayoutConstraint.Attribute, relatedBy relation: NSLayoutConstraint.Relation, toItem view2: Any?, attribute attr2: NSLayoutConstraint.Attribute, multiplier: CGFloat, constant c: CGFloat)

註意上面註釋 view1.attr1 = view2.attr2 * multiplier + constant 如果只設置為數字,則相當於view2為nil,所以view1的屬性值只能等於constant的值,不會乘以multiplier。

6.終於寫完了,哈哈。  demo工程地址,對應的方法已打斷點,可以跟著代碼一步步調試,有助於理解。

 

 

疑問:在4中最後一部分紅色字體的內容,私有屬性constraintsSet為啥不直接使用,還要使用runtime給當前對象綁定一個同名的屬性,每次使用時獲取綁定的屬性的值,不懂,希望知道的同學不吝賜教。

 

 

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • mysqldump version occ error while mysql workbench export data ...
  • 查詢mongo鏡像 拉取鏡像(拉取STARS最多的那個就可以了) 使用自定義配置文件啟動mongo 使用環境變數配置初始化賬號密碼 數據持久化 數據導出 更多請訪問這裡 ...
  • 接手別人的項目,在一次修改某個畫曲線的功能時發現卡頓,以前的同事是不管歷史記錄直接從頭畫,然後領導希望能有歷史曲線,改完後發現切換不同設備時會卡頓,然後我就在畫曲線的功能里逐個輸出消耗時間,最後發現資料庫操作時出現等到幾秒的情況,我這個是SQLITE本地資料庫,然後查看以前同事寫的方法,發現他把數據 ...
  • 是一種高性能大數據存儲方案,支持快速過濾查找和即席OLAP分析,已在20+企業生產環境上部署應用,其中最大的單一集群數據規模達到幾萬億。針對當前大數據領域分析場景需求各異而導致的存儲冗餘問題,業務驅動下的數據分析靈活性要求越來越高,CarbonData提供了一種新的融合數據存儲方案,以一份數據同時支... ...
  • app.post('/upload', function(req, res){ //接收前臺POST過來的base64 var imgData = req.body.imgData; //過濾data:URL var base64Data = imgData.replace(/^data:image ...
  • 1、第一步,在本地資料庫中建一個與伺服器同名的資料庫 2、第二步,右鍵源資料庫,任務》導出數據,彈出導入導出提示框,點下一步繼續 3、遠程資料庫操作,確認伺服器名稱(伺服器地址)、身份驗證(輸入用戶名、密碼)、選擇需要導出的源資料庫,點下一步繼續 4、本地目標伺服器操作,確認伺服器名稱、輸入用戶名密 ...
  • 一.開啟慢查詢日誌,可以讓MySQL記錄下查詢超過指定時間的語句,通過定位分析性能的瓶頸,才能更好的優化資料庫系統的性能。 二、慢日誌參數: slow_query_log 慢查詢開啟狀態slow_query_log_file 慢查詢日誌存放的位置(這個目錄需要MySQL的運行帳號的可寫許可權,一般設置 ...
  • 今天我遇到了一個需求,是將一個DBF文件導入到Oracle庫中,之前一直使用的是公司提供的遷移工具,但是不知道怎麼回事今天一直報DBF文件無法訪問之類的錯誤,嘗試多次之後,一氣之下棄之不用,另尋他法。 ODBC(Open Database Connectivity)是微軟提供的,專門為解決異構資料庫 ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...