Why can't the new UIImageView animate?
I have a code in my iPhone app:
//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];
DummyImageView only shows but never flips if you change line9 dummyImageView to viewView, fromView do flip please tell me why?
source to share
I am asking this question to Apple Developer tech support, they said,
"The main problem is that due to the timing of your animation, there is no" previous "state for Core Animation for animation, since the view is just added to the view hierarchy and so when you try to navigate, it can all display theβ final βstate.
If you instead flip the next iteration of the runloop, then Core Animation will have time to create an initial state for the view layer, and thus flip correctly. You can do this by splitting your flip method in two and using -performSelector: withObject: afterDelay: like this: "
-(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 am wondering why you are using -renderInContext: - it is more efficient to just use the same image that "fromView" uses and assign it as an image for your new UIImageView, since this is saves both CPU time and memory, especially since I noticed in your example that the image is smaller than the view you are using.
source to share
[UIView setAnimationTransition:β¦ forView: dummyImageView cache:YES]; //line:9
The view in the method -setAnimationTransition:β¦
must be assigned to the view containing the change. In your case self
.
It dummyImageView
doesn't change itself (external changes like supervisor change don't matter), so the animation can't do anything.
source to share