ios - 是否可以连续使用 UISnapBehavior 捕捉 UIView

标签 ios ios7 uidynamicbehavior uidynamicanimator

我正在研究 CS193P,我想创建一种效果,让卡片从 0,0 一个接一个地卡入到位。我试图链接动画,但 View 一起飞行,我也在尝试使用 UIDynamicAnimator 并且发生了同样的事情。所有的 View 都在捕捉在一起。这是我必须捕捉 View 的代码。

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

在这种情况下使用它是否有意义?我尝试了几种不同的方法来让卡片一张一张地飞起来,但没能做到。

提前致谢!

最佳答案

由于您要添加所有 UISnapBehaviors同时,动画师将它们一起运行。延迟将它们添加到动画师中,它们将自行制作动画。

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

关于ios - 是否可以连续使用 UISnapBehavior 捕捉 UIView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23096088/

相关文章:

cocoa-touch - 如何检测 iOS 7 中麦克风输入权限被拒绝

ios - 当 UIGravityBehavior 处于事件状态时,UIDynamicAnimator 拒绝达到平衡

ios - iOS 8.0 中的 UISearchBar barTintColor clearColor 错误

iphone - 轻量级核心数据迁移后,如何为现有实体的新属性设置默认值?

ios - 即使应用程序位于同一位置,信标的邻近范围也会来回变化

ios - AVAudioRecorder 在 prepareToRecord 上抛出异常

ios7 - UIDynamics 和设备旋转

swift - 为什么调用UIDynamicBehavior的action closure后会出现memory leak/retain cycle?

iphone - NSString 中子字符串之后的子字符串

ios - 使用 DropDownMenu Swift 4 约束关闭容器 UIView 中的面板