Cannot animate view with CATiledLayer correctly - iphone

Can UIViews with a CATiledLayer have animations?
If I call the following:
[myContentView setNeedsDisplay];
CGContextRef context = UIGraphicsGetCurrentContext();
[UIView beginAnimations:nil context:context];
[UIView setAnimationTransition: UIViewAnimationTransitionCurlUp forView:self.view cache:YES];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationDuration:10];
[UIView commitAnimations];
This will work well if the UIView is a "normal" UIView without a CATiledLayer. However as soon as I add a sublayer of type CATiledLayer, the animations does not work correctly.
The page flip works, but the underlying page is not shown (just blank) until the animation is finished.
I have tried [myContentView setNeedsDisplay]; before, after and even in the middle of the animation block. No difference if myContentView has a CATiledLayer.
Any help appreciated.

Figured it out:
cache:YES];
Set to NO

Related

UIView animation is leaking

In my viewcontroller i am using animation for changing the frame of UIButton and UIView, going from portrait to landscape user can see views growing but problem is animation is leaking everywhere and showing everything coming from different sides.
Here is the code
[UIView beginAnimations:Nil context:Nil];
[UIView setanimationDuration:1];
[view1 setFrame:CGRectMake(100,100,200,300)];
[UIView commitAnimations];
Thanks
Please elaborate more on what "leaking everywhere" means, and does it make a difference if you use block animation:
[UIView animateWithDuration:1.0 delay:0.0 options:nil animations:^{
[view1 setFrame:CGRectMake(100,100,200,300)];
}completion:^(BOOL done){
if (done) {
NSLog(#"animation complete");
}
}];
According to Apple's documentation:
In iOS 4 and later, use the block-based animation methods.
(Recommended)

UIView Animation, Transforming layer.shadowPath

I created a UIImageView that has a custom shadowPath. This view will be moved left and right across the screen, but I need the shadowPath to have a different transformation than the image layer itself. Is this possible? Moving the image itself works, and the shadow moves as well when I do the following:
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
im.center = location;
[UIView commitAnimations];
But if I try to add a layer transformation before "commitAnimations", the shadow will simply take on that property right away, for example:
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:5];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
im.center = location;
im.layer.shadowPath = [self createPerspectiveShadow:im]; //this will not animate
[UIView commitAnimations];
So, my question is, is there a way to recalculate a shadow during the animation?
Thanks
The UIView animations don’t always allow you to animate CALayer attributes; try using a CABasicAnimation instead.
You can pass a CGPathREf to the toValue
theAnimation.toValue = (id)[UIBezierPath bezierPathWithRect:newRect].CGPath;
see this post

Why new UIImageView can not Animating?

I have some code in my iPhone app like that :
//fromView is a UIImageView.
//self is a UIView.
UIGraphicsBeginImageContext(fromView.bounds.size);
[fromView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *dummyFromImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageView* dummyImageView = [[UIImageView alloc] initWithImage:dummyFromImage];
[self addSubview:dummyImageView];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView: dummyImageView cache:YES]; //line:9
[UIView commitAnimations];
The dummyImageView only show but never flip, if you change line9's dummyImageView to fromView, fromView do flip, Please tell me why?
I ask this question to Apple Developer Technical Support, they said,
"The basic problem is that because of the timing of your animation there is no "previous" state for Core Animation to animate from, as the view was just added to the view hierarchy, and so when the transition is attempted, all it can do is display the "final" state.
If you instead perform the flip on the next runloop iteration, then Core Animation will have had time to create an initial state for the view's layer, and thus the flip will occur correctly. You can do this by splitting your flip method in two and using -performSelector:withObject:afterDelay: like so:
"
-(IBAction)flip {
UIImageView* dummyImageView = [[UIImageView alloc] initWithImage:fromView.image];
dummyImageView.frame = fromView.frame;
[window addSubview:dummyImageView];
[self performSelector:#selector(animate:) withObject:dummyImageView afterDelay:0.0];
}
-(void)animate:(UIView*)view {
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView: view cache:YES];
[UIView commitAnimations];
}
However, since you mention that "fromView" is a UIImageView, I wonder why you are using -renderInContext: – it is more efficient to simply use the same image that "fromView" is using and assign it as the image for your new UIImageView, as this saves both CPU time and memory, especially since I notice in your sample that the image is smaller than the view you are using.
[UIView setAnimationTransition:… forView: dummyImageView cache:YES]; //line:9
The view in the -setAnimationTransition:… method should be assigned to the view that contains the change. In your case, self.
The dummyImageView itself is not changed (exterior changes such as changing superview is irrelevant), so the animation can do nothing.

how to implement an iPhone view transition animation with both flipping and scaling?

how can I implement the animation we see in the iPhone Music app's coverflow screen? when you click on a small view, it flips and scales up to another view? how can I do this? I can use core animation to flip and scale a view, but how can I do the transition to another view? thanks
You need an UIView as Container for the two UIViews (frontside/backside) and then remove/add these from/to the container as subviews while doing the animations in between:
UIView *flipContainer;
UIView *frontSide;
UIView *backSide;
//...
-(void)turnUp
{
[backSide removeFromSuperview];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1.0];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:flipContainer cache:YES];
[UIView setAnimationDuration:1.0];
CGAffineTransform transform = CGAffineTransformMakeScale(1.2, 1.2);
flipContainer.transform = transform;
[UIView commitAnimations];
[flipContainer addSubview:frontSide];
}
-(void)turnDown
{
[frontSide removeFromSuperview];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1.0];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:flipContainer cache:YES];
[UIView setAnimationDuration:1.0];
CGAffineTransform transform = CGAffineTransformMakeScale(1, 1);
flipContainer.transform = transform;
[UIView commitAnimations];
[flipContainer addSubview:backSide];
}
I'm trying the exact code you are doing - I get a zoom effect but no turn over. The only difference is that right before the turnUp code I add the flipContainer (with back showing) so then it can be flipped over.
// construct animation container
self.flipContainer = [[FlipContainer alloc] init];
[self.flipContainer.view setFrame:CGRectMake(clickedSquareX, clickedSquareY, 200, 200)];
[self.flipContainer.view addSubview:self.backside.view];
// add animation container
[self.myParentView.view addSubview:self.flipContainer.view];
// PROCEED to your turnUp code
The reason I'm doing this is I have a bunch of images in a horizontal UIScrollView and so to 'simulate' a 200x200 image flipping over and zooming to show detail I add my flipContainer with the backside showing the exact image over the exact spot of the pressed image. It should work shouldn't it? A bit confusing to me is the first line of your turnUp code you do:
[backSide removeFromSuperview];
..which would remove the view I just added.
I'm not sure if this is the right spot to put this question in - sorry if it isn't!

iPhone: transitioning views with Core Animation

On the Mac, the best way for a simple cross-fade transition of views (without any custom keyframe timing) is to do something such as the following excerpt:
[[self animator] replaceSubview:aView with:bView];
Unfortunately the animator property isn't available on the iPhone. What's the best bet of doing this on the iPhone? Is it setting alpha channels on each view? Sample code would be excellent.
Thanks.
The basic way to animate views on the iphone is to use the UIView beginAnimations and commitAnimations calls. They allow you to modify the animatable properties of a view and have those changes animated.
For instance I have a custom view that is hidden and shown using this approach:
- (void) showAView:(CustomAView *)aView
{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
aView.frame = CGRectMake(0.0f, 110.0f , aView.frame.size.width, aView.frame.size.height);
[UIView commitAnimations];
}
- (void) hideAView:(CustomAView *)aView
{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
aView.frame = CGRectMake(0.0f, self.view.frame.size.height, aView.frame.size.width, aView.frame.size.height);
[UIView commitAnimations];
}
By wrapping the frame property change in the UIView beginAnimations/commitAnimations the change has a standard animation applied to it.
You can add additional properties to the animation by using UIView animation class methods eg.
[UIView setAnimationTransition:UIViewAnimationTransitionCurlDown forView:self.view cache:YES];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];