Implementing a gauge control with UIKit on the iPhone

江枫思渺然 提交于 2019-12-30 14:49:22

问题


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.

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):

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.

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?


回答1:


Look into the UIScrollView class.

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.

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.

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

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



来源:https://stackoverflow.com/questions/2220650/implementing-a-gauge-control-with-uikit-on-the-iphone

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!