奇怪的按钮动画
Weird Button Animation
我是第一次尝试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
后(标签从底部收缩到顶部):
UILabel 正在消失,但它的内容仍然可见。
您需要将UILabel的clipsToBounds
属性设置为true
。
label.clipsToBounds = true
您可以通过检查属性检查器中的 Clip Subviews
属性 在界面构建器中设置相同的 属性。
我认为你也应该制作动画 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()
}
}
我是第一次尝试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
后(标签从底部收缩到顶部):
UILabel 正在消失,但它的内容仍然可见。
您需要将UILabel的clipsToBounds
属性设置为true
。
label.clipsToBounds = true
您可以通过检查属性检查器中的 Clip Subviews
属性 在界面构建器中设置相同的 属性。
我认为你也应该制作动画 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()
}
}