简体   繁体   English

Swift:如何在更改 ZD304BA20E96D87411588EEABAC850E3 之前完成 UIView 绘图 animation

[英]Swift: how to finish a UIView draw animation before changing a label

I have a little problem regarding the timing of an animation.关于 animation 的时序,我有一点问题。

The sequence should be:顺序应该是:

  1. Button is pressed按钮被按下
  2. Animation is displayed显示Animation
  3. Label is changed Label改

In reality the sequence is:实际上顺序是:

  1. Button is pressed按钮被按下
  2. Label is changed Label改
  3. Animation is displayed显示Animation

Here is the code of the main VC:下面是主VC的代码:

import Foundation
import UIKit

class MainVC: UIViewController {

  var counter: Int = 0

  @IBOutlet weak var counterLabel: UILabel!
  
  @IBOutlet weak var nextButton: UIButton!

  @IBOutlet weak var animationView: AnimationView!

  @IBAction func nextButtonPressed(_ sender: UIButton) {
     counter += 1
     animationView.progress = 1
     //this view draws a progress bar and animates it from 0 to 100% via CABasicAnimation. 
     counterLabel.text = "\(counter)"
     animationView.progress = 0 
     //the progress bar is reset
  }

  override func viewDidLoad() {
     super.viewDidLoad()
     nextButton.setTitle("Next")
     counterLabel.text = "\(counter)"
  }

}

I've experimented with the dispatch queue, but I just can't get it to work right.我已经尝试过调度队列,但我无法让它正常工作。 Any ideas on how to solve this?关于如何解决这个问题的任何想法?

You don't show enough of your code for us to be able to help you specifically.您没有显示足够多的代码,我们无法专门为您提供帮助。

In general, you would set some object to be the delegate of your CAAnimation, and implement the method func animationDidStop(_ anim: CAAnimation, finished flag: Bool) .一般来说,你会设置一些 object 作为你的 CAAnimation 的代理,并实现方法func animationDidStop(_ anim: CAAnimation, finished flag: Bool)

In that method you'd then change the label text (assuming finished == true .)在该方法中,您将更改 label 文本(假设finished == true 。)

I don't really like the delegate pattern for animations.我不太喜欢动画的委托模式。 If you have multiple animations it can be painful to handle custom completion code for each one.如果您有多个动画,为每个动画处理自定义完成代码可能会很痛苦。 What I've done is use the fact that you can attach objects to CAAnimation objects, and attach a closure to the animation.我所做的是使用您可以将对象附加到 CAAnimation 对象,并将闭包附加到 animation 的事实。 I then set the view controller to be the delegate, and in the view controller's animationDidStop() , I look for a closure attached to the animation, and execute it if there is one.然后我将视图 controller 设置为委托,并在视图控制器的animationDidStop()中,查找附加到 animation 的闭包,如果有,则执行它。

Edit:编辑:

See this article for a discussion on using key-value coding to attach objects to CAAnimation and CALayer objects:有关使用键值编码将对象附加到CAAnimationCALayer对象的讨论,请参阅这篇文章:

Core Animation Key-value coding extensions 核心 Animation 键值编码扩展

It's old, and written using Objective-C, but the concepts are the same in Swift, and very useful.它很旧,使用 Objective-C 编写,但 Swift 中的概念相同,非常有用。 For a more general discussion on key-value coding, which is updated for Swift, see this article: https://developer.apple.com/documentation/objectivec/nsobject/nskeyvaluecoding有关键值编码的更一般性讨论(针对 Swift进行了更新),请参阅这篇文章: https://developer.apple.com/documentation/objectivec/nsobject/nskeyvaluecoding

(The two functions you need to know in Swift are setValue(_:forKeyPath:) and value(forKey:) .) (Swift中你需要知道的两个函数是setValue(_:forKeyPath:)value(forKey:) 。)

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

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