获取自定义 UIView 子类以跟随 UIControl

发布于 2024-12-19 01:35:15 字数 413 浏览 1 评论 0原文

我有一个 UIControl 的子类,它代表一个滑块开关,带有一个变量“百分比”,用于存储滑块的当前位置,即 0.0 表示滑块在左侧,1.0 表示滑块在右侧。我有两张看起来像气泡的图像,当用户单击滑块时,我想显示其中一张图像(它们大小相同,但看起来不同,设计如下:如果百分比 <= 0.5,则显示一张图像,另一张则显示如果百分比> 0.5)并使对话气泡的“尾部”跟随滑块(即,框架被设置为百分比的函数)。

我为图像创建了一个自定义 UIView,它通过布尔参数更改要显示的图像。我尝试在 UIControl 子类中创建一个参数,用于绘制 UIView 以及所有其他绘图,但我需要将其绘制在 UIControl 的矩形之外,而这无法从 UIControl 的 drawRect: 方法内部完成。管理这两个视图并确保当 UIControl 的百分比值发生变化时气泡始终更新的好方法是什么?

I've got a subclass of UIControl which represents a slider switch, with a variable 'percent' which stores the current position of the slider, i.e. 0.0 means the slider is on the left, 1.0 means the slider is on the right. I have two images which look like speech bubbles, and when the user clicks the slider I want to display one of these images (they are the same size but look different, designed so one is displayed if percent <= 0.5 and the other one if percent > 0.5) and have the 'tail' of the speech bubble follow the slider (i.e. the frame is set as a function of the percentage).

I have created a custom UIView for the image which changes the image to display via a boolean parameter. I have tried creating a parameter in the UIControl subclass that draws the UIView along with all the other drawing, but I need to draw it outside the UIControl's rect, which can't be done from inside the UIControl's drawRect: method. What would be a good way of managing these two views and making sure the speech bubble always updates when the percentage value of the UIControl changes?

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

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

发布评论

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

评论(4

风情万种。 2024-12-26 01:35:15

听起来你的 UIControl 应该简单地是:

  • 足够高以包含栏和气泡视图
  • 透明(以便您可以看到气泡“滑动”的视图部分的后面)

或者我错过了什么?

Sounds like your UIControl should simply be:

  • Tall enough to contain the bar and bubble view
  • Transparent (so that you can see behind the part of the view where the bubble "slides")

Or am I missing something?

心碎无痕… 2024-12-26 01:35:15

首先,确保 连续 属性 在 UISlider 中设置为 TRUE。

然后,确保您有一个 IBAction 来接收来自 UISlider 的更新事件。您可以在 Interface Builder(内置于 XCode 4 中)中轻松建立连接,也可以使用 UIControl 的 addTarget:action:forControlEvents: 方法。

最后,在 IBAction 中,您现在可以更改“语音气泡”UIView 的框架(确保将其作为 @interface .h 文件中的成员链接到父视图控制器)。它相对于滑块的显示位置留给您作为家庭作业(因为它实际上是特定于实现的 - 或者,换句话说,它的显示方式取决于每个单独的应用程序和程序员)。

我希望这些信息对您有所帮助!

First off, make sure the continuous property is set to TRUE in your UISlider.

Then, make sure you have an IBAction in place to receive update events from your UISlider. You can make the connection easily in Interface Builder (built into XCode 4) or you can do it programatically using UIControl's addTarget:action:forControlEvents: method.

And lastly, inside that IBAction you can now change the frame of your "speech bubble" UIView (make sure to link that up to your parent view controller as a member in your @interface .h file). Where it appears in relation to the slider is left to you as a homework assignment (cause it's really implementation specific -- or, to put it another way, how it appears is up to each individual app & programmer).

I hope this information helps you out!

可爱暴击 2024-12-26 01:35:15

感谢您的回答 - 事实证明,最好的方法是为我的自定义 UIControl 声明委托协议,并将语音气泡视图指定为委托。然后,我创建了一个方法,告诉语音气泡更新位置(传入位置),并在控件位置更新时从 UIControl 调用该方法。

Thanks for the answers - it turned out the best way to do it was to declare a delegate protocol for my custom UIControl, and assign the speech bubble view as the delegate. Then I made a method that tells the speech bubble to update location (passing in the location) and called that from the UIControl whenever the position of the control updated.

终止放荡 2024-12-26 01:35:15

可能更简单的方法是向 UIControl 添加一个指向 UIView 的属性,并在更改控件状态的方法中移动它。当然,要做到这一点,您需要创建一个像 MyCustomControl 这样扩展 UIControl 的类。

Probably the simplier way is to add to the UIControl a property pointing to the UIView and move it around in the method(s) where you change the control state. Of course to do that you need to create a class like MyCustomControl that extends UIControl.

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