Changing Default Behaviour of UISlider - No continuous sliding and Custom button - iphone

I want to achieve above type of UI using slider... But Slider should stop at only 3 positions
1. Extreme Right
2. Extreme Left
3. Center
Will it be possible to achieve this type of UI using UISlider in iPhone?? How should I achieve it??
EDIT:
I want to achieve same as TrackBar in VB.NET. Is it possible with the UISlider??? or please suggest me some other control/code in iPhone apart from SegmentedControl...
EDIT 2
int progressAsInt =(int)(slider.value + 0.5f);
NSString *newText =[[NSString alloc] initWithFormat:#"%d",progressAsInt];
sliderLabel.text = newText;

Look at the documentation for UISlider. The methods you are interested in are setMinimumTrackImage: forState: and setMaximumTrackImage: forState: to set the track, setThumbImage: forState: for the slider thumb
To make it stick at the beginning, middle or end, your viewcontroller should, in the valueChanged method linked to from the slider, determine the appropriate value and then use setValue: animated: to move the slider to the appropriate place. So, if your slider goes from 0 to 2, and the user changes it to 0.75, you assume this should be 1 and set the slider value to that.
Also - turn off the continuous property or the above won't work.

Related

Objective C - How to change the UIslider value with button Click actions

As show in the figure above I have 1 button to decrease and 1 button to increase the value of the slider.
The range of the slider is 1 to 10.
My requirement is to change slider's value 0.5 by each button click.
For example if the slider's current value is 4, clicking the + should change the value to 4.5 and change the display accordingly.
Same thing with negative button to change value to 3.5.
How can I accomplish this?
On tapPlusButton change the value of slider by adding 0.5 to current value like this.
[self.slider setValue:self.slider.value+0.5];
Create an outlet for the slider if it's created using Interface builder.
Then just do "mySliderOutletName.value += customValue";
Add the above code to the Action triggered by touch up inside for the UIButton.
If it's created from code, add it to the class interface, then you can refer to it in all the methods of the class.
There's a good video tutorial about UISlider here.
The steps are these:
Create a class variable with your UISlider, and set the min and the max values of it
Add a target to the buttons. When the button is pressed, do:
[self.mySlider setValue:self.mySlider.value + 0.5];
for iOS 11 Objective C
- (IBAction)Plus:(id)sender {
[self.mySlider setValue:self.mySlider.value+1];
NSInteger index = mySlider.value;
[self.imageView setImage:[UIImage imageNamed:[NSString stringWithFormat:#"%#.png", [imagenArray objectAtIndex:index]]]];
[_counter setText:[NSString stringWithFormat:#"%d/%lu", index+1, (unsigned long)imagenArray.count]];
}
- (IBAction)Menus:(id)sender {
[self.mySlider setValue:self.mySlider.value-1];
NSInteger index = mySlider.value;
[self.imageView setImage:[UIImage imageNamed:[NSString stringWithFormat:#"%#.png", [imagenArray objectAtIndex:index]]]];
[_counter setText:[NSString stringWithFormat:#"%d/%lu", index-1, (unsigned long)imagenArray.count]];
}

UISlider highlited property for showing the value.

i have IBOutlet uislider. i want it to show the value of slider when the user presses the slider and change the value of it when the user take his hand off the slider i want it to disapper. So when the user touches to change the value of slider the label shows the value and when the user take his finger of the slider the label automaticly disapper.
My code is:
-(IBAction)sliderSlide:(UISlider *)aSlider {
float f=slider.value;
NSString *show=[NSString stringWithFormat:#"%.2f %%",f];
label2.text=show;
}
i know i need to use slider.highlited=YES; but where and how can i turn it back to hidden?
- (IBAction)touchEndedAction
{
self.label2.hidden = YES;
}
set the IBAction to the sliders UIControlEventEditingDidEnd or UIControlEventTouchCancel
try it out.
UIControlEventEditingDidEnd didn't work for me, but UIControlEventTouchDown works

setting a default value for slider when app loads

In my iPhone app I'm using a slider for adjusting the volume and working fine.
But I'm not able to set a default value for slider when app loads.
I have seen that only when the slider is touched the value gets changed.But i need a default value when app loads.
My code is given below
- (IBAction)sliderChanged:(id)sender {
slider = (UISlider *) sender;
slider.minimumValue = 0.5;
slider.maximumValue = 2.2;
progressAsInt = slider.value ;
}
how can i solve this issue.
please help.Thanks in advance.
There are two ways to change default value of slider.
Using Interface builder. See attached picture and value of current property.
Using Code
You just need to write this one line in viewWillAppear delegate method.
-(void)viewWillAppear:(BOOL)animated{
slider.value = 1.0;
}
Hope this help.
First you'll need to set the slider to an IBOutlet iVar of the view controller where the slider is.
Then, in the viewDidLoad of that view controller, you can set the value you want.
(Also, you should be able to do it in Interface Builder, but I'm not very familiar with it).

How to hide a UIButton title after onclick event

When I click a button Its title should be hide. I do't want to set the title to empty string #"". So, How can i do this?
Just a better solution that I'm using now:
The accepted answer doesn't let me re-use the name if I need, so I'm using in this way:
-(void)SomeButtonPressed {
someButton.titleLabel.textColor = [UIColor clearColor];
}
I think it's better just keeping the button's label invisible.
Why don't you want to set the title to an empty string? Simply store the value in a local field and set the button's title to #"" and everything will be fine.
In your .h:
NSString *someLocalField;
in your .m:
-(void)SomeButtonPressed {
someLocalField = someButton.text;
someButton.text = #"";
}
This way, if you ever need to restore the text of the button, you can do so:
someButton.text = someLocalField
If you want to do this for a bunch of buttons, you could always use an NSDictionary and associate the string values with the buttons.
If you want it to disappear when a finger is on the button,
[button setTitle:#" " forState:UIControlStateHighlighted];
If you want it to toggle between being displayed and not,
[button setTitle:#" " forState:UIControlStateSelected];
[button setTitle:#" " forState:UIControlStateSelected|UIControlStateHighlighted];
and then set button.selected = !button.selected in the button action.
I'm using a single space instead of the empty string because sometimes the empty string has special handling which makes it equivalent to nil. If the empty string works, you can use that instead.
I haven't tried this, but if you need the title text to stay the same, but still hide from a user you might be able to set the font color to [UIColor clearColor];
using button.titleLabel.hidden = YES will not work (at least on on iOS 7).
I ended up using:
// remove the button since hiding it doesn't work
[button.titleLabel removeFromSuperview];
// put back when you're done
[button addSubview:button.titleLabel];
// Hide text
button.titleLabel.layer.opacity = 0.0f;
// Show text
button.titleLabel.layer.opacity = 1.0f;
Just hiding the title sounds a bit odd and not very Apple interface like. You can also just set the button to be hidden and then the entire thing goes away. If you do want the title to be the only thing that goes away (keep in mind that the button will still work in this state, just not have a title) then you could always assign the background color someButton.currentTitleColor = someButton.backgroundColor; should make the text vanish (you may need to set the shadow color as well).
I guess here you can find a solution, simply make the label that contains the text hidden.
button.titleLabel.hidden = YES;
You can put this in an IBAction linked in InterfaceBuilder to the Touch Inside Up event associate to your button
Have you tried button.titleLabel.hidden = YES ?

How to check image during animation

I have set up an animation in the following way (self is an UIImageView, myImages an Array of UIImages):
self.animationImages = myImages;
self.animationDuration = 50;
self.animationRepeatCount = 0;
[self startAnimating];
During the animation I'd like to check the current image. I tried it the following way
if([self image]==[UIImage imageNamed:#"image1.png"]);
but this does not work. Is there a straight forward way for this? Can I keep track of which image is shown during the animation?
There is no official way to get the currently displayed image (index) from an animated UIImageView. Note: If the animationImages property contains a value other than nil, the contents of the image property is not used at all and will not contain usable data.
Still, you can measure the time that has passed since the animation started and evaluate the current frame/image index yourself.