遇到一些问题,搜索了一些类似的问题,但无法解决问题.我有一个简单的按钮动画,我在我制作的实用程序类中使用我的项目.问题是按钮代码在动画完成之前执行.
实用程序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];
}
Run Code Online (Sandbox Code Playgroud)
我试图确保动画在任何按钮触发代码之前完成:
[UIView animateWithDuration:0.0f delay:0.0f options: UIViewAnimationOptionTransitionNone animations:^{
[Utilities buttonBobble:sender];
}completion:^(BOOL finished){
//Do stuff
}];
Run Code Online (Sandbox Code Playgroud)
即使这样有效,我希望将它抽象出来,我可以做到这样的事情:
if([Utilities buttonBobble:sender]){
//Make it send a BOOL so when it's done I execute stuff like normal
}
Run Code Online (Sandbox Code Playgroud)
欢迎任何想法.
更改您的实用程序方法以获取一个完成块,该块封装了按钮在完成浮动时所需的操作:
+(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];
}
Run Code Online (Sandbox Code Playgroud)
在原始按钮操作方法中,您传递该操作块而不是直接在方法中运行代码:
- (IBAction)buttonAction:(id)sender
{
[Utilities buttonBobble:sender
actionWhenDone:^(BOOL finished){
// Your code here
}];
// Nothing here.
}
Run Code Online (Sandbox Code Playgroud)
作为设计说明,您还可以考虑将该实用程序方法放在以下类别中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];
}
Run Code Online (Sandbox Code Playgroud)
然后动作看起来像
- (IBAction)buttonAction:(id)sender
{
[sender JMMBobbleWithActionWhenDone:^(BOOL finished){
// Your code here
}];
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
122 次 |
| 最近记录: |