Ios 在情节提要中添加第二个viewcontroller时,uiview.animate停止工作
我已经创建了一个简单的动画,显示了一个硬币的脉动,它工作得非常好,直到我在故事板上的动画之前添加了viewcontroller 在用这个动画显示屏幕之前,我想在我的游戏中有一个菜单屏幕 这是动画的代码:Ios 在情节提要中添加第二个viewcontroller时,uiview.animate停止工作,ios,swift,Ios,Swift,我已经创建了一个简单的动画,显示了一个硬币的脉动,它工作得非常好,直到我在故事板上的动画之前添加了viewcontroller 在用这个动画显示屏幕之前,我想在我的游戏中有一个菜单屏幕 这是动画的代码: 让imageView:UIImageView=coinImageView[0] UIView.animate(持续时间:0.5, 延迟:0.1, 选项:[ .重复一遍, .自动翻转, .curveEaseIn, .allowUserInteraction ], 动画:{ imageView.t
让imageView:UIImageView=coinImageView[0]
UIView.animate(持续时间:0.5,
延迟:0.1,
选项:[
.重复一遍,
.自动翻转,
.curveEaseIn,
.allowUserInteraction
],
动画:{
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()
}
}
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)
})
}
}
第二个viewcontroller(带有动画的)如下所示:
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()
}
}
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)
})
}
}
这个问题有什么明显的解释吗?经过一天毫无结果的研究,我终于找到了答案。 当第一个硬币生成时,视图还不可见,因为动画不起作用。另一批硬币很好。 我只是在函数中输入以下代码来检查:
if (view.window == nil) {
print("view is not visible")
} else {
print("view is visible")
}
谢谢从何处填充coinImageView字段?好问题,我已更新了问题描述。