In Swift (iOS) is there a way to get a UIImageView location during an animation of that object?

Question

I'm still learning the basics of swift so please bare with me.

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)

Show source
| xcode   | swift   | ios   | uiimageview   | frame   2017-01-07 12:01 2 Answers

Answers to In Swift (iOS) is there a way to get a UIImageView location during an animation of that object? ( 2 )

  1. 2017-01-07 12:01

    http://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
    
  2. 2017-01-07 12:01

    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.

Leave a reply to - In Swift (iOS) is there a way to get a UIImageView location during an animation of that object?

◀ Go back