I saw some material on the web, but still can't quite get to where I want. I need to animate my view downwards, making it's height bigger.
Here is my code so far. What's happening here, is that instead of my view resizing, it's just changing it's location a bit up. If I change the proprty instead of "bounds.size" to be "transform.scale.y", it's a little better, only this time it expands the view both up and down, not just downwards.
Another thing I'm not of : are these keys just CALayer properties ? where I can find a list of these keys?
I would really appreciate help here. Thanks!
int x = self.btnHead.frame.origin.x;
int y = self.btnHead.frame.origin.y;
int height = self.btnHead.frame.size.height;
int width = self.btnHead.frame.size.width;
CABasicAnimation *resizeAnimation = [CABasicAnimation animationWithKeyPath:@"bounds.size"];
[resizeAnimation setToValue:[NSValue valueWithCGSize:CGSizeMake(width,height+50)]];
resizeAnimation.fillMode = kCAFillModeForwards;
resizeAnimation.removedOnCompletion = NO;
CAAnimationGroup *animationGroup = [CAAnimationGroup animation];
animationGroup.animations = [NSArray arrayWithObjects:resizeAnimation,nil];
animationGroup.removedOnCompletion = NO;
animationGroup.fillMode = kCAFillModeForwards;
animationGroup.removedOnCompletion=NO;
animationGroup.duration = 0.1;
[self.btnHead.layer addAnimation:animationGroup forKey:@"animations"];
Edit: posting my first code as requested - This will just change my view size, but won't animate , no matter the duration I enter.
int x = self.btnHead.frame.origin.x;
int y = self.btnHead.frame.origin.y;
int height = self.btnHead.frame.size.height;
int width = self.btnHead.frame.size.width;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:10];
CGRect rect = CGRectMake(x,y,width ,height+BUTTON_HEIGH*2);
self.btnHead.frame = rect;
[UIView commitAnimations];
Can't you just change the frame within a UIView animation block. It looks like that CABasicAnimation is not required in this case.
clipsToBounds is the solution.
Explanation: clipToBounds is a property that specifies if subviews should be cropped or not. So if a subview is bigger than its superview it will be cropped if you set
"superview.clipToBounds=YES"
. In the case of this post the superview WAS resized but there was no visible difference for the user, because subviews were drawn even if they were outside of the frame.It's a little bit tricky, because there is no visible link with the animation, but that's why THIS property makes the difference.
Animating the
frame
property can be problematic, but it should work correctly as long as thetransform
is the identity (that is, you haven't changed it). I just tested the following code, and it worked as expected:Again, this won't work as expected if you've changed the
transform
property at any time. For a more bulletproof version of this animation, you need to animate thebounds
property like you tried to with Core Animation. You still don't need to drop into Core Animation, though.The important thing to understand here is the relationship between the
bounds
andcenter
properties. All affine transforms (translate, scale or rotate) need a reference point about which to perform the transform. In the case of aUIView
that is thecenter
point.CALayer
lets you move the reference point around via theanchorPoint
property, but we don't need to worry about that in this case.The reason the view scales in both directions is that the scaling operation happens around the
center
. The easiest thing to to then, is to move thecenter
y by half the height delta. Like this:The final consideration is forcing the view to redraw when the
bounds
changes. For performance reasons, the view will not redraw when you change itsbounds
. However, it does redraw when you change theframe
property (yet another quirk of theframe
property). To force a redraw onbounds
change, you need to set thecontentMode
like I did in the first line of the previous snippet. You probably want to set it back after the animation finishes, but that's up to you. This is also the reason your Core Animation version only changes the location and not the size. You need this line in your code somewhere before you run the animation:I hope this all makes sense. The
frame
,bounds
andcenter
properties can be a little confusing at first.Solved the issue. Not sure exacly why.