我在Swift iOS应用程序中遇到了一些动画问题。我试图让用户抓住UIImageView并将其拖动(平移)到另一个点。然后,如果他们推动“动画”,它会沿着从第一个点到第二个点的路径显示图像视图的动画。
这是我到目前为止所做的,更重要的是我只是想尽早解决问题。当按下“animate”按钮时,我收到一条错误,上面写着:
CGPathAddLineToPoint(CGMutablePathRef, const CGAffineTransform *,
CGFloat, CGFloat): no current point.
这是我的代码:
// There was some global stuff set up earlier, such as pathPlayer1 which is an
// array of CGPoints I am using to store the path; they are commented
override func viewDidLoad() {
super.viewDidLoad()
var panRecognizer1 = UIPanGestureRecognizer(target: self, action: "handlePanning1:")
playerWithBall.addGestureRecognizer(panRecognizer1)
pathPlayer1.append(playerWithBall.center)
}
func handlePanning1(recognizer: UIPanGestureRecognizer) {
var newTranslation: CGPoint = recognizer.translationInView(playerWithBall)
recognizer.view?.transform = CGAffineTransformMakeTranslation(lastTranslation1.x + newTranslation.x, lastTranslation1.y + newTranslation.y)
if recognizer.state == UIGestureRecognizerState.Ended {
// lastTranslation1 is a global
lastTranslation1.x += newTranslation.x
lastTranslation1.y += newTranslation.y
// another global to get the translation from imageview center in main view
// to the new point in main view
playerWithBallPos.x = playerWithBall.center.x + lastTranslation1.x
playerWithBallPos.y = playerWithBall.center.y + lastTranslation1.y
// add this point to the path to animate along
pathPlayer1.append(playerWithBallPos)
//This was to make sure the append was working
println(pathPlayer1)
}
}
@IBAction func animatePlay(sender: UIButton) {
var path = CGPathCreateMutable()
var i: Int = 0
for (i = 0; i < pathPlayer1.count; i++) {
var location: CGPoint! = pathPlayer1[i]
// I think if its the first time you need to call CGPathMoveToPoint?
if firstTime {
CGPathMoveToPoint(path, nil, location.x, location.y)
firstTime = false
} else {
CGPathAddLineToPoint(path, nil, location.x, location.y)
}
}
var pathAnimation: CAKeyframeAnimation = CAKeyframeAnimation(keyPath: "pos")
pathAnimation.path = path
pathAnimation.duration = 1.0
}
平移工作得很好,似乎正确地得到了新的观点,但我没有使用Objective-c和计算机科学课的经验,这些课程值得了解swift / iOS并且我不熟悉这些类型动画。
我想让我的解决方案工作,以便我可以将它从单个图像视图扩展到多个并同时为每个图像视图设置动画(想想一个体育剧本和动画剧或类似的东西)