1

有点问题,搜索了一些类似的问题,但无法进行。我有一个简单的按钮动画,我在我制作的实用程序类中的项目中使用它。问题是按钮代码在动画完成之前执行。

实用程序 class.m 中的动画代码:

+(void)buttonBobble:(UIButton *)button{
  button.transform = CGAffineTransformMakeScale(0.8, 0.8);
  [UIView beginAnimations:@"button" context:nil];
  [UIView setAnimationDuration:.5];
  button.transform = CGAffineTransformMakeScale(1, 1);
  [UIView commitAnimations];
}

我试图确保动画在任何按钮中触发代码之前完成:

[UIView animateWithDuration:0.0f delay:0.0f options: UIViewAnimationOptionTransitionNone  animations:^{
    [Utilities buttonBobble:sender];
}completion:^(BOOL finished){
    //Do stuff
}];

即使可行,我也希望将其抽象到可以执行以下操作的地方:

if([Utilities buttonBobble:sender]){
  //Make it send a BOOL so when it's done I execute stuff like normal
}

欢迎任何想法。

4

2 回答 2

5

更改您的实用程序方法以采用一个完成块,该块封装了该按钮在完成摆动时需要采取的操作:

+(void)buttonBobble:(UIButton *)button 
     actionWhenDone:(void (^)(BOOL))action
{
    button.transform = CGAffineTransformMakeScale(0.8, 0.8);
    [UIView animateWithDuration:0.5f animations:^{
        button.transform = CGAffineTransformMakeScale(1, 1);
     }
                     completion:action];
}

然后,在原始按钮操作方法中,您传递该操作块,而不是直接在方法中运行代码:

- (IBAction)buttonAction:(id)sender
{
    [Utilities buttonBobble:sender
             actionWhenDone:^(BOOL finished){
                // Your code here
        }];
    // Nothing here.
}

作为设计说明,您还可以考虑将该实用程序方法放入以下类别UIButton

@implementation UIButton (JMMBobble)

- (void)JMMBobbleWithActionWhenDone:(void (^)(BOOL))action
{
    self.transform = CGAffineTransformMakeScale(0.8, 0.8);
    [UIView animateWithDuration:0.5f animations:^{
        self.transform = CGAffineTransformMakeScale(1, 1);
     }
                     completion:action];
} 

然后动作看起来像

- (IBAction)buttonAction:(id)sender
{
    [sender JMMBobbleWithActionWhenDone:^(BOOL finished){
                // Your code here
        }];
}
于 2013-07-21T02:53:47.340 回答
0

我喜欢以不同的方式来做,特别是如果动画将彼此嵌套,我发现如果我使用NSTimers. 您可以看到计时器使用与动画时间相同的间隔。

-(void)fadeOutText{
    [UIView beginAnimations:@"fadeOutText" context:NULL];
    [UIView setAnimationDuration:1.0];
    someButton.titleLabel.alpha = 0.0f;
    [UIView commitAnimations];
    [NSTimer scheduledTimerWithTimeInterval:1.0
                                     target:self
                                   selector:@selector(changeTextAndFadeIn)
                                   userInfo:nil
                                    repeats:NO];
}
-(void)changeTextAndFadeIn{
    [someButton setTitle:@"Testing" forState:UIControlStateNormal];
    [UIView beginAnimations:@"fadeInText" context:NULL];
    [UIView setAnimationDuration:1.0];
    someButton.titleLabel.alpha = 1.0f;
    [UIView commitAnimations];
    [NSTimer scheduledTimerWithTimeInterval:1.0
                                     target:self
                                   selector:@selector(finishedFading)
                                   userInfo:nil
                                    repeats:NO];
}
-(void)changeTextAndFadeIn{
    //Code to be executed after text has faded out / changed / faded in.
}
于 2013-07-21T10:07:25.493 回答