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]];
}
Related
I want to create a small horizontal scrollview with few buttons as shown in image. User can select any one of them and details will be shown accordingly. I am not sure how to code it programmatically. Please help.
Image:- http://i.stack.imgur.com/uDqkn.jpg
ok follow steps to implement the same.
step 1 : take UITableview and apply affin transform for 90 degree.
step 2 : take a button on each cell also apply affinetransform of 90 degree.
step 3 : for each table cell height (now you can say width) will be calculated with the help of stringwidth function of NSString. so please type code in call back method (heightforRowatIndexPath).
step 4 : take layer of tableview and apply round corner property with value 10.0f
step 5 : same you can do for the button their color.
step 6: for outer arrow yo need to implement some UIScrollview module.
here all memory will be managed by tableview for n number of buttons in horizontal scrolling.
Thanks,
Please let me know for any issue
In an ARC view controller ...
#define kHEIGHT 35.0
- (void)createButtonScrollViewWithButtonTitles:(NSArray *)titles {
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0.0,100.0,320.0,kHEIGHT)];
CGFloat buttonPositionX = 0.0;
for (NSString *title in titles) {
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self action:#selector(pressedButton:) forControlEvents:UIControlEventTouchDown];
[button setTitle:title forState:UIControlStateNormal];
button.frame = CGRectMake(buttonPositionX, 0.0, 90.0, kHEIGHT);
buttonPositionX += 90.0;
[scrollView addSubview:button];
}
scrollView.contentSize = CGSizeMake(buttonPositionX, kHEIGHT);
[self.view addSubview:scrollView];
}
After you get this working, you can create a version that takes an array of images. For that, the button type will change to UIButtonTypeCustom. You'll use setImage: rather than setTitle: and you'll have the chance to vary the width by advancing buttonPositionX by the image.size.width, rather than a constant.
Not sure how to interpret the little scroll-arrow-looking things on each side of the image you posted. Another subsequent enhancement would be to overlay each end with a little image view (with userInteractionEnabled = NO) that matches those arrow things.
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;
}
I want to create a custom calendar application using an array of buttons,so I want to know how to start with this..please help me out.w.r.t this,I want to know if any libraries are present which uses buttons and not a grid layout,because I dont want to use a grid layout
Hello friends,I tried to solve this issue with comments below and wrote this code
for (int columnIndex = 0; columnIndex < 6; columnIndex++)
{
// now loop over the rows
for (int rowIndex = 0; rowIndex < 7; rowIndex++)
{
NSString *buttonTitle = [NSString stringWithFormat:#"%d",columnIndex,rowIndex];
CGRect newFrame = CGRectMake(10 + rowIndex * 40, 60 + columnIndex * 40, 35, 35);
UIButton *newButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
newButton.frame = newFrame;
newButton.backgroundColor = [UIColor grayColor];
[newButton setTitle:buttonTitle forState:UIControlStateNormal];
[newButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
// tag the button, so we know how to handle each one
//newButton.tag = (columnIndex << 8) + rowIndex;
//newButton =[m_buttonArray objectAtIndex:rowIndex];
NSLog(#"%d",[m_buttonArray count]);
[newButton addTarget:self action:#selector(buttonPressed) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:newButton];
}
and when I run the project i see 6*7 grid of buttons..
but I am confused how shall I add these buttons to an array so that I can access w.r.t array,I think it requires a 2D array..so firends please help me out how shall I do this
Thanks&Regards
Ranjit
There's no grid layout in iOS, afaik.
One of your options is to lay out your interface in the interface builder:
Create a new file using UIViewController subclass. Tap the .xib file in the XCode 4.x
Tap the right panel button on the top left, then select "library". Drag a UIButton over to your view and position it. Then you can copy/paste buttons.
I recommend you to find an iOS interface builder tutorial and proceed with that if you want to take this approach.
This is the simplest, yet tedious path. You would be able to visually see how your calendar looks. The downside is that this approach would require you to define 42 buttons and 42 outlets. On the plus side, you would be able to alter your .XIB file
Another way would be to create the interface programmatically an NSMutableArray and add 42 buttons there. You would add each button to your subview using a custom frame for each. This is a very error prone solution, and you will have to manually edit frame offsets/widths if you need to make changes.
I hope this helps!
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.
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).