我对swift相对较新,而且我正在开发一款游戏,当点击屏幕上的随机位置时,UIView会移动。问题是我每次尝试在执行UIView的所有动画的IBAction中添加新代码 它弄乱了动画。 这是我的相关代码:
import UIKit
class ViewController: UIViewController {
@IBOutlet var bottomConstraint: UIView!
@IBOutlet var topConstraint: UIView!
@IBOutlet var square: UIView!
@IBOutlet var SecondsScore: UILabel!
var timer: NSTimer!
var incrementA = 0.0
//====================FunctionA========================//
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
timer?.invalidate()
}
//======================Actions========================//
@IBAction func squareTapped(tapped: UITapGestureRecognizer) {
SecondsScore.text = "\(++incrementC)" //HERE IS THE CODE I ADD AND IT CHANGES THE ANIMATION OF THE UIVIEW
animateSlide()
timeIntervalForSquare()
}
@IBAction func Button(sender: UIButton) {
timer?.invalidate()
}
//=====================FunctionB=========================//
func rand() {
var yaran = UInt32(bottomConstraint.center.y) - 40
var xaran = UInt32(topConstraint.center.x) - 40
square.center.y = CGFloat(arc4random_uniform(yaran)) + 30
square.center.x = CGFloat(arc4random_uniform(xaran)) + 30
}
func timeUpSegue() {
performSegueWithIdentifier("GameOver", sender: nil)
println("Segue: Game Over")
}
func timeIntervalForSquare() {
timer = NSTimer.scheduledTimerWithTimeInterval(2, target: self, selector: Selector("timeUpSegue"), userInfo: nil, repeats: false)
}
func animateSlide() {
UIView.animateWithDuration(0.7, animations: {self.rand()}, completion: nil)
}
}
UIView" Square"是动画的正方形。 如果有人可以将这些代码插入到Xcode中(有或没有那行代码我旁边有评论),看看有什么不对,我会非常感激。
如果代码有点乱,我道歉,我会尽力保持井井有条。
感谢所有能帮助我的人!