6
A
回答
6
即使视图已经加载,当调用viewDidLoad
时,用户可能看不到该视图。这意味着你可能会发现你的动画看起来已经开始了。为了解决这个问题,你需要改用viewDidAppear
开始动画。
至于fadeIn
和fadeOut
功能 - 它们不存在。你需要自己写。谢天谢地,这很容易做到。像下面的东西可能会足够好。
func fadeViewInThenOut(view : UIView, delay: NSTimeInterval) {
let animationDuration = 0.25
// Fade in the view
UIView.animateWithDuration(animationDuration, animations: {() -> Void in
view.alpha = 1
}) { (Bool) -> Void in
// After the animation completes, fade out the view after a delay
UIView.animateWithDuration(animationDuration, delay: delay, options: .CurveEaseInOut, animations: {() -> Void in
view.alpha = 0
},
completion: nil)
}
}
14
我的建议是利用Swift扩展使代码更加模块化,易于使用。例如,如果您想让多个标签淡入淡出或一个标签在多个视图中淡入/淡出,则必须在各处传递animateWithDuration方法,这可能很麻烦。一个更清洁的选择是创建一个名为UIView.swift的文件(我们的UIView扩展)。下面的代码添加到该文件:
import Foundation
extension UIView {
func fadeIn(duration: NSTimeInterval = 1.0, delay: NSTimeInterval = 0.0, completion: ((Bool) -> Void) = {(finished: Bool) -> Void in}) {
UIView.animateWithDuration(duration, delay: delay, options: UIViewAnimationOptions.CurveEaseIn, animations: {
self.alpha = 1.0
}, completion: completion) }
func fadeOut(duration: NSTimeInterval = 1.0, delay: NSTimeInterval = 3.0, completion: (Bool) -> Void = {(finished: Bool) -> Void in}) {
UIView.animateWithDuration(duration, delay: delay, options: UIViewAnimationOptions.CurveEaseIn, animations: {
self.alpha = 0.0
}, completion: completion)
}
}
现在你可以添加淡入/淡出功能的UIView(例如,的UILabel,UIImage的,等等)的任何子女。里面你的viewDidLoad()函数,你可以添加:现在
self.statusLabel.alpha = 0
self.statusLabel.text = "Sample Text Here"
self.myLabel.fadeIn(completion: {
(finished: Bool) -> Void in
self.myLabel.fadeOut()
})
,您可以使用此示例代码的图像视图,标签,文本视图,滚动视图或UIView的任何孩子为好。我希望这有帮助。
4
更新雨燕3.1 - 在@Andy代码
func fadeViewInThenOut(view : UIView, delay: TimeInterval) {
let animationDuration = 0.25
// Fade in the view
UIView.animate(withDuration: animationDuration, animations: {() -> Void in
view.alpha = 1
}) { (Bool) -> Void in
// After the animation completes, fade out the view after a delay
UIView.animate(withDuration: animationDuration, delay: delay, options: [.curveEaseOut], animations: {() -> Void in
view.alpha = 0
}, completion: nil)
}
}
+0
编辑按钮出于某种原因。复制粘贴答案只是稍微修改语法并不好。 – Fogmeister
4
回答更新的斯威夫特3 - 使用扩展
extension UIView {
func fadeIn(duration: TimeInterval = 1.0, delay: TimeInterval = 0.0, completion: @escaping ((Bool) -> Void) = {(finished: Bool) -> Void in}) {
UIView.animate(withDuration: duration, delay: delay, options: UIViewAnimationOptions.curveEaseIn, animations: {
self.alpha = 1.0
}, completion: completion)
}
func fadeOut(duration: TimeInterval = 1.0, delay: TimeInterval = 3.0, completion: @escaping (Bool) -> Void = {(finished: Bool) -> Void in}) {
UIView.animate(withDuration: duration, delay: delay, options: UIViewAnimationOptions.curveEaseIn, animations: {
self.alpha = 0.0
}, completion: completion)
}
}
用法:
self.statusLabel.alpha = 0
self.statusLabel.text = "Sample Text Here"
self.myLabel.fadeIn(completion: {
(finished: Bool) -> Void in
self.myLabel.fadeOut()
})
相关问题
- 1. (Swift)淡入淡出标签
- 2. 如何使用淡入淡出/淡入淡出使用JS
- 3. 点击锚标签时淡入淡出
- 4. 淡入淡出改变标签?
- 5. 悬停时淡入/淡出标签
- 6. C#文本标签淡入/淡出
- 7. 两个C4Images之间的淡入淡出或淡入淡出
- 8. 淡入淡出或淡入淡出后的显示块
- 9. 淡入淡出div标记
- 10. JQuery - IE6 - 如何同时淡入淡出和淡入淡出?
- 11. 如何让组合框的标签在Flex中淡入淡出
- 12. 如何将交叉淡入淡出添加到UI标签?
- 13. jScrollPane标签 - 如何让它们淡入淡出?
- 14. 如何淡入和淡出标签。连续
- 15. 如何淡入淡出?
- 16. 如何在淡入时淡入淡入淡出
- 17. 如何使菜单淡入淡出?
- 18. 如何使文字淡入淡出
- 19. 如何使用ECSlidingController淡入淡出topviewcontoller?
- 20. jquery淡出,淡入淡出,等待,淡入淡出
- 21. 淡入淡出
- 22. 淡入淡出
- 23. 淡入淡出
- 24. 淡入淡出UILabel,改变文字,淡入淡出:只有淡入淡出
- 25. 使用阵列中的字符串淡入/淡出标签
- 26. 如何使用C#中的定时器使标签淡入淡出?
- 27. 淡入/淡出UIScrollView的内容如Mobile Safari在其标签
- 28. 使fadetoggle淡入淡出
- 29. 使用jQuery淡入淡出
- 30. 如何在vb.net中使用带淡入/淡出的标签显示消息?
这应该是正确答案。非常干净和抽象。谢谢。 – user3286381
这是一个很棒的解决这个问题的方法。 –
这看起来不错!谢谢。仅供参考“完成”参数需要一个@escaping关闭。 – adamcee