onigunn
onigunn

Reputation: 4778

CoreAnimation keeping track of origin values

I'm facing really often requirement to animate a view and animate it back to its origin value. (Something like zooming in a UIImageView on Tap and back)

Is there a more elegant solution to keep track of the origin values instead of creating class fields and assigning those on viewDidLoad?

How do you guys handle such behaviour?

Upvotes: 0

Views: 88

Answers (2)

Matt Long
Matt Long

Reputation: 24476

If you apply a transform to the view you can just set it back to the identity transform when you're done. This code creates a basic toggle that scales up and down.

// Check to see if the current transform is identity which means
// no transform applied. If it is, scale the view. Otherwise, 
// set it back to identity.
CGFloat scale = 3.0f;
if (CGAffineTransformIsIdentity([_imageView transform])) {
  [_imageView setTransform:CGAffineTransformMakeScale(scale, scale)];
} else {
  [_imageView setTransform:CGAffineTransformIdentity];
}

This keeps you from having to keep track of what the starting value was.

Upvotes: 1

stevekohls
stevekohls

Reputation: 2255

Your solution of keeping track of the initial origin values in the class is exactly the way I do it. No inelegance there, just something you have to do. I'm not aware of any other solution.

Upvotes: 0

Related Questions