如何用SUBVIEWS动画缩放UIView?

时间:2013-01-05 13:38:29

标签: objective-c ios cocoa-touch uiview uiviewanimation

此代码执行以下操作:它需要一个NavigationController的“快照”,意味着消失。然后将快照添加到容器视图控制器的视图中,滑动并显示新的NavigationController。要创建“弹出效果”, midAnFrame 会使视图比实际视图大一些。之后 postAnFrame 提供滑出容器视图的框架。效果很好,唯一的问题是 animationContainer 的子视图在动画期间不会像超级视图那样调整大小。

- (void)bringUpSettingsNavigationController {
CGFloat gradientWidth = 20;
CGFloat frameIncrease = 10;
CGRect preAnFrame  = CGRectMake(- gradientWidth, 0, self.view.frame.size.width + gradientWidth, self.view.frame.size.height);
CGRect midAnFrame  = CGRectMake(-(frameIncrease + gradientWidth), - frameIncrease, preAnFrame.size.width + (2*frameIncrease), preAnFrame.size.height + (2*frameIncrease));
CGRect postAnFrame = CGRectMake(midAnFrame.size.width, midAnFrame.origin.y, midAnFrame.size.width, midAnFrame.size.height);

UIView *animationContainer = [[UIView alloc]initWithFrame:preAnFrame];
animationContainer.backgroundColor = [UIColor clearColor];
animationContainer.autoresizesSubviews = YES;

self.topImageView = [[UIImageView alloc]initWithImage:[self topImageFromView:self.view]];
self.topImageView.frame = CGRectMake(gradientWidth, 0, preAnFrame.size.width - gradientWidth, preAnFrame.size.height);
[animationContainer addSubview:self.topImageView];
self.topImageView.clipsToBounds = YES;
UIView *gradientView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, gradientWidth, preAnFrame.size.height)];
CAGradientLayer *gradientLayer = [CAGradientLayer layer];
gradientLayer.frame = gradientView.bounds;
gradientLayer.colors = [NSArray arrayWithObjects:(id)[[UIColor clearColor] CGColor], (id)[[UIColor colorWithWhite:0 alpha:.5] CGColor], nil];
gradientLayer.startPoint = CGPointMake(0, 0.5);
gradientLayer.endPoint = CGPointMake(1.0, 0.5);
[gradientView.layer insertSublayer:gradientLayer atIndex:0];
gradientView.backgroundColor = [UIColor clearColor];
[animationContainer addSubview:gradientView];
gradientView.clipsToBounds = YES;

[self.view addSubview:animationContainer];

BYSettingsNavigationController *settingsNavigationController = [[BYSettingsNavigationController alloc]initWithRootViewController:[[BYSettingsViewController alloc]init]];
[self addChildViewController:settingsNavigationController];
settingsNavigationController.view.frame = self.view.bounds;
[self.view insertSubview:settingsNavigationController.view belowSubview:animationContainer];
[settingsNavigationController didMoveToParentViewController:self];

[UIView animateWithDuration:0.2 animations:^{
    animationContainer.frame = midAnFrame;
} completion:^(BOOL finished) {
    [UIView animateWithDuration:.5 animations:^{
        animationContainer.frame = postAnFrame;
    } completion:^(BOOL finished) {
        [self.topImageView removeFromSuperview];
        [gradientView removeFromSuperview];
        self.topImageView = nil;
    }];
}];

[self.currentNavigationController willMoveToParentViewController:nil];
[self.currentNavigationController.view removeFromSuperview];
[self.currentNavigationController removeFromParentViewController];
}

check the lower right corner

提前感谢您的想法并祝2013年成功!

的Dario

1 个答案:

答案 0 :(得分:2)

您正在做的不是放大,只是改变帧大小。 要放大,您需要更改视图的转换。

你需要做的就是沿着这条路走下去:

view.transform = CGAffineTransformMakeScale (2.0 , 2.0);

例如缩放X2