How to animate the translation of an UIImageView that has been dragged from one point to another?

240 views Asked by At

I'm having an issue with some animations in a Swift iOS application. I am trying to allow a user to grab a UIImageView and drag (pan) it to a different point. Then if they push "animate" it shows the animation of the imageview along a path from the first point to the second.

Here is what I have so far, which is more so me just trying to hammer an early solution. I'm getting an error when the "animate" button is pressed that says:

CGPathAddLineToPoint(CGMutablePathRef, const CGAffineTransform *, 
                     CGFloat, CGFloat): no current point.

Here is my code:

// 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
}

The panning is working just fine and it appears to be correctly getting the new point, but I have no experience using objective-c and a computer science class worth of knowledge of swift/iOS and I'm not familiar with these types of animations.

I would like to make my solution work so that I could extend it from a single imageview to multiple and animate each one simultaneously (think of a sports playbook and animating a play or something like that)

0

There are 0 answers