ObjC - UIViews:独立地动画视图和子视图?

发布于 2024-09-26 23:29:58 字数 843 浏览 0 评论 0原文

我有一个 UIView 及其子视图。当我对 myView (size.x+20) 进行动画处理时,我的子视图也进行了动画处理,但是我想同时独立地平移我的子视图 (origin.x-40)(子视图不会因为 myView 的大小调整而移动)。

我可以通过调整翻译(或更好的位置,因为我稍后可能想更改 y) .fromValue、.toValue 和 .duration 来补偿由 myView 更改引起的移动。

UIView beginAnimations:nil context:NULL];
[myView setFrame:pVnewFrame];
[UIView commitAnimations];

CABasicAnimation *bBTranslateAnimation = [CABasicAnimation animationWithKeyPath:@"position"]; 
bBTranslateAnimation.delegate = self;
bBTranslateAnimation.fromValue = [NSValue valueWithCGPoint:CGPointMake(160, 480)];
bBTranslateAnimation.toValue = [NSValue valueWithCGPoint:CGPointMake(160, 436)];
bBTranslateAnimation.duration = 0.35;
[buttonBar addAnimation:bBTranslateAnimation forKey:@"anim"];

但这不是正确的解决方案。有没有独立移动视图和子视图的好方法? (设置 autoresizeSubviews 和 autoresizingMask 没有帮助)

提前致谢!

I have an UIView and its subview. When I animate myView (size.x+20) my subview is being animated too, however I want to translate my subview at the same time independently(origin.x-40) (without the subview moving because of the resizing of myView).

I was able to do it by adjusting the translations (or better position, because I might wanna change y later) .fromValue, .toValue and .duration to compensate movement caused by changes to myView.

UIView beginAnimations:nil context:NULL];
[myView setFrame:pVnewFrame];
[UIView commitAnimations];

CABasicAnimation *bBTranslateAnimation = [CABasicAnimation animationWithKeyPath:@"position"]; 
bBTranslateAnimation.delegate = self;
bBTranslateAnimation.fromValue = [NSValue valueWithCGPoint:CGPointMake(160, 480)];
bBTranslateAnimation.toValue = [NSValue valueWithCGPoint:CGPointMake(160, 436)];
bBTranslateAnimation.duration = 0.35;
[buttonBar addAnimation:bBTranslateAnimation forKey:@"anim"];

But that is no proper solution. Is there a good way to move view and subviews independently?
(setting autoresizeSubviews and autoresizingMask doesn't help)

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冷…雨湿花 2024-10-03 23:29:58

执行此操作的方法是使两个视图成为同级视图,而不是使一个视图成为另一个视图的子视图。只需将作为子视图的视图放在作为父视图的视图之上,它应该看起来像您想要的那样。

如果您希望“子”视图剪辑到前一个父视图的内容,则可能需要将子视图放入容器视图中,该容器视图是父视图的同级视图,并设置容器视图不透明=否并且具有清晰的背景颜色。然而,这会影响性能。

The way to do this is to make the two views siblings, instead of making one view a subview of the other. Just put the view that was the subview on top of the view that was the parent view, and it should look like you want it to.

If you want the "child" view to clip to the contents of the former parent view, you might need to put the view that was the child inside a container view that is a sibling of the view that was the parent, and set the container view to opaque = no and with a clear background color. That will affect performance, however.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文