我有一个带有动画的UIImageView,在UIView中,我应用了一个fadeIn效果,但我需要在触摸时动画的UIImageView应用淡出。
这是我为淡入而做的。
UIView.animateWithDuration(0.5, delay: delay,
options: UIViewAnimationOptions.CurveEaseOut, animations: {
uiImageView.alpha = 1.0
}
答案 0 :(得分:43)
根据我的研究,我会这样做:(假设你正在使用故事板)
转到UIImageView,在“属性”下,选中“启用用户交互”复选框。
将TapGestureRecognizer拖到图片视图的顶部。
控制点击Tap Gesture并拖动以对ViewControler.swift进行操作。
在里面添加以下代码:
UIView.animateWithDuration(0.5, delay: 0.5, options: .curveEaseOut, animations: {
self.uiImageView.alpha = 0.0
}, completion: nil)
然后你就完成了!
答案 1 :(得分:11)
从iOS 10开始,Apple发布了更强大的新iOS Animations SDK,尤其是时间功能和交互性。
使用这种方法淡出代码:
UIViewPropertyAnimator(duration: 0.5, curve: .easeOut, animations: {
self.uiImageView.alpha = 0.0
}).startAnimation()
要了解有关Property Animator的更多详情,请查看iOS 10 Animations demo。
答案 2 :(得分:10)
斯威夫特4。向UIView对象添加淡入和淡出功能
extension UIView {
func fadeIn(_ duration: TimeInterval = 0.5, 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 = 0.5, delay: TimeInterval = 1.0, completion: @escaping (Bool) -> Void = {(finished: Bool) -> Void in}) {
UIView.animate(withDuration: duration, delay: delay, options: UIViewAnimationOptions.curveEaseIn, animations: {
self.alpha = 0.3
}, completion: completion)
}
}
示例强>
label.fadeIn()
label.fadeOut()
imageView.fadeOut(completion: {
(finished: Bool) -> Void in
imageView.removeFromSuperview()
})
label.fadeIn(completion: {
(finished: Bool) -> Void in
label.text = "Changed!"
})
答案 3 :(得分:1)
快速5
这对我来说很好。我想为标签制作动画,并持续淡入/淡出。我将标签放在“ cardHeaderView”中。
@IBOutlet weak var cardHeaderView: UIView!
将其放置在“ viewDidAppear”中。我的延迟为零,所以动画会立即开始。
fadeViewInThenOut(view: cardHeaderView, delay: 0)
这是功能。
func fadeViewInThenOut(view : UIView, delay: TimeInterval) {
let animationDuration = 1.5
UIView.animate(withDuration: animationDuration, delay: delay, options: [UIView.AnimationOptions.autoreverse, UIView.AnimationOptions.repeat], animations: {
view.alpha = 0
}, completion: nil)
}