How to show images to the extreme end of the UISlider? - iphone

I'm implementing UISlider. I want to show images to the extreme end of the slider(eg. volume control images of mute and full sound). I'm using
UIImage *maximumValueImage = [UIImage imageNamed:#"speaker_plus.png"];
UIImage *minimumValueImage = [UIImage imageNamed:#"speaker_minus.png"];
slider.maximumValueImage = maximumValueImage;
slider.minimumValueImage = minimumValueImage;
How to implement the following method?
-(CGRect)maximumValueImageRectForBounds:(CGRect)bounds;
-(CGRect)minimumValueImageRectForBounds:(CGRect)bounds;

I did same for one of my app. Below is code.
UIImage *stetchLeftTrack = [[UIImage imageNamed:#"leftslide.png"]
stretchableImageWithLeftCapWidth:10.0 topCapHeight:0.0];
UIImage *stetchRightTrack = [[UIImage imageNamed:#"rightslide.png"]
stretchableImageWithLeftCapWidth:10.0 topCapHeight:0.0];
[slider setThumbImage:[UIImage imageNamed:#"imgSlider.png"] forState:UIControlStateNormal];
[slider setMinimumTrackImage:stetchLeftTrack forState:UIControlStateNormal];
[slider setMaximumTrackImage:stetchRightTrack forState:UIControlStateNormal];
Hope this Help.
These are the images I used.
Black image is leftside.png
White image is rightside.png
Black Circle is imgSlider.png

Related

Align UISlider thumb image

I've customized my UISlider but my thumb image looks strange, I mean it's position not aligned by center:
But should be like this:
And here is code:
UIImage *leftTrack = [[UIImage imageNamed:#"blueTrack.png"] stretchableImageWithLeftCapWidth:3 topCapHeight:0];
UIImage *rightTrack = [[UIImage imageNamed:#"whiteTrack.png"] stretchableImageWithLeftCapWidth:3 topCapHeight:0];
[slider setThumbImage:[UIImage imageNamed:#"thumbButton.png"] forState:UIControlStateNormal];
[slider setMinimumTrackImage:leftTrack forState:UIControlStateNormal];
[slider setMaximumTrackImage:rightTrack forState:UIControlStateNormal];
There are two ways:
First one is to use
- (CGRect)trackRectForBounds:(CGRect)bounds;
and / or
- (CGRect)thumbRectForBounds:(CGRect)bounds trackRect:(CGRect)rect value:(float)value;
methods of UISlider when subclassing to lay out thumb the way you want
Second way is to adjust your artwork:

how to customised slider?

I have Created my customized slider like this
UIImage *image = [UIImage imageNamed:#"thumb.png"];
CGRect frame = CGRectMake(22.5, 220.0, 280.0, 20.0);
UISlider *myslider = [[UISlider alloc] initWithFrame:frame];
[myslider addTarget:self action:#selector(sliderChanged:) forControlEvents:UIControlEventValueChanged];
[myslider setBackgroundColor:[UIColor clearColor]];
[myslider setThumbImage:image forState:UIControlStateNormal];
UIImage *stetchLeftTrack = [[UIImage imageNamed:#"leftLineImage.png"]
stretchableImageWithLeftCapWidth:10.0 topCapHeight:0.0];
UIImage *stetchRightTrack = [[UIImage imageNamed:#"SlideRight.png"]
stretchableImageWithLeftCapWidth:10.0 topCapHeight:0.0];
[myslider setMinimumTrackImage:stetchLeftTrack forState:UIControlStateNormal];
[myslider setMaximumTrackImage:stetchRightTrack forState:UIControlStateNormal];
myslider.minimumValue = 0.0;
myslider.maximumValue = 100.0;
myslider.continuous = YES;
myslider.value = 5.0;
[self.view addSubview:myslider];
In this slider i have taken slider left track image and slider right track image the size of both images are same i have done this by adjust size of preview but still I am getting my left track image is bigger how can I solve this problem.
and my second question
i have call the event on slider value change but i need to call a event when the slider get slide-fully or slider value get 100 how can I do this also.
Hi for your first question refer UICatalog app example provided in developer.
http://developer.apple.com/library/ios/#samplecode/UICatalog/Introduction/Intro.html
For second answer you can try this:-
- (void)sliderChanged:(id)sender
{
if (sender.value==100) {
//do your code
}
else {
//do nothing
}
}

How to place image on UISlider?

I have to place a slider and want to place an imge on slider. the slider i want is this
CAn any body help me?
See the UISlider class reference. You want to set setThumbImage:forState: as well as setMinimumTrackImage:forState: and setMaximumTrackImage:forState:. Use the same images for the minimum and maximum track images, and use UIControlStateNormal for the state.
Below code will be in ViewWillAppear...
-(void)viewWillAppear:(BOOL)animated
{
slider.backgroundColor = [UIColor clearColor];
UIImage *stetchLeftTrack = [[UIImage imageNamed:#"yellowslide.png"]
stretchableImageWithLeftCapWidth:10.0 topCapHeight:0.0];
UIImage *stetchRightTrack = [[UIImage imageNamed:#"yellowslide.png"]
stretchableImageWithLeftCapWidth:10.0 topCapHeight:0.0];
[slider setThumbImage: [UIImage imageNamed:#"1.PNG"] forState:UIControlStateNormal];
[slider setMinimumTrackImage:stetchLeftTrack forState:UIControlStateNormal];
[slider setMaximumTrackImage:stetchRightTrack forState:UIControlStateNormal];
slider.minimumValue = 1.0;
slider.maximumValue = 3.0;
slider.continuous = YES;
slider.value = 0.0;
}

how to create a slider?

How can I create a slider in customized slider in iPhone. Currently I am using the slider selected from "xib"(Interface Builder) but it is showing me normal slider.But I need slider with customized with my own button and showing text
Slide To Start
How can I do this please help me out .
See following links
http://www.applausible.com/blog/?p=250
http://iphone4developer.blogspot.com/2010/12/custom-uislider.html
Change iPhone UISlider bar image
http://jainmarket.blogspot.com/2010/06/customize-default-uislider-in-iphone.html
http://www.xprogress.com/post-35-uislider-tutorial-example-how-to-use-slider-in-iphone-sdk-xcode/
http://www.obsessivecode.com/projects/ocprogress/
you can accomplish like
UISlider *mySlider;
mySlider = [[UISlider alloc]init];
/* Slider Customization */
[mySlider setThumbImage:[UIImage imageNamed:#"yourimage.png"] forState:UIControlStateNormal];
/*the above line will sets a custom image for your slider ball i.e; thumb image*/
/* the below code will make your slider line custom */
UIImage *sliderLeftTrackImage = [[UIImage imageNamed: #"lineImage.png"] stretchableImageWithLeftCapWidth: 9 topCapHeight: 0];
UIImage *sliderRightTrackImage = [[UIImage imageNamed: #"lineImage.png"] stretchableImageWithLeftCapWidth: 9 topCapHeight: 0];
[mySlider setMinimumTrackImage: sliderLeftTrackImage forState: UIControlStateNormal];
[mySlider setMaximumTrackImage: sliderRightTrackImage forState: UIControlStateNormal];
/* end of Slider Customization */
[mySlider release];
You will need to use stretchable images for the customization, and load them with the following methods:
setMinimumTrackImage:forState:
setMaximumTrackImage:forState:
setThumbImage:forState:
The minimum track image is the part on the left of the thumb (by default colored blue), and the maximum track image is on the right side.
Here is an example for the customization:
UIImage *minimumTrackImage = [UIImage imageNamed:#"sliderMin.png"];
UIImage *stretchableMinimumTrackImage = [minimumTrackImage stretchableImageWithLeftCapWidth:6 topCapHeight:6];
[slider setMinimumTrackImage:stretchableMinimumTrackImage forState:UIControlStateNormal];
UIImage *maximumTrackImage = [UIImage imageNamed:#"sliderMax.png"];
UIImage *stretchableMaximumTrackImage = [maximumTrackImage stretchableImageWithLeftCapWidth:6 topCapHeight:6];
[slider setMaximumTrackImage:stretchableMaximumTrackImage forState:UIControlStateNormal];
UIImage *handle = [UIImage imageNamed:#"sliderHandle.png"];
[slider setThumbImage:handle forState:UIControlStateNormal];
UISlider *objslider = [[UISlider alloc] initWithFrame:CGRectMake(14, 249, 290, 22)];
UIImage *stetchLeftTrack = [[UIImage imageNamed:#"slider_fill.png"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:0.0];
UIImage *stetchRightTrack = [[UIImage imageNamed:#"slider.png"]
stretchableImageWithLeftCapWidth:0.0 topCapHeight:0.0];
[objslider setThumbImage: [UIImage imageNamed:#"slider_ball.png"]forState:UIControlStateNormal];
[objslider setMinimumTrackImage:stetchLeftTrack forState:UIControlStateNormal];
[objslider setMaximumTrackImage:stetchRightTrack forState:UIControlStateNormal];
objslider.minimumValue = 0.0;
objslider.maximumValue = 100.0;
objslider.continuous = YES;
objslider.value = 0.0;
[self.view addSubview:objslider];

UISliderbar moves when moving the track

I have a custom slider bar and when I slide it back and forth the track image seems to move left and right a little. Anyone know why this happens?
UIImage *stetchLeftTrack = [[UIImage imageNamed:highBar] stretchableImageWithLeftCapWidth:capLeft topCapHeight:0.0];
UIImage *stetchRightTrack = [[UIImage imageNamed:grooveBar] stretchableImageWithLeftCapWidth:capRight topCapHeight:0.0];
[customSlider setThumbImage: [UIImage imageNamed:thb] forState:UIControlStateNormal];
[customSlider setThumbImage: [UIImage imageNamed:thbH] forState:UIControlStateHighlighted];
[customSlider setMinimumTrackImage:stetchLeftTrack forState:UIControlStateNormal];
[customSlider setMaximumTrackImage:stetchRightTrack forState:UIControlStateNormal];
Is it possible that your capLeft and capRight values are not what you expect (they should probably be nonzero integers)? It could be stretching the entire image rather than just the part you intend to have stretched.