查看动画问题

时间:2013-12-18 14:42:01

标签: ios cocoa-touch uiviewanimationtransition

我正在尝试将视图动画转换为另一个视图,该视图对应于屏幕的一小部分。两个视图具有相同的尺寸和中心。 我不断得到全屏动画。从下面的代码中,有人可以让我知道我做错了什么?

谢谢,

-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]];

    }
}

3 个答案:

答案 0 :(得分: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]];
    }
}

其次,要转换的子视图的超级视图实际上是要进行转换。因此,这意味着您必须将viewA和viewB添加到子视图,将此子视图添加到另一个视图,然后执行动画。

答案 1 :(得分:0)

这是一个没有任何乱跑的解决方案。只需使用第一条消息运行转换到另一个视图,然后使用第二条消息返回原始状态:

- (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];
}

答案 2 :(得分:-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];