我该如何使用阿尔法扩展对于SWIFT所有的对象?

问题描述 投票:0回答:2

我有下面的代码改变,如文本,图像,...与影响对象的值。和它的工作:

UIView.animate(withDuration: 1.0, delay: 0.0, options: UIViewAnimationOptions.curveEaseOut, animations: {
                        self.lblCityTemp.alpha = 0.0
                        self.imgCityIcon.alpha = 0.0
                    }, completion: {
                        (finished: Bool) -> Void in

                        //Once the label is completely invisible, set the text and fade it back in
                        self.imgCityIcon.image = UIImage(named: icon)
                        self.lblCityTemp.text = "\(temp)°"

                        // Fade in
                        UIView.animate(withDuration: 1.0, delay: 0.0, options: UIViewAnimationOptions.curveEaseIn, animations: {
                            self.lblCityTemp.alpha = 1.0
                            self.imgCityIcon.alpha = 1.0

                        }, completion: nil)
                    })

现在,我希望做一个扩展的代码,我用我所有的对象,我下面写的代码:

extension NSObject {

    func Fade (alphaOUT: Double,alphaIN: Double, input: Any) {
        UIView.animate(withDuration: 1.0, delay: 0.0, options: UIViewAnimationOptions.curveEaseOut, animations: {
            self.alpha = CGFloat(alphaOUT)
        }, completion: {
            (finished: Bool) -> Void in

            // Fade in
            UIView.animate(withDuration: 1.0, delay: 0.0, options: UIViewAnimationOptions.curveEaseIn, animations: {
                self.alpha = CGFloat(alphaIN)

            }, completion: nil)
        })
    }
}

但是我有自我字错误,我不知道我怎么能像标签,ImageView的,每个对象的设定值....

我怎样才能做到这一点?是扩展是最好的办法还是没有?如果没有,那么,什么是最好的方式?

ios swift
2个回答
2
投票

整个想法应该采取一些UIView和动画它。然后,你需要UIView的扩展,因为这个类有你需要的属性。

extension UIView { ... }

无论如何,如果你需要什么现在当动画结束时做一些事情?然后,你需要完成处理程序参数的方法

func fade(..., completion: @escaping () -> Void = { }) {

......第一动画结束后会被调用。

下一步建议:

  • 可以说,阿尔法参数的类型应该CGFloat的,那么你就不必转换DoubleCGFloat
  • 此外,什么是input?我想你不会有这种解决方案需要它
  • 方法名称应与小大写字母开头和命名参数,你应该使用驼峰

extension UIView {
    func fade(alphaOut: CGFloat, alphaIn: CGFloat, completion: @escaping () -> Void = { }) {
        UIView.animate(withDuration: 1, delay: 0, options: .curveEaseOut, animations: {
            self.alpha = alphaOut
        }, completion: { _ in
            completion() // this is called when animation ends
            UIView.animate(withDuration: 1, delay: 0, options: .curveEaseIn, animations: {
                self.alpha = alphaIn
            })
        })
    }
}

然后,当你需要调用它,改变你需要什么completion关闭动画结束后,

imgCityIcon.fade(alphaOut: ___, alphaIn: ___) {
    self.imgCityIcon.image = UIImage(named: icon)
}

请注意,您在使用旧语法的一些东西:

例如UIViewAnimationOptions.curveEaseIn已更名为UIView.AnimationOptions.curveEaseIn。我建议你开始使用雨燕的新版本


2
投票

您需要实现UIView而不是NSObject的一个扩展。因为NSObject的没有像阿尔法任何财产,你会得到一个错误。

编码实施例:

extension UIView {
    func Fade (alphaOUT: Double,alphaIN: Double, input: Any) {
        UIView.animate(withDuration: 1.0, delay: 0.0, options: UIView.AnimationOptions.curveEaseOut, animations: {
            self.alpha = CGFloat(alphaOUT)
        }, completion: {
            (finished: Bool) -> Void in

            // Fade in
            UIView.animate(withDuration: 1.0, delay: 0.0, options: UIView.AnimationOptions.curveEaseIn, animations: {
                self.alpha = CGFloat(alphaIN)

            }, completion: nil)
        })
    }
}

class AnimationHelper {

class func Fade (alphaOUT: Double,alphaIN: Double, input: Any , yourLabel: UILabel, yourTextField: UITextField) {
            UIView.animate(withDuration: 1.0, delay: 0.0, options: UIView.AnimationOptions.curveEaseOut, animations: {
                yourLabel.alpha = CGFloat(alphaOUT)
                yourTextField.alpha = CGFloat(alphaOUT)
            }, completion: {
                (finished: Bool) -> Void in

                // Fade in
                UIView.animate(withDuration: 1.0, delay: 0.0, options: UIView.AnimationOptions.curveEaseIn, animations: {
                    // Access your text field and label here.

                }, completion: nil)
            })
        }

}
© www.soinside.com 2019 - 2024. All rights reserved.