how to customised slider? - iphone

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
}
}

Related

how to change the slider thumb image?

I have to create my own customized slider. I am trying to change the thumb image,background of the slider and in the some test like slide to unlock.
I have tried like this but its not working
CGRect frame = CGRectMake(0.0, 219.0, 323.0, 20.0);
UISlider *myslider = [[UISlider alloc] initWithFrame:frame];
[myslider addTarget:self action:#selector(sliderAction:) forControlEvents:UIControlEventValueChanged];
[myslider setBackgroundColor:[UIColor clearColor]];
[myslider setThumbImage: [UIImage imageNamed:#"sliderThumb#2x.png"] forState:UIControlStateNormal];
myslider.minimumValue = 0.0;
myslider.maximumValue = 50.0;
myslider.continuous = YES;
myslider.value = 15.0;
[self.view addSubview:myslider];
But in this the size of thumb pic is very big.How to reduce the thumb pic size....and how to give the slider background and text.Please help me out
[[UISlider appearance] setThumbImage:[UIImage imageNamed:#"ball.png"] forState:UIControlStateNormal];
[slider setMinimumTrackImage:[[UIImage imageNamed:#"volume_slider_oragne.png"] stretchableImageWithLeftCapWidth:0.3 topCapHeight:0.0] forState:UIControlStateNormal];
[slider setMaximumTrackImage:[[UIImage imageNamed:#"volume_strap_gry.png"] stretchableImageWithLeftCapWidth:0.3 topCapHeight:0.0] forState:UIControlStateNormal];
To set the background colour of the slider, I would presume that you can set the property backgroundColor to [UIColor colorWithPatternImage:[UIImage imageNamed:#"slider background"]];
Also, images that have the appending ending of #2x, mean that they are twice the size of the standard copy. This is for retina displays only, you should not use it in a UIImage on a standard (legacy) device. Try setting the image name to #"sliderThumb.png" instead, as long as you have the legacy copy of the image.
Just provide a different size of sliderThumb#2x.png and all we work itself out.

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];

Slider Bar should appear as it moves towards its right

Im my app, i need a slider with no bar initially. And as the user changes it, it should be visible.
As the user slides the thumb of the slider to the right the slider bar should start appearing on the left side.
Please share anything you have in your mind.
Thanks,
I've never tried this but I suppose it's possible. It sounds like a bad idea from a user-experience perspective, but that's outside the scope of this answer.
You can change the min and max images for a slider using the code below. You could add in some logic to hide these images initially (or set these images to transparent PNGs). Add a method called - (void)sliderAction:(id)sender which will be called when the slider is changed. Add logic there to change the images to something visible, or un-hide the images.
UIImage *minImage = [UIImage imageNamed:#"min.png"];
UIImage *maxImage = [UIImage imageNamed:#"max.png"];
UIImage *thumbImage= [UIImage imageNamed:#"thumb.png"];
minImage=[minImage stretchableImageWithLeftCapWidth:10.0 topCapHeight:0.0];
maxImage=[maxImage stretchableImageWithLeftCapWidth:10.0 topCapHeight:0.0];
UISlider* customSlider = [[UISlider alloc] initWithFrame:CGRectMake(30, 100, 200, 50)];
[customSlider setMinimumTrackImage:minImage forState:UIControlStateNormal];
[customSlider setMaximumTrackImage:maxImage forState:UIControlStateNormal];
[customSlider setThumbImage:thumbImage forState:UIControlStateNormal];
customSlider.minimumValue = 0.0;
customSlider.maximumValue = 100.0;
customSlider.continuous = YES;
customSlider.value = 50.0;
[customSlider addTarget:self action:#selector(sliderAction:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:customSlider];
[customSlider release];

Making a Transparent image button with bgcolor

I have a transparent image which ("hold_empty.png", below) which I want to turn into a button. Inside the button there will be a view with a colored background which is slightly smaller size than the actual image to hold the background color.
The reason for this is because the image has rounded corners and so I cannot simply put a background color on the image as it will appear to be bigger than the image.
The image is a transparent square with "rounded corners". The effect I am trying to create should be like layers.
Background color layer (red, green, etc)
The "hold_empty.png" picture (above)
The whole object (including bg color layer) should be clickable.
The problem I am experiencing is that only the image (and its borders) appears to be clickable, and not the whole object.
The code follows below.
// x,y,w,h
CGRect frame = CGRectMake(10, 10, 72, 72);
CGRect frame2 = CGRectMake(5, 5, 60, 60); // I know this is not filling the image, its just a test
UIView *bgColorView = [[UIView alloc] initWithFrame:frame2];
[bgColorView setFrame:frame2];
bgColorView.backgroundColor = [UIColor redColor];
UIImage *image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"hold_empty" ofType:#"png"]];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn addSubview:bgColorView];
[btn setImage:image forState:UIControlStateNormal];
btn.frame = frame;
[btn addTarget:self action:#selector(btnSelectColor:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
[bgColorView release];
So, to summarize: How do I make a transparent image button with a background color clickable?
Thanks.
The clickable area is generally the frame of the button which would not be clipped by its super-views, if you would let them clip contents.
The fact that you see something on screen doesn't mean it's "visible" in the sense that you can touch it. If you let every view clip its subviews (through IB, or by setting view.clipsToBounds = YES;) then any problems in that respect will show up clearly.
(Note that any UIButton, UIImageView, etc is a view and can be set to clip its contents)
Do you need to maybe enable user interaction on the subview?
bgColorView.userInteractionEnabled = YES;
I think I may have an answer that is working, but would like your comments/suggestions.
I created an UIImageView and put my background color layer and image layer inside that and then put the UIImageView inside the btn.
The only problem is now the button does not look like it is being pressed.
When I add the "setShowsTouchWhenHighlighted" option it has what looks like a big white star in the middle of the button when the user clicks ont he button.
// x,y,w,h
CGRect frame = CGRectMake(10, 10, 72, 72);
CGRect frame2 = CGRectMake(5, 5, 62, 62); // This fits, but may not be 100% accurate
// View
UIView *bgColorView = [[UIView alloc] initWithFrame:frame2];
[bgColorView setFrame:frame2];
bgColorView.backgroundColor = [UIColor redColor];
bgColorView.userInteractionEnabled = YES;
[bgColorView setNeedsDisplayInRect:frame2];
// Image
UIImage *image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"hold_empty2" ofType:#"png"]];
UIImageView *imgView = [[UIImageView alloc] initWithImage:image];
[imgView insertSubview:bgColorView atIndex:0];
// Btn
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn setFrame:frame];
[btn addSubview:imgView];
[btn setShowsTouchWhenHighlighted:YES];
[btn setUserInteractionEnabled:YES];
[btn addTarget:self action:#selector(btnSelectColor:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
[imgView release];
[bgColorView release];
I've cracked it now. One of the problems I was having was that it was not working with avatars (sometimes it would appear behind the layer, othertimes it would not show at all).
This is my solution.
UIImage *image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"some_avatar" ofType:#"png"]];
UIImageView *imgView = [[UIImageView alloc] initWithImage:image];
CGRect fullFrame = CGRectMake(25, 10, 70,72);
CGRect frame = CGRectMake(28, 13, 63,65);
UIButton *h=[UIButton buttonWithType:UIButtonTypeCustom];
[h setFrame:fullFrame];
[[h layer] setMasksToBounds:YES];
//[h setBackgroundImage:image forState:UIControlStateNormal];
//[h setImage:image forState:UIControlStateNormal];
[h setShowsTouchWhenHighlighted:YES];
[h setClipsToBounds:YES];
CAGradientLayer *gradientLayer = [[CAGradientLayer alloc] init];
[gradientLayer setBounds:frame];
[gradientLayer setPosition:CGPointMake([h bounds].size.width/2, [h bounds].size.height/2)];
[gradientLayer setColors:[NSArray arrayWithObjects:
(id)[[UIColor darkGrayColor]CGColor],(id)[[UIColor blackColor] CGColor], nil]];
[[h layer] insertSublayer:gradientLayer atIndex:0];
[h insertSubview:imgView atIndex:1];
[h addTarget:self action:#selector(btnSelectColor:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:h];
[imgView release];