【问题标题】:Weird Button Animation奇怪的按钮动画
【发布时间】:2016-04-19 10:27:45
【问题描述】:

这是我第一次尝试IOS动画,所以如果我从一开始就担心,请告诉我正确的方向。

我想要的:当我点击ButtonOne 时,Label 会慢慢消失。 代码如下:

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var labelHeight: NSLayoutConstraint!
    private var isHidden: Bool = false

    @IBAction func clickButtonOne(sender: UIButton) {
        isHidden = !isHidden
        if isHidden {
            labelHeight.constant = 0
        } else {
            labelHeight.constant = 60
        }
        UIView.animateWithDuration(1.0, animations: {
                () -> Void in
                self.view.layoutIfNeeded()
            }, completion: nil)
    }
    override func viewDidLoad() {
        super.viewDidLoad()
    }
}

在我点击ButtonOne之前: 在我点击ButtonOne(标签从下到上缩小)后:

【问题讨论】:

    标签: ios swift uibutton swift2 uilabel


    【解决方案1】:

    我认为您还应该为UIView.alpha 属性设置动画:

    导入 UIKit

    class ViewController: UIViewController {
    
        @IBOutlet weak var button: UIButton!
        @IBOutlet weak var labelHeight: NSLayoutConstraint!
        private var isHidden: Bool = false
    
        @IBAction func clickButtonOne(sender: UIButton) {
            isHidden = !isHidden
            var alpha: CGFloat = 1
            if isHidden {
                alpha = 0
                labelHeight.constant = 0
            } else {
                labelHeight.constant = 60
            }
            UIView.animateWithDuration(1.0, animations: {
                    () -> Void in
                    self.button.alpha = alpha
                    self.view.layoutIfNeeded()
                }, completion: nil)
        }
    
        override func viewDidLoad() {
            super.viewDidLoad()
        }
    }
    

    【讨论】:

    • 我自己试过 label.text="" ,它的工作方式类似于 alpha=0,但我认为 ZeMoon 的解决方案更好。顺便说一句,谢谢你的编辑,真的很尴尬......
    【解决方案2】:

    UILabel 正在消失,但它的内容仍然可见。

    您需要将UILabel的clipsToBounds属性设置为true

    label.clipsToBounds = true
    

    您可以通过检查属性检查器中的Clip Subviews 属性在界面构建器中设置相同的属性。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-23
      • 1970-01-01
      • 1970-01-01
      • 2016-03-02
      • 1970-01-01
      • 2016-12-05
      • 2021-02-10
      • 1970-01-01
      相关资源
      最近更新 更多