0
votes

I have a UIImageView that contains an animated picture (sequence of pictures) i'm trying to move the UIImageView from its current position to the right of the screen (from middle to right of the screen) to create a running effect of the character

when i press the button that i want to trigger the animation the image suddenly disappear then get animated from the left side off the screen to the middle. I have no idea why the UIImageView is being moved off the screen (left side)

this is my code

@IBAction func correctButtonPressed(sender: AnyObject) {
        if isTrue {
            winAudio.play()
            playRunAnimation()
            UIView.animateWithDuration(3, animations: { () -> Void in
                var frame = self.animatedImage.frame
                frame.origin.x += (self.animatedImage.superview?.frame.width)!
                self.animatedImage.frame = frame
            })
        }
    }

EDIT :

this is how i animate my UIImageView

func playIdelAnimation() {
        self.image = UIImage(named: "Idel1.png")
        var imageArray = [UIImage]()
        for i in 1...10 {
            let str = "Idle\(i).png"
            let imageName = UIImage(named: str)!
            imageArray.append(imageName)
        }
        self.animationImages = imageArray
        self.animationDuration = 0.5
        self.startAnimating()
    }

self.image = UIImage(named: "Idel1.png") is to set a default picture for the UIImageView then im creating an array of UIImage to animate it in the UIImageView

using (self) because i have a custom class for the UIImageView

1
Do you use some constraints on the animatedImage ?Jonas
yes i did use some constraintsAli Alebrahim
They could cause the problem because if you change the origin of the image the constraints are still there and want to keep the image at its position.Jonas
ok thanks i will try to remove them and place the UIImageView programmaticallyAli Alebrahim
no i don't think thats the best solution. Just change the constraints in your animation too.Jonas

1 Answers

0
votes

First you need a reference to the constraints of the imageview from your interfacebuilder (Don't forget to connect the outlet in the interfacebuilder with the constraints):

 @IBOutlet weak var leftConstraint: NSLayoutConstraint!
 @IBOutlet weak var rightConstraint: NSLayoutConstraint!

I assume your image has a leftConstraint.constant = 0 and rightConstraint.constant = self.view.frame.width

Then you animate with:

self.leftConstraint.constant = self.view.frame.width
self.rightConstraint.constant = self.view.frame.width + self.animatedImage.frame.width

UIView.animateWithDuration(3.0) {
    self.view.layoutIfNeeded()
}

I hope this helps. Good luck.