CAAnimation隐藏了一半的视图

时间:2014-03-31 09:00:08

标签: ios objective-c macos calayer caanimation

我正在进行纸牌游戏,我需要这些卡片才能翻转#34;。首先面朝下然后翻转,所以脸部朝上。我做了一个很好的动画,它很好用,除了在动画过程中隐藏在后面的图层后面的一半视图(见截图)。

动画完成后,卡片视图会在其他视图之上叠加。

有关动画期间隐藏一半视图的任何提示吗?

截图: 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"]];
        }
    }
}

1 个答案:

答案 0 :(得分:0)

我最终搞乱了卡片的xIndex,所以现在它可以工作了。

我已经对代码进行了大量更改,但我无法在此处发布。