2014-03-31 53 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

您是否設置了卡的zIndex? –

+0

你看過添加一個「z」翻譯卡片寬度的一半嗎? – Wain

+0

不,我在CAAnimations方面仍然很不在意。 @DavidRönnqvist如何設置卡片的Z-index?爲什麼它只能隱藏一半的卡片?什麼是z翻譯? –

回答

0

我結束了卡片的xIndex亂搞,所以現在它的工作原理。

我已經更改了代碼,所以我不能在這裏發佈它。