当 UIControl 的数据发生变化时,如何对其调整大小做出反应

发布于 2024-12-29 09:31:31 字数 418 浏览 0 评论 0原文

  • 我构建了一个 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 技术交流群。

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

发布评论

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

评论(3

哎呦我呸! 2025-01-05 09:31:31

我有兴趣看看是否有更好的答案。

此时,我所知道要做的就是重写日历视图上的 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, send setNeedsLayout 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.

不回头走下去 2025-01-05 09:31:31

将动画逻辑从特定视图移出并移至管理所有控件的视图控制器中。视图仍然可以计算出自己的正确大小,并让视图控制器询问。例如:

[self.calendarView setDate:date];
CGSize calendarSize = [self.calendarView sizeForDate:date];
[UIView animateWithDuration:...
        animations:^{
         ... re-layout everything, including the calendarView ...
        }];

另一种类似的方法:

[self.calendarView setDate:date];
[UIView animateWithDuration:...
        animations:^{
         [self.calendarView sizeToFit];
         ... re-layout everything else ...
        }];

根据您的喜好,有很多类似的方法。但关键是将逻辑移出具体视图。通常视图控制器是最好的解决方案。如果控件创建一个可以进入单个 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:

[self.calendarView setDate:date];
CGSize calendarSize = [self.calendarView sizeForDate:date];
[UIView animateWithDuration:...
        animations:^{
         ... re-layout everything, including the calendarView ...
        }];

Another, similar approach:

[self.calendarView setDate:date];
[UIView animateWithDuration:...
        animations:^{
         [self.calendarView sizeToFit];
         ... re-layout everything else ...
        }];

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 its layoutSubviews, but it's more common that a view controller is a better solution here.

丶情人眼里出诗心の 2025-01-05 09:31:31

感谢您的帮助。我尝试了这两种方法并取得了成功。 setFrame 的重写和layoutSubviews 的实现有效,但其他控件会跳转到新位置,而不是随着日历控件的增长而动画到这些位置。

在动画过程中移动其他控件的方法是我必须采用的方法。但是,我希望保持控件的逻辑非常独立以供重用,因此我将动画保留在控件中,但添加了一个委托来对大小变化做出反应,并将委托调用放在动画块内。完全公开的是,当我增长控件时,我还会将新月份的日历动画化到控件上,这样我就可以在动画块中完成这两件事。

所以,最终代码看起来像这样:

// inside the calendar control:
[UIView animateWithDuration:0.5 animations:^{
    self.frame = newOverallFrame;  // animate the growth of the control
    _offScreenCalendar.frame = newOnScreenFrame; // animate new month on screen
    _onScreenCalendar.frame = newOffScreenFrame; // animate old month off screen
    if (self.delegate)
    {
        [self.delegate handleCalendarControlSizeChange:newOverallFrame]; // animate other controls per the delegate
    }
}];

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:

// inside the calendar control:
[UIView animateWithDuration:0.5 animations:^{
    self.frame = newOverallFrame;  // animate the growth of the control
    _offScreenCalendar.frame = newOnScreenFrame; // animate new month on screen
    _onScreenCalendar.frame = newOffScreenFrame; // animate old month off screen
    if (self.delegate)
    {
        [self.delegate handleCalendarControlSizeChange:newOverallFrame]; // animate other controls per the delegate
    }
}];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文