2017-03-23 13 views
1

0.5秒後に公開したいUIViewがあり、0.5秒後に再度非表示にして簡単なアニメーションを作成します。私のコードは次の通りです:UIViewPropertyAnimatorでUIViewのアルファベットをアニメーション化

let animation = UIViewPropertyAnimator.init(duration: 0.5, curve: .linear) { 
     self.timerBackground.alpha = 1 
     let transition = UIViewPropertyAnimator.init(duration: 0.5, curve: .linear) { 
      self.timerBackground.alpha = 0 
     } 
     transition.startAnimation(afterDelay: 0.5) 
    } 
    animation.startAnimation() 

私はそれを試しても何も起こりません。私はそれが両方とも同時に実行されているからだと思います。これは互いに打ち消し合うことを意味しますが、 "afterDelay"部分が防ぐべきことではありませんか?

私はそれらを個別に実行すると、つまり、隠された状態から見えない状態に、または隠されている状態にフェードするかのいずれかで動作しますが、シーケンスで実行しようとすると機能しません。

UIViewが不透明でも非表示でもありません。

+0

"ですが、" afterDelay "部分が防止すべきものではありませんか?いいえ。アニメーションの後に何かしたい場合は、_completion handler_をプロパティー・アニメーターに追加します。 https://developer.apple.com/reference/uikit/uiviewpropertyanimator/1648373-addcompletion – matt

答えて

0

UIView.animateKeyframesを使用すると、複雑なアニメーションがあればコードをうまく構築できます。他のコンプリートブロック内でネストされたUIViewアニメーションを使用すると、おそらくインデントレベルがばらつき、可読性はゼロになります。ここで

は例です:

/* Target frames to move our object to (and animate) 
    or it could be alpha property in your case... */ 

let newFrameOne = CGRect(x: 200, y: 50, width: button.bounds.size.width, height: button.bounds.size.height) 
let newFrameTwo = CGRect(x: 300, y: 200, width: button.bounds.size.width, height: button.bounds.size.height) 

UIView.animateKeyframes(withDuration: 2.0, 
           delay: 0.0, 
          options: .repeat, 
          animations: { _ in 
    /* First animation */          
    UIView.addKeyframe(withRelativeStartTime: 0.0, relativeDuration: 0.5, animations: { [weak self] in 
     self?.button.frame = newFrameOne 
    }) 

    /* Second animation */          
    UIView.addKeyframe(withRelativeStartTime: 0.5, relativeDuration: 0.5, animations: { [weak self] in 
     self?.button.frame = newFrameTwo 
    }) 

    /* . . . */ 

    }, completion: nil) 
4

あなたはTimerを使用して、あなたのUIViewPropertyAnimator対象に、すべてのタイマーチックにアニメーションブロックを隠す/表示されてを追加することができます。ここで

は、コードベースです:

@IBOutlet weak var timerBackground: UIImageView! 

private var timer: Timer? 
private var isShown = false 
private var viewAnimator = UIViewPropertyAnimator.init(duration: 0.5, curve: .linear) 

override func viewDidLoad() { 
    super.viewDidLoad() 
    viewAnimator.addAnimations { 
     self.timerBackground.alpha = 1 
    } 
    viewAnimator.startAnimation() 
    isShown = true 

    self.timer = Timer.scheduledTimer(timeInterval: 0.5, target: self, selector: #selector(self.startReversedAction), userInfo: nil, repeats: true) 
} 

func startReversedAction() { 
    // stop the previous animations block if it did not have time to finish its movement 
    viewAnimator.stopAnimation(true) 
    viewAnimator.addAnimations ({ 
     self.timerBackground.alpha = self.isShown ? 0 : 1 
    }) 
    viewAnimator.startAnimation() 
    isShown = !isShown 
} 

私はiOS 10 Animations demo projectdots jumpingのために非常に類似した挙動を実装しました。

詳細については、自由にご覧ください。

関連する問題