ios - iOS 中的 subview 动画一次一个

标签 ios iphone objective-c animation uiview

我有 9 个以编程方式创建和布局的自定义 subview 。我想一次为它们的创建和位置变化设置动画,但现在整套 9 个 subview 正在同时设置动画。

这是我的代码:

- (void)viewDidLoad{

    //create 9 setCardViews and add them to the array setCards to be able to identify them later
    for (int i=0; i<9; i++) {
        SetCardView *card = [[SetCardView alloc]initWithFrame:CGRectMake(self.gameView.center.x, self.gameView.center.y, 0, 0)];
        [self.gameView addSubview:card];
        [self.setCards addObject:card];
    }

    //layout those 9 views
    [self updateLayout];
}

然后,updateLayout 方法被调用。它设置了一个框架大小, subview 在其中布局:

-(void)updateLayout
{
    CGRect canvas = [self resizeCanvasWithNumberOfCards:[self.setCards count]];

    for (SetCardView *card in self.setCards)
    {
        [self layOutCard:card withFrame:canvas atIndex:[self.setCards indexOfObject:card]];
    }
}

最后,在 layOutCard 方法中我计算了 View 的位置(我在此处省略了那部分代码)并在其框架中设置动画变化:

-(void)layOutCard:(SetCardView *)card withFrame:(CGRect)canvas atIndex:(NSUInteger)index
{
    //calculate the new frame of the view

    CGRect rect = //calculations ;

    [SetCardView animateWithDuration:0.5 delay:self.delay options: UIViewAnimationOptionCurveEasyInOut animations:^{card.frame=rect;} completion:nil];
}

所以所有这些动画同时发生,也许是因为它在 updateLayout 方法的循环内,所以 Controller 等待它完成?无论如何,我想不出一种方法来制作 View 的动画而不使用循环,以便它们一次为一个动画制作动画。

我该怎么做?

谢谢

最佳答案

使它起作用的方法之一是在 updateLayout 中枚举卡片时增加延迟,然后将其传递给 layOutCard:withFrame:atIndex(当然,您必须向该方法添加延迟参数。或 ft index 参数开始从0开始,每次加1就可以用它来计算延迟:

-(void)layOutCard:(SetCardView *)card withFrame:(CGRect)canvas atIndex:(NSUInteger)index
{
    //calculate the new frame of the view

    CGRect rect = //calculations ;
    float myDelay = 0.5 * index; //0.5 is your duration.
    [SetCardView animateWithDuration:0.5 delay: myDelay options: UIViewAnimationOptionCurveEasyInOut animations:^{card.frame=rect;} completion:nil];
}

关于ios - iOS 中的 subview 动画一次一个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23777489/

相关文章:

ios - 将 WordPress iOS 应用程序与受 Shibboleth 保护的 WordPress 结合使用

iphone - coredata中使用的undoManager

iphone - UIImagePickerController 不填满屏幕

ios - TestFlight应用程序在启动时卡住

objective-c - "Expected a Type"编译器应该知道的协议(protocol)错误

ios - 我必须手动删除 KVO 中的观察者吗

ios - iOS App在带有日志的NSURLConnection上崩溃,但我无法重复,请帮助我

ios - SceneKit UIImage Material 为黑色

iphone - 应用程序打开然后立即关闭 - iPhone 模拟器

ios - 尝试使用 Card.io-iOS-SDK 扫描卡时应用程序崩溃。