首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >是否可以连续使用UIViews来抓取UISnapBehavior

是否可以连续使用UIViews来抓取UISnapBehavior
EN

Stack Overflow用户
提问于 2014-04-15 23:03:01
回答 1查看 968关注 0票数 1

我正在CS193P上工作,我想要创造一个效果,扑克牌从0,0,一个接一个地飞入其中。我试图连锁动画,但视图一起飞,我也尝试使用UIDynamicAnimator和同样的事情发生。所有的景色都合在一起了。下面是我必须捕捉视图的代码。

代码语言:javascript
复制
-(void)snapCardsForNewGame
{
    for (PlayingCardView *cardView in self.cards){
        NSUInteger cardViewIndex = [self.cards indexOfObject:cardView];
        int cardColumn = (int) cardViewIndex / self.gameCardsGrid.rowCount;
        int cardRow = (int) cardViewIndex % self.gameCardsGrid.rowCount;
        UISnapBehavior *snapCard = [[UISnapBehavior alloc]initWithItem:cardView snapToPoint:[self.gameCardsGrid centerOfCellAtRow:cardRow inColumn:cardColumn]];
        snapCard.damping = 1.0;
        [self.animator addBehavior:snapCard];

    }


}


-(void)newGame
{
    NSUInteger numberOfCardsInPlay = [self.game numberOfCardsInPlay];
    for (int i=0; i<numberOfCardsInPlay; i++) {
        PlayingCardView *playingCard = [[PlayingCardView alloc]initWithFrame:CGRectMake(0, 0, 50, 75)];
        playingCard.faceUp = YES;
        [playingCard addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(flipCard:)]];
        [self.cards addObject:playingCard];
        //NSUInteger cardViewIndex = [self.cards indexOfObject:playingCard];
        //int cardColumn = (int) cardViewIndex / self.gameCardsGrid.rowCount;
        //int cardRow = (int) cardViewIndex % self.gameCardsGrid.rowCount;

       // playingCard.frame = [self.gameCardsGrid frameOfCellAtRow:cardRow inColumn:cardColumn];
        playingCard.center = CGPointMake(0, 0);
        [self.gameView addSubview:playingCard];
        [self snapCardsForNewGame];
    }
}

在这种情况下使用它有意义吗?我尝试了几个不同的东西,让牌一个一个地飞进来,但没能做到。

提前感谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-04-15 23:31:40

因为您同时添加了所有的UISnapBehaviors,动画师将它们全部运行在一起。延迟添加到动画师,他们将是自己的动画。

代码语言:javascript
复制
-(void)snapCardsForNewGame
{
    for (PlayingCardView *cardView in self.cards){
        NSUInteger cardViewIndex = [self.cards indexOfObject:cardView];
        int cardColumn = (int) cardViewIndex / self.gameCardsGrid.rowCount;
        int cardRow = (int) cardViewIndex % self.gameCardsGrid.rowCount;
        UISnapBehavior *snapCard = [[UISnapBehavior alloc]initWithItem:cardView snapToPoint:[self.gameCardsGrid centerOfCellAtRow:cardRow inColumn:cardColumn]];
        snapCard.damping = 1.0;

        NSTimeInterval delayTime = 0.01 * cardViewIndex;
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayTime * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self.animator addBehavior:snapCard];
        });
    }
}
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23096088

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档