iOS基础 -- UISlider

/**
     *  UISlider
     */
    
    self.slider = [[UISlider alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 200.0f, 23.0f)];
    self.slider.center = self.view.center;
    self.slider.minimumValue = 0.0f;
    self.slider.maximumValue = 100.0f;
    self.slider.value = self.slider.maximumValue / 2.0;
    [self.slider addTarget:self action:@selector(sliderValueChanged:) forControlEvents:UIControlEventValueChanged];
    self.slider.continuous = NO;//is always callback the selector
    //[self.slider setThumbImage:[UIImage imageNamed:@"2.png"] forState:UIControlStateNormal];
    //[self.slider setThumbImage:[UIImage imageNamed:@"3.png"] forState:UIControlStateHighlighted];
    self.slider.minimumTrackTintColor = [UIColor redColor];
    self.slider.maximumTrackTintColor = [UIColor greenColor];
    self.slider.thumbTintColor = [UIColor blackColor];
    
    ///image should be 23*23 width*tall and retina twice
    //[self.slider setMinimumValueImage:[UIImage imageNamed:@"test.png"]];
    //[self.slider setMaximumValueImage:[UIImage imageNamed:@"test.png"]];
    //[self.slider setThumbImage:[UIImage imageNamed:@"test.png"] forState:UIControlStateNormal];
    
    ///image should be 11*9 width*tall resizable image
    //[self.slider setMinimumTrackImage:[self minimumTrackImage] forState:UIControlStateNormal];
    //[self.slider setMaximumTrackImage:[self maximumTrackImage] forState:UIControlStateNormal];
    [self.view addSubview:self.slider];

以上是添加UISlider到当前view。target方法如下:

-(void) sliderValueChanged: (UISlider *)paramSender
{
    if([paramSender isEqual:self.slider])
        NSLog(@"%f",paramSender.value);
}

这里跟continuous属性相关,确定上面的回调方法是持续调用,还是稳定后调用。

在iOS7中,minimum和maximum的track images只有1点宽,所以给它设置一个图片是完全无用,看起来也不会好看。所以呢,在iOS7中还是推荐使用tint colors。


你可能感兴趣的:(iOS)