我正在尝试模拟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出现在海报的位置,但根本没有翻转动画。为什么不呢?
答案 0 :(得分:1)
您告诉视图要转换为 self.view
,因此难怪为什么整个视图都是动画效果。尝试使用海报视图告诉过渡到动画。
[UIView transitionWithView:poster.view duration:3
options:UIViewAnimationOptionTransitionFlipFromLeft animations:^{
[poster removeFromSuperview];
[self.view addSubview:newView];
}
completion:NULL];
答案 1 :(得分: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];
}
];
}
抱歉我的英语不好,我不会说英语!