UISlider步骤不均匀。

时间:2014-03-18 10:07:07

标签: ios objective-c ios7 uikit uislider

我需要实现一个滑块控件,如下图

滑块的长度不均匀。 每当用户滑动时,滑块滑块将仅停留在接近当前范围的步骤之一。

但真正的挑战是UISlider在最初提供的自定义点(范围)上带点。

任何人都可以帮助我实现此功能。

1 个答案:

答案 0 :(得分:1)

我认为你最好不要试图将UISlider变成这种配置

如果这是我的问题,我会构建一个像这样结构的自定义视图

 DKCustomSliderView
 -> UIImageView (subview for slider head)

实现了这样的接口。

@interface DKCustomSliderView : UIView

-(void)setStopPoints:(NSArray *)stopPoints; //array of NSNumbers between 0-1 which define indents
-(void)setSliderHeadImage:(UIImage *)sliderHeadImage; //image to use as slider head
-(void)setIndentImage:(UIImage *)indentImage; //image to use as indent marker
-(void)setTrackImage:(UIImage *)trackImage; //stretchy image to use on track 

@end

我会在滑块图片视图上跟踪平移手势。

-(void)handlePanGesture:(UIPanGestureRecogniser *)pgr {

if(pgr.state == UIGestureRecogniserStateChanged) { 

   //am i near an indent? , if so lock me here and be slightly sticky.

}
}

并且DKCustomSliderView的绘制矩形看起来有点像这样。

-(void)drawRect:(CGRect)dirtyRect {

CGRect bounds = self.bounds;

// 1. draw track across width

for(NSNumber *marker in self.stopPoints) {

//draw a instance of self.indentImage at ([marker floatValue]/bounds.size.width)

}

}