首页 > 解决方案 > 使用 Xcode 11.4.1,为什么这些错误显示在动画部分?

问题描述

为什么当我尝试在handleTapAnimation()内部调用动画函数时会出现这些错误viewDidLoad()?我在代码片段下方提供了一个相关的屏幕截图。

作为参考,我使用 Xcode 11.4.1 作为我的 IDE。

import UIKit

class ViewController: UIViewController {

  // Other methods

  override func viewDidLoad() {
    super.viewDidLoad()

    setupLabel()
    setupStackView()

    //animation
    view.addGestureRecognizer(UITapGestureRecognizer(target: self,
                                                     action: #selector(handleTapAinmation)))

  }
  @objc fileprivate func handleTapAnimation() {
    print ("Animating")

    UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 0.5, options: .curveEaseOut, animations: {


      self.titleLabel.transform = CGAffineTransform(translationX: -30, y: 0)


    }) { (_) in
      UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .curveEaseOut, animations: {

        self.titleLabel.alpha = 0
        self.titleLabel.transform =
        self.titleLabel.transform.translatedBy(x: 0,y: -200)

      })
    }

    UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 0.5, options: .curveEaseOut, animations: {

      self.titleLabel.transform = CGAffineTransform(translationX: -30, y: 0)

    }) { (_) in
      UIView.animate(withDuration: 0.5, delay: 0.5, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .curveEaseOut, animations: {

        self.bodyLabel.alpha = 0
        self.bodyLabel.transform =
          self.bodyLabel.transform.translatedBy(x: 0,y: -200)

      })
    }
  }

错误照片

任何帮助深表感谢。

标签: swiftxcodeuikit

解决方案


只需复制第 46 行并将其粘贴到 viewdidload() 并将 handleTapAnimation() 函数放入 viewcontroller 类,因为您将它写在 viewcontroller 之外


推荐阅读