当 UIControl 的数据发生变化时,如何对其调整大小做出反应
- 我构建了一个 UIControl 子类来显示 1 个月的日历视图
一部 iPhone。大多数月份需要 5 周才能显示日期,但是
有些月份需要 6 个,所以我构建了动态调整大小的控件
本身根据需要。我正在使用 UIView 动画来更改框架
控制在这里。 - 问题是,我现在需要屏幕上的其他控件
当日历改变大小时移动/调整大小。我真的需要那个
日历控件的动画改变大小时发生的情况。
理想情况下,我不会在日历中编写大量细节来完成此操作
控制屏幕上的其他控件。 - 这里最好的策略是什么?我希望我能以某种方式锚定
其他控件到日历控件的框架并具有
平台在动画帧变化时调整它们的位置/大小。
但是,到目前为止,我找不到任何支柱和弹簧的组合来
让这一切发生。 - 我是否只需要硬着头皮添加其他屏幕控件
我的日历控件内发生的动画?
- I've built a UIControl subclass to display a 1-month calendar view on
an iPhone. Most months require 5 weeks to display the dates, but
some months need 6, so I've built the control to dynamically resize
itself as needed. I'm using UIView animation to change the frame of
the control here. - The problem is, I now need the other controls on the screen to
move/resize when the calendar changes size. And I really need that
to happen with the animation of the calendar control changing size.
Ideally, I'd do this without coding a bunch of details in my calendar
control about other controls on the screen. - What's the best strategy here? I was hoping I could somehow anchor
the other controls to the frame of the calendar control and have the
platform adjust their location/size as it animates the frame change.
But, thus far, I can't find any combination of struts and springs to
make that happen. - Do I just need to bite the bullet and add my other on-screen controls
to the animation happening inside my calendar control?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我有兴趣看看是否有更好的答案。
此时,我所知道要做的就是重写日历视图上的
setFrame
,并在其更改时将setNeedsLayout
发送到其超级视图。但我不确定标准视图是否会正确自动调整大小。一般来说,几何图形沿着视图树向下流动,而不是向上流动,并且您希望它同时执行这两个操作。
您可能必须在包含视图上实现layoutSubviews。
I'll be interested to see if there are better answers to this.
At this point, all I know to do is override
setFrame
on your calendar view and when it changes, sendsetNeedsLayout
to its superview.But I'm not sure if standard views will autoresize this correctly. Generally geometry flows down the view tree, not up, and you want it to do both.
You may have to implement layoutSubviews on the containing view.
将动画逻辑从特定视图移出并移至管理所有控件的视图控制器中。视图仍然可以计算出自己的正确大小,并让视图控制器询问。例如:
另一种类似的方法:
根据您的喜好,有很多类似的方法。但关键是将逻辑移出具体视图。通常视图控制器是最好的解决方案。如果控件创建一个可以进入单个
UIView
的逻辑集合,那么您可以让该“集合”UIView
在其layoutSubviews
中管理相同的内容code>,但更常见的是视图控制器是更好的解决方案。Move the animation logic out of the specific view and into a view controller that manages all of the controls. The view can still figure out its own proper size, and let the view controller ask. For instance:
Another, similar approach:
There are lots of similar approaches based on your preferences. But the key is to move the logic out of the specific view. Usually a view controller is the best solution. If the controls make a logical collection that could go into a single
UIView
, then you could have that "collection"UIView
manage the same thing in itslayoutSubviews
, but it's more common that a view controller is a better solution here.感谢您的帮助。我尝试了这两种方法并取得了成功。 setFrame 的重写和layoutSubviews 的实现有效,但其他控件会跳转到新位置,而不是随着日历控件的增长而动画到这些位置。
在动画过程中移动其他控件的方法是我必须采用的方法。但是,我希望保持控件的逻辑非常独立以供重用,因此我将动画保留在控件中,但添加了一个委托来对大小变化做出反应,并将委托调用放在动画块内。完全公开的是,当我增长控件时,我还会将新月份的日历动画化到控件上,这样我就可以在动画块中完成这两件事。
所以,最终代码看起来像这样:
Thanks for the help. I tried both of these approaches with success. The override of setFrame and implementation of layoutSubviews worked, but the other controls jumped to their new locations rather than animating to those locations as the calendar control grew.
The approach of moving the other controls during the animation is what I had to go with. However, I wanted to keep the logic of the control pretty self-contained for re-use, so I left the animation in the control but added a delegate to react to size change, and put the delegate call inside the animation block. In full disclosure, I am also animating the new month's calendar onto the control while I'm growing the control, and this way I could do both of those things in the animation block.
So, the end code looks something like this: