簡體   English   中英

如何使用Swift 4/5擴展多個類

[英]How to extend more than one class using Swift 4/5

我正在嘗試擴展多個類,即UIButton和UITextField。 它們都具有相同的功能,在我調用該功能時可以擺動。 我試圖不重復我的代碼不止一次。 我一直在嘗試使用protocal,以便可以擴展並編寫所需的函數,然后在我的類中對其進行擴展,但是問題出在我的函數中,我必須調用self,但是由於self可以僅在UITextField和UIButton上調用。

這是我的代碼

import UIKit

extension UIButton {

    func wiggle() {
        let position = "position"
        let wiggleAnimation = CABasicAnimation(keyPath: position)
        wiggleAnimation.duration = 0.05
        wiggleAnimation.repeatCount = 5
        wiggleAnimation.autoreverses = true
        wiggleAnimation.fromValue = CGPoint(x: self.center.x - 4.0, y: self.center.y)
        wiggleAnimation.toValue = CGPoint(x: self.center.x + 4.0, y: self.center.y)
        layer.add(wiggleAnimation, forKey: position)
    }

}

extension UITextField {

    func wiggle() {
        let position = "position"
        let wiggleAnimation = CABasicAnimation(keyPath: position)
        wiggleAnimation.duration = 0.05
        wiggleAnimation.repeatCount = 5
        wiggleAnimation.autoreverses = true
        wiggleAnimation.fromValue = CGPoint(x: self.center.x - 4.0, y: self.center.y)
        wiggleAnimation.toValue = CGPoint(x: self.center.x + 4.0, y: self.center.y)
        layer.add(wiggleAnimation, forKey: position)
    }

}

這是我嘗試嘗試的操作,但由於我打電話給自己而出現錯誤。

protocol Animations {
    func wiggle()
}

extension Animations {
    func wiggle() {
        let position = "position"
        let wiggleAnimation = CABasicAnimation(keyPath: position)
        wiggleAnimation.duration = 0.05
        wiggleAnimation.repeatCount = 5
        wiggleAnimation.autoreverses = true
        wiggleAnimation.fromValue = CGPoint(x: self.center.x - 4.0, y: self.center.y)
        wiggleAnimation.toValue = CGPoint(x: self.center.x + 4.0, y: self.center.y)
        layer.add(wiggleAnimation, forKey: position)
    }
}

extension UIButton: Animations {}

extension UITextField: Animations {}

我收到的錯誤是'Self'類型的值沒有成員'center''Self'類型的值沒有成員'center'使用了未解析的標識符'layer'

UIView具有center屬性。 對於Swift 5+,您的協議聲明應如下所示:

protocol Animations: UIView {
    func wiggle()
}

注意只有UIViews才能符合此協議。

對於Swift 4.x,您必須像這樣使用它:

protocol Animations {
    func wiggle()
}

extension Animations where Self: UIView {
   func wiggle() {
        let position = "position"
        let wiggleAnimation = CABasicAnimation(keyPath: position)
        wiggleAnimation.duration = 0.05
        wiggleAnimation.repeatCount = 5
        wiggleAnimation.autoreverses = true
        wiggleAnimation.fromValue = CGPoint(x: self.center.x - 4.0, y: self.center.y)
        wiggleAnimation.toValue = CGPoint(x: self.center.x + 4.0, y: self.center.y)
        layer.add(wiggleAnimation, forKey: position)
    }
}

前面給出的答案實際上無法使用Swift 4.x進行編譯,因為協議無法直接從Swift 5之前的類繼承。

在Swift 4.x中編寫此代碼的正確方法包括僅在符合類型從UIView繼承時才為wiggle()函數提供默認實現。 正確的寫法是:

protocol Animations {
    func wiggle()
}
extension Animations where Self: UIView {
   func wiggle() {
        let position = "position"
        let wiggleAnimation = CABasicAnimation(keyPath: position)
        wiggleAnimation.duration = 0.05
        wiggleAnimation.repeatCount = 5
        wiggleAnimation.autoreverses = true
        wiggleAnimation.fromValue = CGPoint(x: self.center.x - 4.0, y: self.center.y)
        wiggleAnimation.toValue = CGPoint(x: self.center.x + 4.0, y: self.center.y)
        layer.add(wiggleAnimation, forKey: position)
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM