iOS 自定义UISlider

 添加下面代码就可以了

-(void)addMySlider{

    //左右轨的图片
    UIImage *stetchLeftTrack= [UIImage imageNamed:@"light_off_on.png"];
    UIImage *stetchRightTrack = [UIImage imageNamed:@"light_off_back.png"];
    //滑块图片
    UIImage *thumbImage = [UIImage imageNamed:@"light_round.png"];
    
    UISlider *sliderA=[[UISlider alloc]initWithFrame:CGRectMake(30, 320, 257, 7)];
    sliderA.backgroundColor = [UIColor clearColor];
    sliderA.value=0.5;
    sliderA.minimumValue=0.0;
    sliderA.maximumValue=1.0;
    
    [sliderA setMinimumTrackImage:stetchLeftTrack forState:UIControlStateNormal];
    [sliderA setMaximumTrackImage:stetchRightTrack forState:UIControlStateNormal];
    //注意这里要加UIControlStateHightlighted的状态,否则当拖动滑块时滑块将变成原生的控件
    [sliderA setThumbImage:thumbImage forState:UIControlStateHighlighted];
    [sliderA setThumbImage:thumbImage forState:UIControlStateNormal];
    //滑块拖动时的事件
    [sliderA addTarget:self action:@selector(sliderValueChanged) forControlEvents:UIControlEventValueChanged];
    //滑动拖动后的事件
    [sliderA addTarget:self action:@selector(sliderDragUp) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:sliderA];


}
-(void)sliderDragUp{
    
    NSLog(@"sliderDragUp");
}

-(void)sliderValueChanged{
    
    NSLog(@"sliderValueChanged");


}






你可能感兴趣的:(iOS 自定义UISlider)