I have such a problem... I want to block user's slider when he is sliding. I thought .isUserInteractionEnabled will handle that but it works only after the user releases slider.
I would like to 'steal' the slider from user and stop it.
How to do that?
You should add a selector to your UISlider object. For instance, if you had a UISlider named "slider" you should do this:
[slider addTarget:self action:#selector(sliderMoved:) forControlEvents:UIControlEventValueChanged];
//runs a method called "sliderMoved" located in the same class as the slider (hence the addTarget:self) when the value is changed of the slider
and then you could make a method like this, which sets the value at the average whenever the user tries to slide the slider:
-(void)sliderMoved:(UISlider*)sender{
sender.value = (sender.maximumValue + sender.minimumValue) / 2;
}
Related
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]];
}
I'm currently trying to use a UIButton titled "X" as a way to remove a Sprite from the view.
Basically, my code works so that when a Sprite is touched, a message is sent to the delegate (View Controller) that passes the (Sprite *)sprite which has been selected. In this method, I draw a UIButton on top of that sprite. So far, so good.
However, the issue is I now want my UIButton to run a #selector to remove that sprite whenever the button is touched down.
Initially I tried this:
-(void)spriteSelected:(Sprite *)sprite{ //delegate method implementation
[sprite.button addTarget:self action:#selector(removeSprite:sprite) forControlEvents: UIControlEventTouchDown]
}
-(void)removeSprite:(Sprite *)sprite{
[sprite removeFromSuperView];}
However, it seems I can't put arguments into the selector like that. Any ideas on how I can adjust this?
Thanks
You should do this in the Sprite class, so in the original class:
-(void)spriteSelected:(Sprite *)sprite{
[sprite youAreSelected];
}
In the Sprite class:
-(void)youAreSelected {
[self.button addTarget:self action:#selector(removeMe:) forControlEvents: UIControlEventTouchDown];
}
-(void)removeMe:(id)sender {
[self removeFromSuperView];
}
A selector is simply a method identifier, not an invocation of the method, so you can't include parameters as though it were a method call.
The usual way to manage something like this would be for the view controller to look at the button that was touched, figure out which sprite it's associated with, and remove the sprite. It was probably the view controller that put both the sprite and the button in the view in the first place, so it should have all the information it needs.
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
Sorry for the noob question. I have a view that has a UISegmentControl in the UIToolBar. I also present a popover from a button. How do I get the value of the UISegmentControl to the popover? Should I have the Popover have an NSInteger ivar to hold this value so when I present the popover, I set that value to whatever the selectedSegmentIndex is? I didn't know if that was the cleanest way since I'm new at this and keep reading stuff about not coupling your classes. Thanks!
In my opinion , You can use UIControlEventValueChanged Event of UISegmentControl to directly set the TAG Propertly of popover to the selectedSegmentIndex of UISegmentControl .
{
[segmentedControl addTarget:self
action:#selector(setvalueofselected:)
forControlEvents:UIControlEventValueChanged];
}
-(IBAction)setvalueofselected:(id)sender
{
popover.tag = segmentedControl.selectedSegmentIndex;
OR
self.intSelected = segmentedControl.selectedSegmentIndex;
}
And from with in your popover click event code try to check the property of TAG . You can also use Some Global Integar for this purpose instead of tag property.
In this way your POPOver can know which segment was selected when they clicked popover.
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).