Jonathan.
Jonathan.

Reputation: 55604

Animate size of a UIView around center point

I have a UIView which I want to grow from it's center when a user touches it. The problem is that when animating it the view expands left and then moves to the right, whereas I want it to expand to the left and right, while keeping the center point the same.

This is the code I have at the moment:

[UIView animateWithDuration:1 animations:^(void) {
    [view setFrame:CGRectMake(-10, 0, 320, view.frame.size.height)];
}];

I didn't think it was going to be difficult to do this, but it seems it is. Short of animating it manually with a timer I have no idea how to get it to expand from it's center.

Upvotes: 8

Views: 9892

Answers (4)

curiousDog
curiousDog

Reputation: 81

instantiate the subview.center's to the parent's view.centerer, it solves the problem for me.

childView.center = view.center

Upvotes: 0

Jesse Head
Jesse Head

Reputation: 598

Just offset the X,Y position of the view. Lets say you're DECREASING both the width and height of the frame by 20 points: you should then ADD 10 points to both the X and Y position of the frame.

Upvotes: 0

Prabhu Natarajan
Prabhu Natarajan

Reputation: 869

Try the code given by @Deepak Danduprolu and also don't forgot to UnCheck the "Use AutoLayout" checkbox in the show file inspector window of the storyboard. It works for me.

Upvotes: 0

Deepak Danduprolu
Deepak Danduprolu

Reputation: 44633

I am not quite sure why it's working for you in a weird manner. Did you alter the anchorPoint property in any way? Otherwise it should grow from center.

Does doing

CGRect newFrame;
newFrame = CGRectInset(view.frame, -10, -30);

[UIView animateWithDuration:1 animations:^(void) {
    view.frame = newFrame;
}];

also give you the same result? What about this?

[UIView animateWithDuration:1 animations:^(void) {
    view.transform = CGAffineTransformScale(view.transform, 1.1, 1.1);
}];

Upvotes: 9

Related Questions