1

我正在尝试模拟在 iTunes iPad 应用程序中看到的翻转动画。在主要特色页面上,如果您点击新版本列表中的一张小海报,它将翻开以显示电影的所有详细信息。

当海报被点击时,我这样做:

//...create newView

[self.view addSubview:poster]; //because the poster was previously in a scrollview

[UIView transitionWithView:self.view duration:3
                   options:UIViewAnimationOptionTransitionFlipFromLeft animations:^{
                       [poster removeFromSuperview];
                       [self.view addSubview:newView];
                   }
                completion:NULL];

但是......整个视图翻转,而不仅仅是海报。它垂直翻转而不是水平翻转,即使我指定了 FlipFromLeft。我究竟做错了什么?

编辑:您似乎必须使用某种容器视图来执行此操作。所以我做了一个,添加了海报,然后创建了一个虚拟 UIView 进行测试:

CGPoint newPoint = [self.view convertPoint:CGPointMake(poster.frame.origin.x, poster.frame.origin.y) fromView:[poster superview]];
poster.frame = CGRectMake(0, 0, poster.frame.size.width, poster.frame.size.height);

UIView *containerView = [[UIView alloc] initWithFrame:CGRectMake(newPoint.x, newPoint.y, poster.frame.size.width, poster.frame.size.height)];
[self.view addSubview:containerView];
[containerView addSubview:poster];


UIView *testView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, containerView.frame.size.width, containerView.frame.size.height)];
testView.backgroundColor = [UIColor redColor];

[UIView transitionWithView:containerView duration:3
                   options:UIViewAnimationOptionTransitionFlipFromLeft animations:^{
                       [poster removeFromSuperview];
                       [containerView addSubview:testView];
                   }
                completion:NULL];

现在,红色的 UIView 出现在海报的位置,但根本没有 Flipping 动画。为什么不?

4

2 回答 2

1

您正在告诉视图使用 过渡self.view,所以难怪整个视图都在动画。试着用海报视图告诉过渡动画。

[UIView transitionWithView:poster.view duration:3
                   options:UIViewAnimationOptionTransitionFlipFromLeft animations:^{
                       [poster removeFromSuperview];
                       [self.view addSubview:newView];
                   }
                completion:NULL];
于 2011-09-30T19:20:47.173 回答
0

我用两个 UIImageView(命名为:fromImageView 和 toImageView)创建了一个 UIViewController(命名为:transitionView)。我在 viewDidLoad 中将 transitionView 添加到我的 viewController 并将 alpha 设置为 0。我从两个 UIView 创建了一个图像:

- (UIImage *) imageFromView: (UIView *) view {
    CGFloat alpha = [view alpha];
    [view setAlpha: 1.0];
    CGSize pageSize = view.frame.size;
    UIGraphicsBeginImageContext(pageSize);

    CGContextRef resizedContext = UIGraphicsGetCurrentContext();
    [view.layer renderInContext: resizedContext];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();
    [view setAlpha: alpha];
    return image;
}

这是我调整大小的翻转代码:

- (void) flipFromView: (UIView *) fromView toView: (UIView *) toView {
    // Warning: positions is wrong when your image (or view) is in UIScrollView...
    CGRect frame = CGRectMake(-8.0, 30.0, 336.0, 380.0); // resizing the fromView if the image is small...
    CGFloat interval = 1.0;

    [[transitionView view] setAlpha: 1.0]; //

    UIImage *fromViewImage = [self imageFromView: fromView];
    UIImageView *fromImageView = [transitionView fromImageView];
    [fromImageView setImage: fromViewImage];
    [fromImageView setFrame: [fromView frame]];


    UIImage *toViewImage = [self imageFromView: toView];
    UIImageView *toImageView = [transitionView toImageView];
    [toImageView setImage: toViewImage];
    [toImageView setFrame: frame];

    [fromView setHidden: YES]; // hide original while animating

    toImageView.layer.transform = CATransform3DMakeRotation(M_PI_2, 0.0, 1.0, 0.0);

    [UIView animateWithDuration: interval
                          delay: 0.0
                        options: UIViewAnimationOptionCurveEaseIn
                     animations: ^{
                         [fromImageView setFrame: frame];
                         fromImageView.layer.transform = CATransform3DMakeRotation(M_PI_2, 0.0, 1.0, 0.0);
                     } completion: ^(BOOL finished) {

                     }
     ];

    [UIView animateWithDuration: interval
                          delay: interval // wait to previous animation ending
                        options: UIViewAnimationOptionCurveEaseOut
                     animations: ^{
                         toImageView.layer.transform = CATransform3DMakeRotation(M_PI_2,  0.0, 0.0, 0.0);
                         toImageView.frame = [toView frame];
                     } completion: ^(BOOL finished) {
                         [toView setAlpha: 1.0];
                         [fromView setHidden: NO];
                     }
     ];
}

对不起,我的英语不好,我不会说英语!

于 2013-03-03T15:00:42.280 回答