Alex Bailey
Alex Bailey

Reputation: 793

Is there a way to get a UIImageView location during an animation of that object?

I have a function that animates the movement of a UIImageView from one set of coordinates to another on the opposite side of the screen. During the animation I want to document where the image is when I tap on it. When I make a call like so I get the destination location not the current location:

var rippleImage = UIImageView()

//example animation block
UIView.animate(withDuration: 6, delay: 0.0, options: [.curveLinear, .allowUserInteraction], animations: {
        self.rippleImage.frame = CGRect(x: (xaxis - 500) , y: (yaxis - 500) , width: (self.rippleImage.frame.width + 1000), height: (self.rippleImage.frame.height + 1000))
        self.rippleImage.alpha = 0.1
    }, completion: nil )

//sample button click function to capture location
@objc func pianoKeyButtonTapped(sender: UIImageView) -> [String] {

    // get and animate current ripple
    let currentRippleLocation = self.rippleImage.frame
    print(currentRippleLocation)
}

In every case I get the same destination location like so:

(-342.5, 67.0, 1060.0, 1060.0)

Upvotes: 1

Views: 1256

Answers (2)

Benjamin Lowry
Benjamin Lowry

Reputation: 3789

You'll have to adjust my answer to your specific coordinates/size that you want to change, but I think this solution should work for you:

UIView.animate(withDuration: 0.1, delay: 0.0, options: [.curveLinear, .allowUserInteraction], animations: {
    UIView.setAnimationRepeatCount(100) //however many times you want
    self.rippleImage.frame.origin.x += 1 //example animation
    self.rippleImage.alpha -= 0.9/100 //you can adjust the variables to change incrementally 
}, completion: nil )

This way the frame that you receive should be more or less accurate.

Upvotes: 1

Emptyless
Emptyless

Reputation: 3050

https://stackoverflow.com/a/7625335/4503593 Converted to Swift 3 code:

the presentationLayer - a property of the CALayer that "provides a close approximation to the version of the layer that is currently being displayed"

let currentRippleLocation = rippleImage.layer.presentation().frame

Upvotes: 6

Related Questions