滑动条UISlider

/*_____________________________UISlider_____________________________*/
- (void)_initSlider {

    //创建滑动条
    UISlider *slider = [[UISlider alloc] initWithFrame:CGRectMake(20, 300, 200, 20)];
    //设置最值
    slider.minimumValue = 0;
    slider.maximumValue = 1;
    
    //设置slider的初始值
    slider.value = .5;
//    [slider setValue:.5 animated:YES];
    
    //设置滑动按钮的图片
    [slider setThumbImage:[UIImage imageNamed:@"com_thumb_max_n-Decoded"] forState:UIControlStateNormal];
    [slider setThumbImage:[UIImage imageNamed:@"com_thumb_max_h-Decoded"] forState:UIControlStateHighlighted];
    
    UIImage *minImg = [UIImage imageNamed:@"com_slider_min_l-Decoded"];
    UIImage *changeImg = [UIImage imageNamed:@"com_slider_max_l-Decoded"];
    
    //设置图片的拉伸点
    minImg = [minImg stretchableImageWithLeftCapWidth:10 topCapHeight:0];
    changeImg = [changeImg stretchableImageWithLeftCapWidth:10 topCapHeight:0];
//
    //设置左侧的滑动条图片
    [slider setMinimumTrackImage:minImg forState:UIControlStateNormal];
    [slider setMaximumTrackImage:changeImg forState:UIControlStateNormal];
    
    //设置滑动条的左侧的颜色
//    [slider setMinimumTrackTintColor:[UIColor redColor]];
    //设置滑动条的右侧的颜色
//    [slider setMaximumTrackTintColor:[UIColor greenColor]];
    
    [slider addTarget:self action:@selector(sliderAction:) forControlEvents:UIControlEventValueChanged];
    
    [self.view addSubview:slider];
    
}

- (void)sliderAction:(UISlider *)slider {

    float value = slider.value;
    NSLog(@"滑动值:%.2f",value);
    
}

你可能感兴趣的:(UI初级)