0

I have an image (innerCircle) that is growing and shrinking in scale with an animation using the following:

UIView.animateWithDuration(5, delay:0, options: [.Repeat, .Autoreverse], animations: { () -> Void in
        self.innerCircle.transform = CGAffineTransformMakeScale(3.5, 3.5)
    }) { (finished: Bool) -> Void in
        UIView.animateWithDuration(1, animations: { () -> Void in
            self.innerCircle.transform = CGAffineTransformIdentity

I am trying to get the current size of the image at any point during the animation so that I can check when it grows beyond a certain point. I want to do this so I can change a label of text from "Inhale" to "Exhale" and visa-versa.

I have tried using

let innerCircleWidth = self.innerCircle.image!.size.width

but that only gets the initial width value. It does not update.

Many thanks!

  • You should look at the presentation layer of `innerCircle`. `innerCircle.layer.presentationLayer()?.frame` – beyowulf Sep 06 '16 at 21:14
  • 1
    "I am trying to get the current size of the image at any point during the animation so that I can check when it grows beyond a certain point. I want to do this so I can change a label of text from "Inhale" to "Exhale" and visa-versa" That is totally the wrong approach. You should build the change from Inhale to Exhale _into the animation_ — not try to watch the animation and interfere with it somehow. – matt Sep 06 '16 at 22:21

2 Answers2

1

When using UIView.animateWithDuration to get accurate information about what is currently being displayed you should check the presentationLayer of the view(s) that's animating, in your case innerCircle.layer.presentationLayer()?.frame not the view's frame itself. For more information about this you can see here.

To get a sense for how this works you can place the following code in a swift playground.

import UIKit
import XCPlayground

let outerCircle = UIView(frame: CGRect(x: 0, y: 0, width: 1000, height: 1000))
let innerCircle = UIView(frame: CGRect(x: 0.0, y: 0.0, width: 100, height: 100))
outerCircle.addSubview(innerCircle)
innerCircle.center = outerCircle.center
innerCircle.backgroundColor = UIColor.redColor()
UIView.animateWithDuration(5, delay:0, options: [.Repeat, .Autoreverse], animations: { () -> Void in
    innerCircle.transform = CGAffineTransformMakeScale(3.5, 3.5)
}) { (finished: Bool) -> Void in
    UIView.animateWithDuration(1, animations: { () -> Void in
        innerCircle.transform = CGAffineTransformIdentity
    })
}



class TimerObject: NSObject {
    override init()
    {
        super.init()
        let timer = NSTimer(timeInterval: 0.10, target: self, selector: #selector(self.printPresentationFrame), userInfo: nil, repeats: true)
        NSRunLoop.mainRunLoop().addTimer(timer, forMode: NSDefaultRunLoopMode)
    }

    @objc func printPresentationFrame()
    {
        print(innerCircle.layer.presentationLayer()?.frame)
    }
}
let timerObject = TimerObject()

XCPlaygroundPage.currentPage.liveView = outerCircle
beyowulf
  • 15,101
  • 2
  • 34
  • 40
0

That's because the size of the image did not change. Applying a transform does not change the size of a view.

Kyle Redfearn
  • 2,172
  • 16
  • 34
  • Oh! Thank you very much, I am still new to this. Is there a way to get the current scale of the view so that I can accomplish what I am trying to do? – Collin Stover Sep 06 '16 at 20:59
  • Probably this would work: http://stackoverflow.com/questions/2694785/get-size-of-uiview-after-applying-cgaffinetransform – Kyle Redfearn Sep 06 '16 at 21:15