簡體   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