【问题标题】:Animate multiple UIView in a cycle在一个循环中为多个 UIView 设置动画
【发布时间】:2013-03-21 04:13:32
【问题描述】:

我正在 iPhone 上创建纸牌游戏。

我的问题是我想在游戏开始时为卡片设置动画,使卡片在套牌中从一个点动画到另一个点。

我在 for 循环中移动我的 UIView 卡片。这就是我的工作

使用此代码,所有卡片一起移动,我需要一个接一个地单独移动卡片

    CGPoint point;

// Create the deck of playing cards
for (int i = 0; i < 28; i++) {  
    CardView *aCardView = [self.mazzo objectAtIndex:i];

    point.x = -100;
    point.y = 200;
    aCardView.center = point;   

    aCardView.zPosition = i;

    [self.viewGioco addSubview:aCardView];

    [aCardView release];

            //Here i call the method to position the card
    [aCardView positionCard];

}

在卡片视图中有这个方法

-(void)positionCard{
    [self performSelector:@selector(_positionCard) withObject:nil afterDelay:0.0];
}
-(void)_positionCard{

[UIView beginAnimations:@"posizionacarta" context:nil];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
[UIView setAnimationDuration:0.3f];

CGPoint point;

point.x = 280 + ((arc4random() % 2) - 1);
point.y = 240 + ((arc4random() % 2) - 1);

self.center = point;

[UIView commitAnimations];

[self setNeedsLayout];  

}

【问题讨论】:

    标签: iphone animation uiview


    【解决方案1】:

    我认为最简单的方法是使用计数器 i 根据卡的索引计算轻微延迟 - 然后将其传递给 positionCard 方法。

    所以:

    [aCardView positionCardWithDelay:i/10];
    

    还有:

    -(void)positionCardWithDelay:(NSNumber)delay
    {
        // Call _position card after a slight delay
        [self performSelector:@selector(_positionCard) withObject:nil afterDelay:delay];
    }
    

    【讨论】:

    • 谢谢,我试过了,它运行了,但是移动流不规则,一些视图一起移动。再次感谢
    • 好的,已解决,使用 i/40.4 作为延迟。非常感谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-04
    • 2021-02-27
    • 1970-01-01
    相关资源
    最近更新 更多