2014-10-29 59 views
1

我正在爲Mac製作應用程序。我連續有5張牌,按一下按鈕,所有的牌應該順序翻轉(第一張牌的翻轉應該在第二張牌的翻轉後)。 我有以下問題CaAnimations不能正常工作

當我點擊動畫按鈕,卡片應該從後到前翻轉(顏色到數字),一切都很好。它們通過繞Y軸翻轉旋轉完美地一個接一個地動畫。

但是,當我點擊動畫按鈕,卡應該以相反的方式(數字顏色)這裏發生了什麼: 所有的卡片與背面層沒有任何動畫和所有的一次性。

之後,每一張卡片(依次)變成正確的前層並且對後層進行動畫處理。

這裏是動畫

https://www.youtube.com/watch?v=fznUFR6pHCQ&feature=youtu.be

什麼我改變,所以我可以動畫OK的視頻。

我初始化卡這樣

flip1= [[PKRGSPFlipAnimationController alloc] initWithParent:firstCardView newFrontImage:[NSImage imageNamed:@"front.png"]]; 
    flip2= [[PKRGSPFlipAnimationController alloc] initWithParent:secondCardView newFrontImage:[NSImage imageNamed:@"front.png"]]; 
    flip3= [[PKRGSPFlipAnimationController alloc] initWithParent:thirdCardView newFrontImage:[NSImage imageNamed:@"front.png"]]; 
    flip4= [[PKRGSPFlipAnimationController alloc] initWithParent:fourthCardView newFrontImage:[NSImage imageNamed:@"front.png"]]; 
    flip5= [[PKRGSPFlipAnimationController alloc] initWithParent:fifthCardView newFrontImage:[NSImage imageNamed:@"front.png"]]; 

上的按鈕,我調用這個函數的點擊

[flip1 flip:1]; 
[flip2 flip:2]; 
[flip3 flip:3]; 
[flip4 flip:4]; 
[flip5 flip:5]; 

這裏是班裏的翻轉(flip1-5)的: .M文件

define FLIP_TIME 1.0 // Time taken to do the animation. 
#define PERSPECTIVE 1000 
#define RECT NSMakeRect(0, 0, 134, 198) // Value of the coordiantes of the parent view. 
#define DEGREES_TO_RADIANS(angle) (angle * M_PI/180.0) 
#import "PKRGSPFlipAnimationController.h" 

@implementation PKRGSPFlipAnimationController 

@synthesize cardLayer; 
@synthesize frontLayer; 
@synthesize backLayer; 

@synthesize parentView; 

@synthesize isFront; 

-(id)initWithParent:(NSView *)newParent newFrontImage:(NSImage *)newFront{ 
    self = [super init]; 
    if (self) { 
     parentView = newParent; 
     isFront = false; 

     cardLayer =[CATransformLayer layer]; 
     [cardLayer setFrame:RECT]; 

     frontLayer = [CALayer layer]; 
     [frontLayer setFrame:RECT]; 
     [frontLayer setZPosition:1.0]; 
     [frontLayer setDoubleSided:NO]; 
     [frontLayer setContents:(id)newFront]; 
     [cardLayer addSublayer:frontLayer]; 

     backLayer = [CALayer layer]; 
     [backLayer setFrame:RECT]; 
     [backLayer setZPosition:2.0]; 
     [backLayer setDoubleSided:NO]; 
     [backLayer setContents:(id)[NSImage imageNamed:@"back.png"]]; 

     [cardLayer addSublayer:backLayer]; 

     [parentView setLayer:[CALayer layer]]; 
     [parentView setWantsLayer:YES]; 
     [parentView.layer addSublayer:cardLayer]; 

    } 
    return self; 
} 

- (CAAnimation*) createAnimFrom:(double)frAngl to:(double)toAngl 
{ 
    //For the card to rotate on the 
    // Y axis - @"transform.rotation.y" 
    // X axis - @"transform.rotation.x" 

    NSString* rotationAxis = @"transform.rotation.y"; 

    CABasicAnimation* ba = [CABasicAnimation animationWithKeyPath:rotationAxis]; 
    ba.fromValue = [NSNumber numberWithFloat:frAngl]; 
    ba.toValue = [NSNumber numberWithFloat:toAngl]; 

    CABasicAnimation *shrinkAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"]; 
    shrinkAnimation.toValue = [NSNumber numberWithFloat:0.7f]; 
    shrinkAnimation.duration = FLIP_TIME*0.5; 
    shrinkAnimation.autoreverses = YES; 

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

-(void) flip: (int) beginTime 
{ 
    CALayer * currLayer; 
    CALayer * nextLayer; 
    if(isFront){ 
     currLayer=frontLayer; 
     nextLayer=backLayer; 
    } 
    else{ 
     currLayer=backLayer; 
     nextLayer=frontLayer; 
    } 
    NSLog(@"%hhd", isFront); 
    isFront=!isFront; 

    [CATransaction begin]; 
    CAAnimation* anim1 = [self createAnimFrom:0.0 to:M_PI]; 
    anim1.duration = FLIP_TIME; 
    anim1.beginTime = CACurrentMediaTime()+beginTime; 
    CAAnimation* anim2 = [self createAnimFrom:-M_PI to:0.0]; 
    anim2.duration = FLIP_TIME; 
    anim2.beginTime = CACurrentMediaTime()+beginTime; 
    [CATransaction commit]; 


    //add perspective 
    CATransform3D mt = CATransform3DIdentity; 
    mt.m34 = 1.0/(double)PERSPECTIVE; 

    currLayer.transform = mt; 
    nextLayer.transform = mt; 

    NSPoint ap = {0.5,0.5}; 
    currLayer.anchorPoint = ap; 
    nextLayer.anchorPoint = ap; 

    [CATransaction begin]; 
    [currLayer addAnimation:anim1 forKey:@"flip"]; 
    [nextLayer addAnimation:anim2 forKey:@"flip"]; 
    [CATransaction commit]; 
} 
@end 

.h文件中

#import <Cocoa/Cocoa.h> 
#import <QuartzCore/QuartzCore.h> 

@interface PKRGSPFlipAnimationController : NSObject 

-(id)initWithParent:(NSView *)newParent newFrontImage:(NSImage *)newFront; 
- (void) flip: (int) beginTime; 

@property (nonatomic, strong) NSView* parentView; 

@property (nonatomic, strong) CATransformLayer * cardLayer; 
@property (nonatomic, strong) CALayer *frontLayer; // Front side (Number side) of the card. 
@property (nonatomic, strong) CALayer *backLayer; // Back side (Blank side) of the card. 

@property (nonatomic) BOOL isFront; // Boolean value for the card to check if it is turned. 
@end 
+0

它的卡號碼端。我只在構造函數中添加它,並只在那裏初始化它。我在那裏添加它,因爲有不同的數字卡片。 – Oya 2014-10-29 09:31:07

回答

0

我添加了一個動畫停止並重新初始化它。這解決了問題