在iPhone上使用UIKit实施量规控件 [英] Implementing a gauge control with UIKit on the iPhone

查看:63
本文介绍了在iPhone上使用UIKit实施量规控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于我正在开发的应用程序,我想实现某种类似仪表的控件.压力表应垂直放置,我想获得刻度盘上打印的值垂直移动的效果,以使当前值出现在压力表的中心.当前不可见的部分会被剪掉.

For an application I'm working on, I'd like to implement sort of a gauge-like control. The gauge should be of vertical orientation and I'd like to get the effect of a dial with the values printed on it to move vertically so that the current value appears at the center of the gauge. While the part of the dial that is not currently visible gets clipped.

我认为一种好方法是让一个UIView充当顶部可见部分(如果需要的话,可以进入仪表的窗口)和一个仅用值和标记绘制表盘的子视图.顶视图将收到一条消息,指示要显示的值,并在子视图周围移动,以便当前值居中,而当前不可见的刻度盘部分被剪裁.我试图对此做一点说明(应将表盘的变灰部分剪掉):

I thought a good approach would be to have one UIView that acts as the top visible part (the gauge's window if you will) and a subview that just draws the dial with the values and markers. The top view would receive a message indicating the value to display and move the subview around so that the current value is centered while the part of the dial that is not currently visible gets clipped. I tried to do a little illustration of this (the part of the dial that is grayed-out should be clipped):

我不确定如何设置视图,以便实际上可以使它工作.我尝试了两个UIView子类,但是呈现表盘的视图只会在屏幕上某个意外的位置绘制,而不会被父视图剪裁.

I'm am unsure how to set up the views so that I can actually get this to work. I tried around with two UIView subclasses but the view rendering the dial would just paint at some unexpected location on the screen and wouldn't be clipped by the parent view.

我实际上只是在寻找实现这样的控件所需的部件的一般建议.我试图描述时使用两个视图是否有意义,还是应该将其实现为一个UIView子类,该子类仅弄清楚在值更改时要绘制表盘的哪一部分?或者也许是完全不同的东西?

I'm really just looking for general advice on the parts needed to implement a control like this. Does it make sense to use two views as I tried to describe or should I implement this as one single UIView subclass that just figures out which part of the dial to draw whenever the value is changed? Or maybe something completely different?

推荐答案

查看UIScrollView类.

根据我在您的问题中所读的内容,这应该可以为您提供所需的内容.

From what I read in your question, this should give you what you need.

滚动视图充当内容视图顶部的一种窗口".如果内容视图较大,则会被滚动视图裁剪.

The scroll view acts as a kind of 'window' on top of a content view. The content view is clipped by the scroll view if it is larger.

因此,您可能拥有一个内容视图,也就是说,出于参数考虑,50x320,滚动视图仅为50 x50.内容视图的区域将被裁剪为50x50的大小,您将能够像其他本机iPhone控件一样,通过用手指滚动来移动内容视图.

So you could have a content view is that is say, for arguments sake, 50x320, and the scroll view be only 50 x 50. The area of the content view will be clipped to the 50x50 size, and you will be able to move the content view by scrolling with your finger, like other native iPhone controls.

许多(如果不是全部)iPhone应用程序以某种方式使用滚动视图. UITableViewUITextView是滚动视图的子类.

Many, if not all, iPhone apps use a scroll view in some way. UITableViews are subclasses of scroll views, as are UITextViews.

Apple有一个不错的示例应用程序,显示了如何使用滚动视图,因此也请检查一下.

Apple has a decent example app that shows how to use a scroll view, so check that out too.

这篇关于在iPhone上使用UIKit实施量规控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆