Ios 是否可以连续使用UISnapBehavior捕捉UIView
我正在CS193P上工作,我想创造一种效果,使卡片从0,0一张接一张地飞入到位。我试图链接动画,但视图飞在一起,我也试图使用UIDynamicMator,同样的事情也发生了。所有视图都在对齐。下面是我必须捕捉视图的代码Ios 是否可以连续使用UISnapBehavior捕捉UIView,ios,ios7,uidynamicbehavior,uidynamicanimator,Ios,Ios7,Uidynamicbehavior,Uidynamicanimator,我正在CS193P上工作,我想创造一种效果,使卡片从0,0一张接一张地飞入到位。我试图链接动画,但视图飞在一起,我也试图使用UIDynamicMator,同样的事情也发生了。所有视图都在对齐。下面是我必须捕捉视图的代码 -(void)snapCardsForNewGame { for (PlayingCardView *cardView in self.cards){ NSUInteger cardViewIndex = [self.cards indexOfObject
-(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];
}
}
-(无效)SnapCardsFornewName
{
用于(在self.cards中播放cardView*cardView){
NSU Integer 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 CenterOfcellarRow:cardRow inColumn:cardColumn]];
snapCard.damping=1.0;
[self.animator addBehavior:snapCard];
}
}
-(无效)新游戏
{
NSUInteger numberOfCardsInPlay=[self.game numberOfCardsInPlay];
对于(int i=0;i由于您同时添加所有的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];
});
}
}
我以前没有使用过这个,但是UIDynamicMator有一个代理,你可以将自己设置为。当动态设置达到平衡时,动画师(我想)暂停并告诉学员它暂停了。这样您就不会在这里写循环了。您抓拍一张卡,等待暂停,再抓拍一张…效果很好。我只是将延迟时间更改为0.05,使卡的输出速度稍微慢一点。谢谢!太棒了!快乐编码:)