4 개의 카드가 화면에 표시되는 작은 카드 게임을 작성 중입니다. 사용자는 각 카드를 탭하여 (다시 한 번 가리기 위해) 탭할 수 있습니다.UIView setAnimationTransition은 가끔씩 만 작동하지만 항상 그렇지는 않습니까?
각 카드 앞면과 뒷면 카드는 이미지보기에 저장됩니다. UIButton은 사용자 탭을 잡아 카드를 넘겨야합니다.
카드의 앞면과 뒷면을 컨테이너 뷰에 하위보기로 추가했으며 애니메이션에는 UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight
메서드를 사용합니다.
아래의 코드를 참조하십시오. 간략화를 위해 & 가독성을 위해 아래 방법에서 4 가지 카드 처리를 제거했습니다. 또한 일부 배열/파일 이름 - 앞면과 뒷면에 대한 정적 이미지 이름 카드에 대한 저글링을 대체했습니다.
이 코드의 이상한 점은 때로는 앞으로 10 번 예상되는 동작 (즉, 반전 애니메이션이 표시됨)이지만 애니메이션이 전혀 표시되지 않는 경우입니다 (즉, 다른 카드면이 표시되지만 뒤집기.). 그런 다음 다른 방향으로 진행됩니다 : 때로는 카드가 애니메이션없이 7 ~ 8 번 표시되고 갑자기 뒤집기 애니메이션이 표시됩니다. 이 이상한 행동에 대한 이유를 알 수 없기 때문에 그것은 내게 열매를 맺고 있습니다. 의견이 있으십니까? [self performSelector:@selector(flipSingleCard) withObject:nil afterDelay:0.0];
이 범인이라고
-(void)flipCardButtonClicked:(id)sender
{
containerView = [[UIView alloc] initWithFrame: CGRectMake(25,420,220,300)];
[self.view addSubview:containerView];
c1Flipped = !c1Flipped;
cardback1 = [[UIImageView alloc] initWithFrame: CGRectMake(0,0,220,300)];
cardfront1 = [[UIImageView alloc] initWithFrame: CGRectMake(0,0,220,300)];
if (c1Flipped)
{
cardback1.image = [UIImage imageNamed:@"backside.png"];
cardfront1.image = [UIImage imageNamed:@"frontside.png"];
}
else
{
cardback1.image = [UIImage imageNamed:@"frontside.png"];
cardfront1.image = [UIImage imageNamed:@"backside.png"];
}
[containerView addSubview:cardfront1];
[containerView addSubview:cardback1];
[cardfront1 release];
[cardback1 release];
[self performSelector:@selector(flipSingleCard) withObject:nil afterDelay:0.0];
}
-(void)flipSingleCard
{
[containerView.layer removeAllAnimations];
[UIView beginAnimations:@"cardFlipping" context:self];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(flipDidStop:finished:context:)];
[UIView setAnimationDuration:0.5];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:containerView cache:YES];
[containerView exchangeSubviewAtIndex:0 withSubviewAtIndex:1];
[UIView commitAnimations];
}
-(void)flipDidStop:(NSString*)animationID finished:(BOOL)finished context:(void *)context
{
[containerView removeFromSuperview];
[containerView release];
}
당신의 생각에 고마워하지만 애니메이션이 비동기 적이기 때문에 나는 직접 메서드를 호출하는 대신 선택기를 사용하는 주된 이유라고 생각했다. (?) 실제 애니메이션이 멈추기 전에 setAnimationDidStopSelector 메서드가 호출되어서는 안된다. – SpaceAce
비동기식이 셀렉터 대 직접 메서드 호출을 사용하는 데 어떤 영향을 미치는지 나는 알 수 없습니다. 근본적으로 똑같은 방식을하고 있습니다 ... 실제 지연을 추가하려고 했습니까? – BHendricks
죄송합니다, 잘못된 선택자를보고있었습니다. performSelector를 사용하여 변경을 시도하고 결과를 게시합니다. 다시 한 번 감사드립니다. Sidenote : 여기에 내 의견을 수정/삭제할 수있는 옵션이 있습니까? – SpaceAce