animateWithDuration无效

时间:2012-07-04 14:11:20

标签: objective-c cocoa-touch

我正在尝试使用一些按钮和控制器为我的主视图设置动画,从屏幕右侧进入屏幕。问题是它没有动画,只是直接进入完成状态。

这是我的代码:

UIStoryboard* storyBoard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
options = [storyBoard instantiateViewControllerWithIdentifier:@"OptionsView"];
options.delegate = self;        
[self.view addSubview:options.view];
options.view.center = CGPointMake(floor(total_width+options.view.frame.size.width/2)+10, floor(total_height/2));

[UIView animateWithDuration:0.5
                      delay:0.0
                    options: UIViewAnimationOptionCurveEaseOut
                 animations:^{
                     options.view.center = CGPointMake(floor(total_width-options.view.frame.size.width/2)+10, floor(total_height/2));
                 } 
                 completion:^(BOOL finished){
                     NSLog(@"Done!");
                 }]; 

修改

为了让事情变得更奇怪,如果我把这个动画放在另一个动画的完成块中,那么这个动画可以工作,但不是新动画。

    UIButton* boton = (UIButton*) sender;
    [UIView animateWithDuration:5.0
                          delay:0.0
                        options: UIViewAnimationOptionCurveLinear
                     animations:^{
                         boton.alpha = 0.0;

                         //Jumps directly to 0, animation doesn't work

                     } 
                     completion:^(BOOL finished){
                         [UIView animateWithDuration:0.5
                                               delay:0.0
                                             options: UIViewAnimationOptionCurveEaseInOut
                                          animations:^{
                                              options.view.center = CGPointMake(floor(total_width-options.view.frame.size.width/2)+10, floor(total_height/2));

                                              //It works now, but it doesn't take 5 seconds to start, it starts right away

                                          } 
                                          completion:^(BOOL finished){
                                              NSLog(@"Done!");
                                          }]; 

                     }];

1 个答案:

答案 0 :(得分:3)

animateWithDuration:...调用之前的行直接将center分配给您正在设置的相同值。删除它或在那里分配初始center值。具有相同开始值和结束值的动画显然没有效果。

相关问题