ios - 允许一次调用一个类别方法 ios (@synchronized)

标签 ios objective-c animation uiviewanimation synchronized

我有一个 UIViewController 和一个用于向 UIViewController 添加方法的类别。类中有一个方法:

@implementation UIViewController (AlertAnimationsAndModalViews)
-(void)someAddedMethod
{
    UIView *someView;
    //do some animation with the view that lasts 3 seconds
    //remove the view and return

}

在任何 View Controller 中我都可以调用这个方法

[self someAddedMethod];

但是,我只想允许此方法一次运行一个。例如,如果我一个接一个地打两个电话

[self someAddedMethod];//call1
[self someAddedMethod];//call2

我希望第二个调用等到第一个调用完成。我知道 UIView animationWithduration... 在一个单独的线程中运行,并且由于我无法在类别中创建 iVar,所以我无法真正使用 @synchronized(someObject)..

有什么建议吗?

提前致谢!

编辑

方法如下所示:

 -(void)showTopBannerWithHeight:(CGFloat)height andWidth:(CGFloat)width andMessage:(NSString *)message andDuration:(CGFloat)duration
 {
 
 UILabel *messageLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, -height, width, height)];
[self.view.superview addSubview:messageLabel];


[UIView animateWithDuration:0.5
                      delay:0
                    options: UIViewAnimationOptionBeginFromCurrentState
                 animations:^{
                     messageLabel.frame = CGRectMake(0, 0, SCREEN_WIDTH, height);
                 }
                 completion:^(BOOL finished){
                     
                     [UIView animateWithDuration: 0.5
                                           delay:duration
                                         options: UIViewAnimationOptionBeginFromCurrentState
                                      animations:^{
                                          messageLabel.frame = CGRectMake(0, -height, SCREEN_WIDTH, height);
                                      }
                                      completion:^(BOOL finished){
                                          [messageLabel removeFromSuperview];
                                      }];
                 }];

所以我从屏幕顶部显示一个“横幅”,等待一段时间(CGFloat),然后将 View 滑出屏幕并删除。因为这是一个类别我不能添加实例变量.. 所以我想要实现的是,如果对该方法进行了多次调用,我希望第一个调用无需等待即可执行,但之后的每个调用都要等到上一个调用完成。

最佳答案

如果它只是关于动画,您可以检查 if ([someView.layer animationForKey:@"sameKeyAsOnCreation"] == nil)。比你只会添加一个动画,如果它当前没有运行。

您还可以使用关联对象自行存储状态(动画运行/未运行)。

关于ios - 允许一次调用一个类别方法 ios (@synchronized),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16708654/

相关文章:

iphone - 为表格中新添加的行设置动画

ios - 如何通过使用自动布局显示和隐藏组件来调整 UITableViewCell 的高度

flutter snackbar 动画和滑动

ios - 使用 iOS App、Swift 获取设备/用户位置的问题

ios - 在没有导航 Controller Swift 的情况下以编程方式更改 View Controller

ios - TWRequest iOS API 是否支持仅应用程序身份验证?

java - Android动画 View 从左到右

ios - Rubymotion:Rake 失败

iphone - 绘图线位于 Core Plot 中的 X 轴下方

Mouseenter 上的 jQuery 动画问题