2014-03-31 5 views
0

카드를 뒤집을 카드가 필요한 곳에서 카드 게임을 만들고 있습니다. 먼저 얼굴을 아래로 향하게하고 뒤집어서 얼굴이 위로 향하게합니다. 나는 멋진 애니메이션을 만들었고 애니메이션을 만드는 동안 뒤쪽의 레이어 뒤에 뷰의 절반이 숨겨져 있다는 점을 제외하고는 멋지게 작동합니다 (스크린 샷 참조).CAAnimation이보기의 절반을 숨 깁니다.

애니메이션이 완료되면 카드보기가 다른보기 위에 잘 쌓입니다.

애니메이션 중에보기의 절반이 숨겨진 이유에 대한 힌트가 있습니까?

스크린 샷 : Screenshot 1

코드 :

- (void)setFaceDirection:(BOOL)up{ 
    if(up && !self.faceUp){ 
     self.faceUp = up; 
     [self flip:@"in"]; 
    }else if(!up && self.faceUp){ 
     self.faceUp = up; 
     [self flip:@"out"]; 
    } 
} 

- (void)flip:(NSString *)direction{ 
    NSLog(@"flip"); 
    [NSTimer scheduledTimerWithTimeInterval:0.0 target:self selector:@selector(firstFlip) userInfo:nil repeats:NO]; 
    [NSTimer scheduledTimerWithTimeInterval:1.2 target:self selector:@selector(secondFlip) userInfo:nil repeats:NO]; 
} 

- (void)firstFlip{ 
    [CATransaction begin]; 
    CAAnimation* anim1 = [self createAnimDirection:@"in"]; 
    [CATransaction commit]; 

    //add perspective 
    CATransform3D mt = CATransform3DIdentity; 
    mt.m34 = 1.0/1000; 
    mt = CATransform3DTranslate(mt, 0.0f, 0.0f, 0.01f); 

    CALayer* lr = [self layer]; 
    lr.transform = mt; 

    NSPoint ap = {0.5,0.0}; // Begin from OS X Mountain Lion ancorPoint by default at 0,0; 
    lr.anchorPoint = ap; 

    // animation delegate to this class to handle message on its completion 
    anim1.delegate = self; 

    CGPoint center = CGPointMake(CGRectGetMidX(self.frame), self.frame.origin.y); 
    // lr.position = center; 

    [CATransaction begin]; 
    [lr addAnimation:anim1 forKey:@"flip"]; 
    [CATransaction commit]; 
} 

- (void)secondFlip{ 
    [CATransaction begin]; 
    CAAnimation* anim1 = [self createAnimDirection:@"out"]; 
    [CATransaction commit]; 

    //add perspective 
    CATransform3D mt = CATransform3DIdentity; 
    mt.m34 = 1.0/1000; 
    mt = CATransform3DTranslate(mt, 0.0f, 0.0f, 0.01f); 

    CALayer* lr = [self layer]; 
    lr.transform = mt; 

    NSPoint ap = {0.5,0}; // Begin from OS X Mountain Lion ancorPoint by default at 0,0; 
    lr.anchorPoint = ap; 

    // animation delegate to this class to handle message on its completion 
    anim1.delegate = self; 

    CGPoint center = CGPointMake(CGRectGetMidX(self.frame), self.frame.origin.y); 
    lr.position = center; 

    [CATransaction begin]; 
    [lr addAnimation:anim1 forKey:@"flip"]; 
    [CATransaction commit]; 
} 

- (CAAnimation*) createAnimDirection:(NSString *) direction 
{ 
    double from = 0.0; 
    double to = M_PI/2; 
    if ([direction isEqualToString:@"in"]){ 
     [self setImage:[NSImage imageNamed:@"Card_Background"]]; 
    }else{ 
     from = -M_PI/2; 
     to = 0.0; 
     [self setImage:self.faceImage]; 
    } 

    NSString* sRotation; 
    sRotation = @"transform.rotation.y"; 
    CABasicAnimation* ba = [CABasicAnimation animationWithKeyPath:sRotation]; 
    ba.fromValue = [NSNumber numberWithFloat:from]; 
    ba.toValue = [NSNumber numberWithFloat:to]; 

    CAAnimationGroup *animationGroup = [CAAnimationGroup animation]; 
    animationGroup.animations = [NSArray arrayWithObjects:ba, nil]; 
    animationGroup.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]; 
    animationGroup.duration = 1.2; 
    animationGroup.fillMode = kCAFillModeForwards; 
    animationGroup.removedOnCompletion = NO; 

    return animationGroup; 
} 

- (void)animationDidStop:(CAAnimation *)theAnimation finished:(BOOL)flag{ 
    if(flag){ 
     if (self.faceUp){ 
      [self setImage:self.faceImage]; 
     }else{ 
      [self setImage:[NSImage imageNamed:@"Card_Background"]]; 
     } 
    } 
} 
+0

카드의 'z 인덱스'를 설정 했습니까? –

+0

카드 너비의 절반에 해당하는 'z'트랜 슬 레이션을 추가 했습니까? – Wain

+0

아니요, CAAnimations에 관해서는 여전히 아주 싫습니다. @ DavidRönnqvist 어떻게 카드의 Z- 인덱스를 설정합니까? 숨겨진 카드의 절반 밖에 안되는 이유는 무엇입니까? z- 번역이란 무엇입니까? –

답변

0

내가 카드의 xIndex와 장난 결국, 그래서 지금은 작동합니다.

코드를 너무 많이 변경했기 때문에 여기에 게시 할 수 없습니다.