繁体   English   中英

在动画Swift中淡入并淡出

[英]Fade In and Fade out in Animation Swift

我有一个带有动画的UIImageView,在UIView中,我应用了一个fadeIn效果,但我需要在触摸时动画的UIImageView应用淡出。

这就是我为淡入而做的。

UIView.animateWithDuration(0.5, delay: delay, 
    options: UIViewAnimationOptions.CurveEaseOut, animations: {
        uiImageView.alpha = 1.0
        }

这是我根据我的研究做的事情:(假设你正在使用故事板)

  1. 转到UIImageView,在Attributes下,选中“User Interaction Enabled”复选框。

  2. 将TapGestureRecognizer拖动到图像视图的顶部。

  3. 控制单击Tap Gesture并拖动以对ViewControler.swift进行操作。

  4. 在里面添加以下代码:

     UIView.animate(withDuration: 0.5, delay: 0.5, options: .curveEaseOut, animations: { self.uiImageView.alpha = 0.0 }, completion: nil) 

那你就完成了!

从iOS 10开始,Apple推出了新的iOS动画SDK ,它功能更强大,特别是在时序功能和交互性方面。

使用这种方法淡出代码将:

UIViewPropertyAnimator(duration: 0.5, curve: .easeOut, animations: {
    self.uiImageView.alpha = 0.0
}).startAnimation()

要获得有关Property Animator的更多详细信息,请查看iOS 10 Animations演示

斯威夫特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!"
})

斯威夫特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)

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM