2011-09-28 52 views
0

我試圖做一個卡片翻轉動畫...但沒有任何反應。iPhone試圖使用塊動畫來模擬卡片翻轉,但沒有任何反應,沒有動畫

這就是我所做的 1.創建一個容器視圖,以保存兩個視圖的動畫。 2.創建第一個視圖。 3.將其添加到容器。 4.創建第二視圖 3.啓動動畫塊,beginAnimations 4.呼叫setAnimationTransition放入視圖容器 5.添加所述第二視圖的視圖容器 6.提交動畫,

我的代碼

// create container view 
CGRect viewRect = CGRectMake(10, 10, 100, 100); 
UIView* myView = [[UIView alloc] initWithFrame:viewRect]; 

// create 2 viues to flip between for animation 
UIImage *i= [ UIImage imageNamed : @"card1.jpg"]; 
UIImageView *topCard=[ [UIImageView alloc] initWithImage: i ]; 
topCard.frame= CGRectMake(0,0,100,100); 
[myView addSubview:topCard]; 
[self.view addSubview:myView]; 

i= [ UIImage imageNamed : @"card2.jpg"]; 
UIImageView *butCard=[ [UIImageView alloc] initWithImage: i ]; 
butCard.frame= CGRectMake(0,0,100,100); 

// set up the animation 
[UIView beginAnimations:@"Flip Top Card" context:nil]; 
[UIView setAnimationDuration:1]; 
[UIView setAnimationRepeatCount:0]; 
[UIView setAnimationRepeatAutoreverses:NO]; 
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut ]; 
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:myView cache:YES]; 
[myView addSubview:butCard]; 

// start the animation 
[UIView commitAnimations]; 

-Ted

+0

要解決這個問題,您可能必須使用關鍵幀動畫或多個動畫。請記住,當卡片翻轉一半時,你想改變一些東西。 –

回答

6

如果這是iOS 4.0或更高版本,你可以這樣做:

[UIView transitionWithView:myView 
        duration:0.3f 
        options:UIViewAnimationOptionTransitionFlipFromLeft 
       animations:^(void) { 
        self.topCardIsShown = !self.topCardIsShown; 
        self.topCard.hidden = !self.topCardIsShown; 
        self.butCard.hidden = self.topCardIsShown; 
       } 
       completion:^(BOOL finished) { 
        // nothing 
       }]; 

這假設您已將兩個卡片視圖添加到myView並且已將@property(nonatomic, assign) BOOL topCardIsShown;添加到您的班級。

+0

如果你不想要屬性,你可能也可以「重用」圖像視圖的「hidden」屬性。 'self.topCard.hidden =!self.topCard.hidden; self.butCard.hidden = self.topCard.hidden;'但這可能有點神祕。 –