iOS 4.2:使用块动画翻转图像

13
我在我的应用程序中有这段代码:
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:imgView cache:YES];
    imgView.image = img2;
[UIView commitAnimations];

但在iOS 4.0及更高版本中不鼓励使用这种方法,应该使用transitionWithView:duration:options:animations:completion:

我无法使其正常工作。有人能帮我吗? 谢谢!

2个回答

19
[UIView transitionWithView:imgView    // use the forView: argument
                  duration:1          // use the setAnimationDuration: argument
                   options:UIViewAnimationOptionTransitionFlipFromLeft 
                          // check UIViewAnimationOptions for what options you can use
                animations:^{         // put the animation block here
                              imgView.image = img2;
                           }
                completion:NULL];     // nothing to do after animation ends.

@KennyTM:你能否澄清在块中使用NULL vs nil的区别吗?(链接:https://dev59.com/bG025IYBdhLWcg3w1ZoL) - penfold

3

我根据您的代码编写了此函数:

- (void)flipCurrentView:(UIView*)oldView withNewView:(UIView*)newView reverse:(BOOL)reverse
{
    newView.alpha = 0;
    [UIView transitionWithView:newView
                      duration:1      
                       options:UIViewAnimationOptionTransitionFlipFromLeft 
                    animations:^{        
                        oldView.alpha = 0;
                        newView.alpha = 1;
                    }
                    completion:^(BOOL finished){
                        if(reverse){
                            [self flipCurrentView:newView withNewView:oldView reverse:NO];
                        }
                        finished = YES;
                    }];   
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接