2013-12-18 39 views
0

我試圖翻轉動畫到另一個視圖,這對應於屏幕的一小部分的視圖。兩個視圖都具有相同的尺寸和中心。 我一直在獲取全屏幕的動畫。從下面的代碼,有人可以讓我知道我做錯了什麼?查看動畫問題

謝謝

-j

+ (void) flipView:(UIView*)viewA toView:(UIView*)viewB wait:(Boolean)wait 
{  
    // get parent view 
    UIView *parent = [viewA superview]; 
    CGRect r = viewA.frame; 

    // create container view with the same dimensions as ViewA 
    UIView *containerView = [[UIView alloc] initWithFrame:viewA.bounds]; 
    containerView.center = viewA.center; 

    // attache both views to intermidiate view 
    [containerView addSubview:viewA]; 
    [containerView addSubview:viewB]; 

    [viewA removeFromSuperview]; 

    [parent addSubview:containerView]; 

    viewB.alpha = 0; 
    viewA.alpha = 1; 

    // Perform the annimation 
    __block BOOL done = NO; 

    [UIView transitionWithView:viewA 
         duration:2.0 
         options: (UIViewAnimationOptionTransitionFlipFromTop) 
        animations:^{ 
         viewA.alpha = 0; 
         viewB.alpha = 1; } 
        completion:^(BOOL finished) { 
         done = YES; 
         // detach all views 
          [viewA removeFromSuperview]; 
          [viewB removeFromSuperview]; 
          [containerView removeFromSuperview]; 
        } 
    ]; 

    if(wait) { 
     while (done == NO) 
      [[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.1]]; 

    } 
} 
+0

您是如何調用此方法的? –

+0

[工具flipView:view1 toView:view2 wait:YES]; – Joao

+0

view1和view2是那些小視圖對不對? –

回答

0

首先擺脫舊動畫代碼的(或至少提交它),它應該看起來像這樣:

+ (void) flipView:(UIView*)viewA toView:(UIView*)viewB wait:(BOOL)wait 
{ 
    viewB.alpha = 0; 
    viewA.alpha = 1; 

    __block BOOL done = NO; 
    [UIView transitionWithView:viewA 
         duration:2.0 
         options: (UIViewAnimationOptionTransitionFlipFromTop) 
        animations:^{ 
         viewA.alpha = 0; 
         viewB.alpha = 1; } 
        completion:^(BOOL finished) { 
         done = YES; 
        } 
    ]; 

    if(wait) { 
     while (!done) 
      [[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.1]]; 
    } 
} 

Seco nd,您想要轉換的子視圖的超級視圖實際上將獲得轉換。所以這意味着您必須將viewA和viewB添加到子視圖中,將此子視圖添加到另一個視圖然後執行動畫。

+0

我相應地編輯了代碼,但仍得到相同的結果。 你能讓我知道我還在做什麼錯嗎? – Joao

+0

你也在另一個子視圖中移動了兩個視圖? –

+0

是的,如: [containerView addSubview:viewA]; [containerView addSubview:viewB]; – Joao

-1

你爲什麼不翻轉在同一方向上的看法?

-(void) FlipView:(bool) fromRight { 

if(fromRight) { 
    [UIView beginAnimations:nil context:nil]; 
    [UIView setAnimationDuration:0.75]; 
    [UIView setAnimationDelegate:self]; 
    [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:view1 cache:YES]; 
    [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:view2 cache:YES]; 

    [UIView commitAnimations]; 
} 
else { 

    [UIView beginAnimations:nil context:nil]; 
    [UIView setAnimationDuration:0.75]; 
    [UIView setAnimationDelegate:self]; 
    [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:view1 cache:YES]; 
    [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:view2 cache:YES]; 

    [UIView commitAnimations]; 


} 
} 

並調用該方法象下面這樣:

[view1 setHidden:NO]; 
    [self FlipView:YES]; 
    [view2 setHidden:YES]; 

扭轉動畫:

[view2 setHidden:NO]; 
    [self FlipView:NO]; 
    [view1 setHidden:YES]; 
0

這裏是一個沒有runloop任何黑客的解決方案。只需使用第一條消息運行到另一個視圖的轉換,然後將第二條消息返回到原始狀態:

- (void) forwardTransitionFromView: (UIView *) viewA toView: (UIView *) viewB 
{ 
    NSAssert(viewA.superview, @"The 'from' view should be added to a view hierarchy before transition"); 
    NSAssert(!viewB.superview, @"The 'to' view should NOT be added to a view hierarchy before transition"); 
    CGSize viewASize = viewA.bounds.size; 
    viewB.frame = CGRectMake(0.0, 0.0, viewASize.width, viewASize.height); 
    [UIView transitionWithView:viewA 
         duration:1.0 
         options:UIViewAnimationOptionTransitionFlipFromRight 
        animations:^{ 
         [viewA addSubview:viewB]; 
        } 
        completion:NULL]; 
} 

- (void) backwardTransitionFromView: (UIView *) viewB toView: (UIView *) viewA 
{ 
    NSAssert([viewB.superview isEqual:viewA], @"The 'from' view should be a subview of 'to' view"); 
    [UIView transitionWithView:viewA 
         duration:1.0 
         options:UIViewAnimationOptionTransitionFlipFromLeft 
        animations:^{ 
         [viewB removeFromSuperview]; 
        } 
        completion:NULL]; 
}