首页 > uiview翻转动画轴心问题

uiview翻转动画轴心问题

我在view1的背面加上view2,view1翻转过来就显示view2,利用uiview动画UIViewAnimationOptionTransitionFlipFromRight实现。
问题是view1翻转的时候不是按照view1的中间轴心翻转,而是按self。view的轴心翻转,请问怎么解决这个问题?

[self.view1 sendSubviewToBack:self.view2]
[UIView transitionFromView:(self.view1)
toView:(self.view2)
duration: 2
options: UIViewAnimationOptionTransitionFlipFromLeft+UIViewAnimationOptionCurveEaseInOut
completion:^(BOOL finished) {
if (finished) {

                    }
                }
 ];

文档中对于这个方法的定义有这么一段:

This method provides a simple way to transition from the view in the fromView parameter to the view in the toView parameter. By default, the view in fromView is replaced in the view hierarchy by the view in toView. If both views are already part of your view hierarchy, you can include the UIViewAnimationOptionShowHideTransitionViews option in the options parameter to simply hide or show them.

也就是说,将from从父类移除,将to移入。动画是基于父视图的。所以只需要在 view1和view2外面套上一个 container view 就可以了。

代码如下:

 UIView *containerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200.0f, 200.0f)];

    [self.view addSubview:containerView];

    UIView *fromView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, containerView.frame.size.width, containerView.frame.size.height)];
    fromView.backgroundColor = [UIColor blueColor];
    UIView *toView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, containerView.frame.size.width, containerView.frame.size.height)];
    toView.backgroundColor = [UIColor purpleColor];
    [containerView addSubview:fromView];

    [self.view addSubview:fromView];

    [CATransaction flush];

    [UIView transitionFromView:fromView toView:toView duration:0.4f options:UIViewAnimationOptionTransitionFlipFromLeft completion:NULL];
【热门文章】
【热门文章】