1

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)
Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Alex Bailey
  • 793
  • 9
  • 20

2 Answers2

6

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
Community
  • 1
  • 1
Emptyless
  • 2,964
  • 3
  • 20
  • 30
1

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.

Benjamin Lowry
  • 3,730
  • 1
  • 23
  • 27