UISlider自定义总结

1. 使用UISlider控件。

    UISlider *progressSlider = [[UISlider alloc]initWithFrame:CGRectMake(0, 0, 300, 10)];

    progressSlider.center = CGPointMake(UIScreenW/2, view.frame.size.height*0.3);
    progressSlider.maximumValue = 300;
    progressSlider.minimumValue = 0;

    [progressSlider addTarget:self action:@selector(changeValue:) forControlEvents:UIControlEventValueChanged]; //changeValue滑块变动时调用的方法。

    [progressSlider addTarget:self action:@selector(changeValue:) forControlEvents:UIControlEventTouchUpInsert];//changeValue滑块变动后调用的方法。

    progressSlider.value = 200;

   //progressSlider条,自定义图片(2 或者 3)

    [self addSubView:progressSlider];


2.拉伸图片《拖动时左边图片全部显示,会随着滑块被拉伸。》

    UIImage *minimumValueImage = [[UIImage imageNamed:@"minImg.png"]stretchableImageWithLeftCapWidth:progressSlider.value topCapHeight:0.0];
    UIImage *maxmumValueImage = [[UIImage imageNamed:@"maxImg.png"]stretchableImageWithLeftCapWidth:progressSlider.maximumValue - progressSlider.value   topCapHeight:0.0];

    [progressSlider setMinimumTrackImage:minimumValueImage forState:UIControlStateNormal];
    [progressSlider setMaximumTrackImage:maxmumValueImage forState:UIControlStateNormal];


3.显示与否《设置两张图片位置大小固定,随滑块显示部分的多少会发生变化》

    UIImage *minimumValueImage = [[UIImage imageNamed:@"minImg.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 14, 0, 14)];
    [progressSlider setMinimumTrackImage:minimumValueImage forState:UIControlStateNormal];
    //    UIImage *barImageRight=[[UIImage imageNamed:@"slider_bar_right"]stretchableImageWithLeftCapWidth:50 topCapHeight:10];
    UIImage *maxmumValueImage = [[UIImage imageNamed:@"maxImg.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 14, 0, 14)];
    [progressSlider setMaximumTrackImage:maxmumValueImage forState:UIControlStateNormal];


你可能感兴趣的:(ios)