我有这个 subview ,我想将其添加到我的主视图中,但要使其从原点展开。我阅读了一些 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];
但是还是不行!
编辑:
根据建议,我已将其移至基于 block 的动画解决方案,这是确切的代码:
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) {
}];
由于某种原因,这仍然不起作用。我看到的可能问题是:
- 我正在从一个具体为 310 x 150 的
nib
加载此 customView,这可能会导致问题。 - 我没有导入正确的框架,但是因为我可以为这个 View 的 frame.origin 部分设置动画,所以我不确定情况是否如此......我有
QuartzCore
和Core Graphics
所有进口的东西。
它在日志中的每个点都给出了正确的内容:例如,在目标帧之前,大小为 0、150,而在我将帧设置为 310、150 之后。但是动画不起作用!
最佳答案
要使 View “增长”到位,请不要为框架设置动画。动画转换。
从 Nib 加载您的 subview 。将其变换设置为 0 的比例:
view.transform = CGAffineTransformMakeScale(0,0);
然后将其添加到您的 super View 中。
在动画 block 中,将变换设置为恒等式:
view.transform = CGAffineTransformIdentity;
并且 View 将增长到正常大小。您可能需要摆弄 anchor 以使其从正确的点开始生长。
您还可以更改 block 内的框架,但如果仅移动 View ,我更喜欢更改中心属性,如果您也有转换,则不应尝试设置框架。
对于奖励积分,您还可以将动画设置为略大于 1.0 的比例,然后在完成 block 中以链接动画的形式设置为恒等变换。这使得 View 像警报 View 一样“弹出”屏幕。
关于ios - 如何在 Xcode 中为 UIView 的宽度和高度设置动画?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12852333/