简体   繁体   English

是否可以连续使用UISnapBehavior捕获UIViews

[英]Is it possible to snap UIViews using UISnapBehavior consecutively

I am working on CS193P and I would like to create an effect where cards fly in from the 0,0 one after another snapping into place. 我正在研究CS193P,我想创建一种效果,卡片从0,0一个接一个地卡入到位。 I tried to chain the animation but the views flying together also I am trying to use UIDynamicAnimator and same thing happens. 我试图链接动画,但视图一起飞行我也试图使用UIDynamicAnimator,同样的事情发生。 All the views are snapping together. 所有观点都在一起攫取。 Here is the code that i have to snap the views. 这是我必须捕捉视图的代码。

-(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];
    }
}

Does it even make sense to use it in this situation? 在这种情况下使用它是否有意义? I tried few different things to get the cards fly in one by one but wasn't able to. 我尝试了几个不同的东西让卡片一个接一个地飞,但是无法做到。

Thanks in advance! 提前致谢!

Since you're adding all the UISnapBehaviors at the same time, the animator runs them all together. 由于您同时添加了所有UISnapBehaviors ,动画师UISnapBehaviors它们全部一起运行。 Delay adding them to the animator and they'll be animated on their own. 延迟将它们添加到动画师,他们将自己动画。

-(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];
        });
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM