代码之家  ›  专栏  ›  技术社区  ›  cs4alhaider Andrei G.

自定义枚举案例的UIView扩展中的新属性

  •  0
  • cs4alhaider Andrei G.  · 技术社区  · 6 年前

    在这里,我创建了一个小枚举来保存我的所有动画案例:

    enum ViewAnimation {
        case changeColor(to: UIColor, duration: TimeInterval)
        case hideView(duruation: TimeInterval)
    }
    

    在这里,我想为UIView创建一个属性:

    extension UIView {
    
    
    var animate: ViewAnimation {
        get {
            return .changeColor(to: .red, duration: 1) // actually I don't know what to add in the getter !
        }
        set {
            switch self.animate {
            case .changeColor(let newColor, let duration):
                UIView.animate(withDuration: duration) {
                    self.backgroundColor = newColor
                }
            case .hideView(let duration):
                UIView.animate(withDuration: duration) {
                    self.alpha = 0
                    self.isHidden = true
                }
            }
        }
    }
    
    }
    

    这是我的课:

    import UIKit
    
    class ViewController: UIViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
    
        let smallView = UIView(frame: CGRect(x: 0, y: 0, width: 200, height: 200))
        view.addSubview(smallView)
        smallView.backgroundColor = .red
        smallView.center = view.center
    
        DispatchQueue.main.asyncAfter(deadline: .now() + 2) {
            smallView.animate = .changeColor(to: .blue, duration: 3)
        }
    }
    }
    

    问题是当我打电话 smallView.animate = .changeColor(to: .blue, duration: 3) 没什么变化!

    知道为什么没用吗?

    3 回复  |  直到 6 年前
        1
  •  2
  •   Rico Crescenzio    6 年前

    为什么要在扩展中创建属性?你甚至不需要干杯。 嗯,最好是创造一种方法。

    extension UIView {
        func animate(animation: ViewAnimation) {
            switch animation {
            case .changeColor(let newColor, let duration):
                UIView.animate(withDuration: duration) {
                    self.backgroundColor = newColor
                }
            case .hideView(let duration):
                UIView.animate(withDuration: duration) {
                    self.alpha = 0
                    self.isHidden = true
                }
            }
        }
    }
    

    并称之为 smallView.animate(animation: .changeColor(to: .blue, duration: 3))

    switch self.animate 因为它会叫“盖特”。 试着做 switch newValue set 有一个隐式局部变量 newValue 这是分配的当前值。

        2
  •  1
  •   AppleCiderGuy    6 年前

    您正在检查开关盒中的旧值。 你需要这样做:

    switch newValue {
        case .changeColor(let newColor, let duration):
            UIView.animate(withDuration: duration) {
                self.backgroundColor = newColor
            }
        case .hideView(let duration):
            UIView.animate(withDuration: duration) {
                self.alpha = 0
                self.isHidden = true
            }
        }
    

    newValue 如果实现如下设置,则为默认值 set{ 或者你可以这样做:

    set(abc){
      switch abc {
    
        3
  •  0
  •   Mocha    6 年前

    我相信开关实际上是在调用getter,因此总是将背景设置为红色。

    var helpme: Int {
        get {
            return 1
        }
        set {
            switch helpme {
            default:
                print(helpme)
            }
        }
    }
    
    func testme() {
        helpme = 20
        print(helpme)
    }
    
    output:
    1
    1
    

        switch self.animate { //  calls the getter which returns red
        case .changeColor(let newColor, let duration):
            UIView.animate(withDuration: duration) {
                self.backgroundColor = newColor
            }
        case .hideView(let duration):
            UIView.animate(withDuration: duration) {
                self.alpha = 0
                self.isHidden = true
            }
        }
    

    我认为一个房产对于你想做的事情来说太有限了。

    class AnimatingView: UIView {
      var animationType: AnimationType?
    }
    
    
    enum AnimationType {
      case changingColor
      case hidingView
    }
    
    func changeColor() {
      animationType = .changingColor
      // change color
    }