【发布时间】:2012-10-02 20:55:06
【问题描述】:
我想将这个子视图添加到我的主视图中,但要使其从原点扩展。我阅读了一些 Apple 文档,但我不明白我在哪里犯了错误。我可以为框架的原点设置动画,即让它从任何地方滑入,但宽度/高度似乎没有动画。代码如下:
[UIView beginAnimations:@"animateAddContentView" context:nil];
[UIView setAnimationDuration:0.4];
customView.frame= CGRectMake(self.view.frame.origin.x +5, self.view.frame.origin.y +5, 0, 150);
[self.view addSubview:customView];
customView.frame= CGRectMake(self.view.frame.origin.x +5, self.view.frame.origin.y +5, 310, 150);
[UIView commitAnimations];
我也尝试过只将 setFrame 部分放入动画中,如下所示:
customView.frame= CGRectMake(self.view.frame.origin.x +5, self.view.frame.origin.y +5, 0, 150);
[self.view addSubview:customView];
[UIView beginAnimations:@"animateAddContentView" context:nil];
[UIView setAnimationDuration:0.4];
customView.frame= CGRectMake(self.view.frame.origin.x +5, self.view.frame.origin.y +5, 310, 150);
[UIView commitAnimations];
但是还是不行!
编辑:
根据建议,我已将其移至基于块的动画解决方案,这是确切的代码:
NSLog(@"yourview : %@",customView);
customView.frame= CGRectMake(self.view.frame.origin.x +5, self.view.frame.origin.y +5, 0, 150);
NSLog(@"yourview : %@",customView);
[self.view addSubview:customView];
NSLog(@"yourview : %@",customView);
// [customView setFrame:CGRectMake( 0.0f, 480.0f, customView.frame.size.width, customView.frame.size.height)];
[UIView animateWithDuration:0.4
delay:0
options:UIViewAnimationCurveEaseInOut
animations:^ {
NSLog(@"yourview : %@",customView);
customView.frame = CGRectMake(self.view.frame.origin.x +5, self.view.frame.origin.y +5, 310, 150);
NSLog(@"yourview : %@",customView);
}completion:^(BOOL finished) {
}];
由于某种原因仍然无法正常工作。我看到的可能问题是:
- 我正在从
nib加载这个 customView,具体是 310 x 150,这可能会导致问题。 - 我没有导入正确的框架,但由于我可以为这个视图的 frame.origin 部分设置动画,我不确定是不是这样...我有
QuartzCore和Core Graphics所有导入的东西。
在日志中的每一点,它都给出了正确的东西:例如,在目标帧之前,大小是 0、150,而在我设置帧之后,它的大小是 310、150。但是动画不起作用!
【问题讨论】:
-
您不能为视图的高度或宽度设置动画。您只能为其原点设置动画。如果你需要获得增长的效果,规模转型是实现它的唯一途径。比例变换的棘手部分是它总是从中心、垂直、水平或两者同时增长。因此,如果您希望它从给定点下降,您将不得不在那里做一些工作。
标签: ios xcode animation uiview uiviewanimation