Can’t add a corner radius and a shadow

Yes, yes there is… If you want both a corner radius and a drop shadow, you don’t turn on -masksToBounds, but rather set the corner radius and set the bezier path of the shadow with a rounded rect. Keep the radius of the two the same: [layer setShadowOffset:CGSizeMake(0, 3)]; [layer setShadowOpacity:0.4]; [layer setShadowRadius:3.0f]; [layer setShouldRasterize:YES]; … Read more

Best Way to Perform Several Sequential UIView Animations?

If you’re willing to step up to iOS 4.0, the new blocks-based animation approach can make this animation chaining trivial. For example: [UIView animateWithDuration:1.0 animations:^{ view.position = CGPointMake(0.0f, 0.0f); } completion:^(BOOL finished){ [UIView animateWithDuration:0.2 animations:^{ view.alpha = 0.0; } completion:^(BOOL finished){ [view removeFromSuperview]; }]; }] will cause view to animate to (0, 0) over a … Read more

How to animate the frame of an layer with CABasicAnimation?

The frame property of a CALayer is a derived property, dependent on the position, anchorPoint, bounds and transform of the layer. Instead of animating the frame, you should instead animate the position or bounds, depending on what effect you are trying to accomplish. To move a layer, you can animate the position: -(void)moveLayer:(CALayer*)layer to:(CGPoint)point { … Read more

When exactly do implicit animations take place in iOS?

UIKit disables implicit animations. To be more specific, a CALayer associated with a UIView will never implicitly animate. CALayers that you create yourself and that are not associated with a UIView will buy into the normal implicit animation machinery. If you’re interested in how this works, implicit animations happen after the normal -actionForKey: lookup. If … Read more

How do you explicitly animate a CALayer’s backgroundColor?

You don’t need to wrap CGColorRefs when setting the toValue or fromValue properties of a CABasicAnimation. Simply use the CGColorRef. To avoid the compiler warning, you can cast the CGColorRef to an id. In my sample app, the following code animated the background to red. CABasicAnimation* selectionAnimation = [CABasicAnimation animationWithKeyPath:@”backgroundColor”]; selectionAnimation.toValue = (id)[UIColor redColor].CGColor; [self.view.layer … Read more

Chaining Core Animation animations

You can also use animation grouping and use the beginTime field of the animation. Try something like this: CABasicAnimation *posAnimation = [CABasicAnimation animationWithKeyPath:@”position”]; [posAnimation setFromValue:[NSNumber numberWithFloat:0.0]]; [posAnimation setToValue:[NSNumber numberWithFloat:1.0]]; // Here’s the important part [posAnimation setDuration:10.0]; [posAnimation setBeginTime:0.0]; CABasicAnimation *borderWidthAnimation = [CABasicAnimation animationWithKeyPath:@”borderWidth”]; [borderWidthAnimation setFromValue:[NSNumber numberWithFloat:0.0]]; [borderWidthAnimation setToValue:[NSNumber numberWithFloat:1.0]]; // Here’s the important part [borderWidthAnimation … Read more