开发者

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 all开发者_StackOverflowoc] 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜