我创建了一个简单的动画,显示出一枚硬币在跳动,并且效果很好,直到在故事板上带有动画的那个之前添加了viewcontroller。
我希望在显示带有此动画的屏幕之前在游戏中拥有菜单屏幕。
这是动画的代码:
let imageView: UIImageView = coinImageView[0]
UIView.animate(withDuration: 0.5,
delay: 0.1,
options: [
.repeat,
.autoreverse,
.curveEaseIn,
.allowUserInteraction
],
animations: {
imageView.transform = CGAffineTransform(scaleX: 1.5, y: 1.5)
}
)
第一个视图(带有菜单)如下:
class MenuViewController: UIViewController {
@IBAction func newGameButton(_ sender: Any) {
let viewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "ViewController") as! ViewController
self.present(viewController, animated: true, completion: nil)
viewController.newGame()
}
override func viewDidLoad() {
super.viewDidLoad()
}
}
和第二个viewcontroller(带有动画的一个)如下:
class ViewController: UIViewController, SavingViewControllerDelegate, UICollectionViewDelegate, UICollectionViewDataSource {
var coinImageView: [UIImageView] = []
func grantCoins(n: Int) {
let coinCount = coin.count
for i in coinCount...(coinCount+n) {
coin.append(UIImage(named: "coin.png")!)
coinImageView.append(UIImageView(image: coin[i]))
coinImageView[i].isUserInteractionEnabled = true
coinImageView[i].addGestureRecognizer(UIPanGestureRecognizer(target: self, action: #selector(self.handlePan(_:))))
coinImageView[i].frame = CGRect(x: 147, y: -100, width: 60, height: 60)
view.addSubview(coinImageView[i])
let finalPoint = CGPoint(
x: 177,
y: 278
)
generateNewCoin(gestureView: coinImageView[i])
}
view.bringSubviewToFront(coinImageView[0])
}
func newGame() {
animateFirstCoin()
}
func animateFirstCoin() {
let imageView: UIImageView = coinImageView[0]
UIView.animate(
withDuration: 0.5,
delay: 0.1,
options: [.repeat, .autoreverse, .curveEaseIn, .allowUserInteraction],
animations: {
imageView.transform = CGAffineTransform(scaleX: 1.5, y: 1.5)
})
}
}
这个问题有明显的解释吗?
答案 0 :(得分:0)
经过一天没有成果的研究,我终于找到了它。 生成第一个硬币时,该视图尚不可见,因此动画无法正常工作。另一批硬币动画效果很好。 我只是将此代码放在函数中以进行检查:
if (view.window == nil) {
print("view is not visible")
} else {
print("view is visible")
}
谢谢