简体   繁体   English

如何在iPhone中创建自定义的时间栏?

[英]How to Create Customized Time Bar in iPhone?

I need to create a time bar like the image attached.The blue lines are the dates indicating some actions.The big Red Arrow mark is used to slide for selecting any of the blue line. 我需要创建一个如上图所示的时间栏。蓝线是表示某些操作的日期。大红色箭头标记用于滑动以选择任何蓝线。 At background the bar is divided into years.The small red circles are to indicate the years. 在背景栏中,该栏分为年份。红色小圆圈表示年份。

Any idea ,how do i start? 任何想法,我该如何开始?

Thanks 谢谢

演示视图

If the blue bars don't move a lot, and you don't need to resize this view very often (which is usually the case on an iPhone), then I'd recommend subclassing UIControl (or UIView) and implement it yourself. 如果蓝色条没有移动太多,并且您不需要经常调整此视图的大小(在iPhone中通常是这种情况),那么我建议您继承UIControl(或UIView)并自己实现。

Have an NSArray property storing NSDate objects and methods like addDate: , removeDate: , removeDateAtIndex: to change it's content from an other object (like your view controller). 具有一个NSArray属性,该属性存储NSDate对象和方法,例如addDate:removeDate:removeDateAtIndex: addDate:以从其他对象(例如您的视图控制器)更改其内容。 In these methods you add or remove the passed date and call [self setNeedsDisplay]; 在这些方法中,您可以添加或删除传递的日期,然后调用[self setNeedsDisplay]; to redraw the lines. 重画线条。

You will need a few methods to calculate the position (in pixels) of a date on the slider and to calculate the date at a specific coordinate. 您将需要一些方法来计算日期在滑块上的位置(以像素为单位)并在特定坐标处计算日期。 This should be easy to to (basic linear interpolation). 这应该很容易(基本线性插值)。

And assuming that the red slider is only able to point to blue lines (but not between them) add a NSDate pointer variable to the currently selected date. 并假设红色滑块只能指向蓝线(但不能指向蓝线),则将NSDate指针变量添加到当前选定的日期。 In the 在里面

In the drawRect: method, you need to draw all the lines using CoreGraphics functions. 在drawRect:方法中,您需要使用CoreGraphics函数绘制所有线条。 Look up the documentation if you need help with this. 如果您需要帮助,请查阅文档。 Apple has some great sample apps too. 苹果也有一些出色的示例应用程序。

To show the red slider, you could either add a custom UIImageView, with a UIPanGestureRecognizer and make sure it only moves vertically in the gesture handler. 要显示红色滑块,您可以添加带有UIPanGestureRecognizer的自定义UIImageView,并确保它仅在手势处理程序中垂直移动。 Or it might be possible to add a UISlider without track images (I'm not 100% sure about that). 或者,也可以添加没有轨迹图像的UISlider(我对此不十分确定)。 In either case, you need to adjust the slider's position once the user let's go and make it jump to the nearest blue line. 无论哪种情况,一旦用户离开,您都需要调整滑块的位置,使其跳至最接近的蓝线。

A rough idea, 1. Get the length of the entire TimeBar.Let it be t. 一个大概的想法:1.获取整个TimeBar.L的长度。 2. Divide by x=numberofYears*365.let it be t_eachday_pos=t/x 3. get the position of each day,calculate its offset as daycount*t_eachday_pos; 2.用x = numberofYears * 365除以t_eachday_pos = t / x。3.获取每天的头寸,将其偏移量计算为daycount * t_eachday_pos; 4. Add your blueLine as a customButton. 4.将blueLine添加为customButton。 5. To do some action, pass the x daynumber(eg. day 245 of year 2 etc.) 5.要执行某些操作,请传递x天数(例如,第2年的第245天等)

Try this, I have used the same thing in one of my app, and it is working 试试这个,我在我的一个应用程序中使用了相同的东西,并且正在工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM