UIButton动画问题-iOS



我正在尝试制作一个UIButton动画,其中按钮移动到一个点,然后被设置为隐藏为true。然而,当我尝试处理以下代码时,该按钮甚至在动画完成之前就消失了。我做得对吗?有什么建议吗?

[UIView animateWithDuration:0.8
                 animations:^{
                     selectedCurveIndex = 0;
                     [tradebutton moveTo:
                      CGPointMake(51,150) duration:0.8 
                                  option:curveValues[UIViewAnimationOptionCurveEaseInOut]];
                 }
                 completion:^(BOOL finished){ 
                     [tradeButton setHidden:TRUE];
                     UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard_iPhone" bundle:nil];
                     UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"ButtonView"];
                     self.modalPresentationStyle = UIModalPresentationCurrentContext;
                     [self presentModalViewController:vc animated:NO];
                 }];

在继续之前,您需要确保已完成设置为

按钮会快速隐藏,因为0.8是一个快速的动画持续时间。你需要找到另一个地方来隐藏按钮,或者你可以

试试这个:

[UIView animateWithDuration:0.8
                 animations:^{
                     selectedCurveIndex = 0;
                     [tradebutton moveTo:
                      CGPointMake(51,150) duration:0.8 
                                  option:curveValues[UIViewAnimationOptionCurveEaseInOut]];
                 }
                 completion:^(BOOL finished){ 
                     if ( finished ) 
                     {    
                         [tradeButton performSelector:@selector(setHidden:) withObject:@"YES" afterDelay:3.0];
                         UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard_iPhone" bundle:nil];
                         UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"ButtonView"];
                         self.modalPresentationStyle = UIModalPresentationCurrentContext;
                         [self presentModalViewController:vc animated:NO];
                     }
                 }];

问题是在moveTo:duration:option:方法中创建第二个内部动画块,并在该内部块中设置所有可设置动画的属性。您不会在外部块中设置任何可设置动画的特性。

这意味着系统立即认为外部动画已经完成,并立即调用完成块。同时,内部动画块仍在运行。

停止使用moveTo:duration:option:。它几乎帮不了你什么,结果却给你带来了这样的麻烦。扔掉它,试试这样的东西:

[UIView animateWithDuration:0.8 animations:^{
    tradeButton.frame = (CGRect){ CGPointMake(51, 150), tradeButton.bounds.size };
} completion:^(BOOL finished) {
    tradeButton.hidden = YES;
    // etc.
}];

请注意,UIViewAnimationOptionCurveEaseInEaseOut是大多数动画的默认值。

最新更新