Changing the Range of a UISlider Programmatically - iphone

How is it possible to set the min max ranges of the UISlider programmatically?
For example (dummy code)
UISlider* slider = [[UISlider alloc] init];
slider.min = -3;
slider.max = 3;
EDIT:
So I have the following:
sl.minimumValue = 5;
NSLog(#"MIN VAL: %d", sl.minimumValue);
This doesn't work, I still get it logging the value 0. Is this because I have set values in interface builder?

Here is it.
UISlider* slider = [[UISlider alloc] init];
slider.minimumValue = -3.0f;
slider.maximumValue = 3.0f;

In the case updating selected values of IBOutlet-ed RangeSeekSlider
rangeSeekSlider.selectedMinValue = 10.0
rangeSeekSlider.selectedMaxValue = 100.0
rangeSeekSlider.setNeedsLayout()

// Add a frame where you want to place the slider. This will place it at (x,y) = 0,0
with a height of 10 and width of 200
CGRect frame = CGRectMake(0.0, 0.0, 200.0, 10.0);
// sliderAction will respond to the updated slider value
UISlider *slider = [[UISlider alloc] initWithFrame:frame];
[slider addTarget:self action:#selector(sliderAction:) forControlEvents:UIControlEventValueChanged];
// Set minimum and maximum value
slider.minimumValue = 0.0;
slider.maximumValue = 50.0;
slider.continuous = YES;
// Initial value
slider.value = 25.0;
// Add slider to view
[self.view addSubview:slider];

Swift 5 :
self.yourSlider.minimumValue = 0.0
self.yourSlider.maximumValue = 4
self.yourSlider.value = 0

Related

Hide programmatically created UISlider

I've programmatically created a UISlider in viewDidLoad using the following code and when a button is pressed I want to hide the object and maybe use it again. I can't seem to get it to work. I've tried a number of approaches which all build correctly but none of which have the desired effect.
CGRect frame1 = CGRectMake(-5.0, 290.0, 100.0, 10.0);
UISlider *sliderSaveurFloral = [[UISlider alloc] initWithFrame:frame1];
[sliderSaveurFloral addTarget:self action:#selector(sliderAction:) forControlEvents:UIControlEventValueChanged];
[sliderSaveurFloral setBackgroundColor:[UIColor clearColor]];
sliderSaveurFloral.minimumValue = 0.0;
sliderSaveurFloral.maximumValue = 50.0;
sliderSaveurFloral.continuous = YES;
sliderSaveurFloral.value = 0.0;
[self.view addSubview:sliderSaveurFloral];
CGAffineTransform trans2 = CGAffineTransformMakeRotation(M_PI * -0.5);
sliderSaveurFloral.transform = trans2;
[sliderSaveurFloral setValue:0];
UISlider inherits from UIView. I believe you can set the hidden property to make it invisible.
sliderSaveurFloral.hidden = YES; //Set it back to NO when you want it appear again
You may also have to make it not interactive when hidden,
sliderSaveurFloral.userInteractionEnabled = NO; //Set it back to YES later when you need

To make 16 uilabels and align them in circular path.

NSMutableArray *views = [[NSMutableArray alloc]initWithCapacity:0];
for (NSInteger i = 0; i<16; i++)
{
UIView *circle = [[UIView alloc]init];
circle.backgroundColor = [UIColor clearColor];
UIImageView *circleImage = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 80, 80)];
circleImage.image = [UIImage imageNamed:#"circle"];
[circle addSubview:circleImage];
UILabel *labelInsideCircle = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, 40, 40)];
labelInsideCircle.backgroundColor = [UIColor clearColor];
labelInsideCircle.textColor = [UIColor greenColor];
labelInsideCircle.font = [UIFont fontWithName:#"Helvetica" size:30.0];
labelInsideCircle.center = circleImage.center;
NSInteger int_ = [self getRandomNumber:0 to:(arrOfOptions.count-1)];
labelInsideCircle.text = [NSString stringWithFormat:#"%#",[arrOfOptions objectAtIndex:int_]];
labelInsideCircle.textAlignment = NSTextAlignmentCenter;
[arrOfOptions removeObjectAtIndex:int_];
[circle addSubview:labelInsideCircle];
[labelInsideCircle release];
[views addObject:circle];
[circle release];
[circleImage release];
}
/* Rotating circles with angles */
float curAngle = 0;
float incAngle = ( 360.0/(views.count) )*3.14/180.0;
CGPoint circleCenter = CGPointMake(380, 580); /* given center */
float circleRadius = 250; /* given radius */
for (UIView *view in views)
{
CGPoint viewCenter;
viewCenter.x = circleCenter.x + cos(curAngle)*circleRadius;
viewCenter.y = circleCenter.y + sin(curAngle)*circleRadius;
view.transform = CGAffineTransformRotate(view.transform, curAngle);
view.center = viewCenter;
[self.view addSubview:view];
curAngle += incAngle;
}
The problem is here the text of UILabel is also getting transformed, which is obvious. What I want is 16 circular views with labels on them without the label's text transformed. Can anyone please help me out with this ?
In this case, you just need to change their location coordinates, not rotate them.
NSMutableArray *views = [[NSMutableArray alloc] initWithCapacity:0];
for (NSInteger i = 0; i<16; i++)
{
UIView *circle = [[UIView alloc]init];
circle.backgroundColor = [UIColor clearColor];
UIImageView *circleImage = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 80, 80)];
circleImage.image = [UIImage imageNamed:#"circle"];
[circle addSubview:circleImage];
UILabel *labelInsideCircle = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, 40, 40)];
labelInsideCircle.backgroundColor = [UIColor clearColor];
labelInsideCircle.textColor = [UIColor greenColor];
labelInsideCircle.font = [UIFont fontWithName:#"Helvetica" size:30.0];
labelInsideCircle.center = circleImage.center;
NSInteger int_ = arc4random()%[arrOfOptions count];
labelInsideCircle.text = [NSString stringWithFormat:#"%#",[arrOfOptions objectAtIndex:int_]];
labelInsideCircle.textAlignment = NSTextAlignmentCenter;
[arrOfOptions removeObjectAtIndex:int_];
[circle addSubview:labelInsideCircle];
[labelInsideCircle release];
[views addObject:circle];
[self.view addSubview:circle];
[circle release];
[circleImage release];
}
/* Rotating circles with angles */
float curAngle = 0;
float incAngle = ( 360.0/(views.count) )*3.14/180.0;
CGPoint circleCenter = CGPointMake(380, 580); /* given center */
float circleRadius = 250; /* given radius */
for (UIView *view in views)
{
CGPoint viewCenter;
viewCenter.x = circleCenter.x + cos(curAngle)*circleRadius;
viewCenter.y = circleCenter.y + sin(curAngle)*circleRadius;
//view.transform = CGAffineTransformRotate(view.transform, curAngle);
view.center = viewCenter;
[self.view addSubview:view];
curAngle += incAngle;
}
Some coding suggestions:
You can use arc4random() function if you don't have anything special in your own random number generator.
You can turn on ARC in xCode!
In a loop set their centers accordingly, don't rotate them nor their super view.
The following is not directly related to your question but may be helpful:
Aparaently you have got all the math available. I suggest to measure how much cpu time gets lost on the cos and sin etc. calls. If you find that significant then think about your algorithm. You will (most probably) find out that you call cos and sin hundrets or thousands of times for a limited number of angles.
You may then try it and find out that possible pre-calculations or just "caching and reusing" earlier results may save significant processing time.
Plus pre-calculating or caching of sinus would do. You can derrive cosinus values from sinus (and vice versa) by adding (or substracting respectively) an offset of pi/2 (or 90 degrees respectively) to the argument.
For similar tasks I was working with degrees (not radiants) and found out that I could not predict the angles but that a significant of full degrees (1°, 2°, 3°, ... and nothing in between) was exact enough for the job. Then I maintained an array of 360 sinus values and used that instead of calling the real function again and again. (Plus I only had to calculate 90 of them and mirrored the results of the other 270 degrees according to the nature of the sinus function) 180 floats is not too much of memory compared to the speed that I gained. Something like that can be suitable for you too. In your case your potenital agruments to sin and cos are limited to full-number multipilers of incAngle.
Which means there are only [views count] number of potential values each.

How to change the XAxis used on my chart ShinobiCharts

I'd like to have the xAxis of my chart to look like this :
Jan, Feb, Mar, Apr, ..... Nov, Dec
Right now it's following it's default, numbering xAxis according to the number of Data Points.
How can i achieve this change to this axis ?
I've tried using Category Axis and setting an NSMutableArray containing these strings ("Jan", "Feb"...) as categories and with a numberRange going from 1 to 12 but it didn't work.
chart = [[ShinobiChart alloc] initWithFrame:chartEmbaixo.frame withPrimaryXAxisType:SChartAxisTypeCategory withPrimaryYAxisType:SChartAxisTypeNumber];
NSMutableArray * monthNames = [[NSMutableArray alloc] initWithObjects:#"Jan", #"Fev", #"Mar", #"Abr", #"Mai", #"Jun", #"Jul", #"Ago", #"Set", #"Out", #"Nov", #"Dez", nil];
SChartNumberRange * numberRange = [[SChartNumberRange alloc] initWithMinimum:[NSNumber numberWithInt:1]andMaximum:[NSNumber numberWithInt:12]];
SChartCategoryAxis *xAxis = [[SChartCategoryAxis alloc] initWithRange:numberRange];
xAxis.categories = monthNames;
chart.xAxis = xAxis;
first i use as my x axis
Edit how i make my x axis:
SChartNumberRange *r1 = [[SChartNumberRange alloc] initWithMinimum:[NSNumber numberWithInt:0] andMaximum:[NSNumber numberWithInt:2]];
SChartCategoryAxis *xAxis = [[SChartCategoryAxis alloc] initWithRange:r1];
xAxis.title = #"";
//xAxis.enableGesturePanning = YES;
xAxis.enableGesturePanning = YES;
xAxis.style.gridStripeStyle.showGridStripes = NO;
xAxis.style.majorGridLineStyle.showMajorGridLines = NO;
when you make you data points it should use the xValue as the x axis point.
like this:
dp.yValue = 1000;
dp.xValue = #"Jan";
the xValue should be set as the x point for that particular data point. This should work, but if it doesn't or you want to do something more complex you can extend this method from SChartDelegate protocol:
-(void)sChart:(ShinobiChart *)chart alterTickMark:(SChartTickMark *)tickMark beforeAddingToAxis:(SChartAxis *)axis
in this method you have the tickMark.tickLabelis the axis label for that given point where you can do your editing. Don't forget to verify what axis your on.
Hope this helps. If not tomorrow i can post you some code from my project (currently i don't have access to it from where i am)
Edit: currently i have this code:
- (void)sChart:(ShinobiChart *)chart alterTickMark:(SChartTickMark *)tickMark beforeAddingToAxis:(SChartAxis *)axis {
if (chart.yAxis == axis ) return;
for (UIView *i in tickMark.tickMarkView.subviews)
[i removeFromSuperview];
tickMark.tickMarkView.frame = CGRectMake(0, 0, 170, 75);
//center the marker at the right place because the size was changed
tickMark.tickMarkX = tickMark.tickMarkX - (tickMark.tickMarkView.frame.size.width/2) ;
tickMark.tickMarkY = 10;
//img
UIImageView *img = [[UIImageView alloc] initWithImage:[UIImage imageNamed: #"graph_bar_tag_2#2x.png"]];
img.frame = CGRectMake( 0, 0, tickMark.tickMarkView.frame.size.width, tickMark.tickMarkView.frame.size.height);
[tickMark.tickMarkView addSubview:img];
//label with the markView's size with 7px padding on the left and on the right
UILabel *label = [[UILabel alloc] initWithFrame: CGRectMake( 7, 5, tickMark.tickMarkView.frame.size.width-14, 15)];
label.backgroundColor = [UIColor clearColor];
//tikMark.tickLabel has an pair of indexes so that i can easily find the data for this particular data point and series.
label.text = [_dataSource getNameFor: tickMark.tickLabel.text];
label.textAlignment = UITextAlignmentCenter;
//color_other_light is a UIColor var
[label setTextColor: color_other_light];
[tickMark.tickMarkView addSubview:label];
...
}

Increment UISlider by 0.2 in range 0 to 2.0 [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Increment UISlider by 1 in range 1 to 100
I am new to iPhone,
How do I have my UISlider go from 0 to 2 in increments of 0.2?
slider = [[UISlider alloc] init];
[slider addTarget:self action:#selector(sliderChange:) forControlEvents:UIControlEventValueChanged];
[slider setBackgroundColor:[UIColor clearColor]];
slider.minimumValue = 0.0;
slider.maximumValue = 2.0;
slider.continuous = YES;
slider.value = 0.0;
- (IBAction)sliderChange:(id)sender{
NSLog(#"slider.value=%f",slider.value);
}
When i slide my log shows...
slider.value = 1.000000
slider.value = 1.123440
slider.value = 1.234550
slider.value = 1.345670
slider.value = 1.567890
.
.
.
I want slider value as 0.0 , 0.2 , 0.4 and so on...
Any help will be appriciated.
In your sliderChanged method, take the value from your slider, round it to the nearest 0.2, then set the value of your slider to this rounded value. This will snap the slider thumb to 0.2 increments.
You can round like this:
float roundedValue = roundf(value / 0.2f) * 0.2f;
(Thanks #koki)

Move left and right in UIScrollView

I have a UIScrollView which scrolls only horizontally. I have a situation where I have views filled by array in UIScrollView. There are three views which altogether make one single component in UIScrollView (there are many such components). Component contains :
UILabel
UIButton
UIView (for underlying text)
I have placed two arrow keys(left and right) at the ends of UIScrollView. On touching left arrow I am shifting one position left (with complete string to display) in UIScrollView and one position right (with complete string to display) in case of right arrow button.
Till now I am able to perform left arrow action. But I have no idea how to perform right arrow touch and how to display complete string to touching arrow keys if only part of that string is being displayed.
I know it is weird that I have a UIScrollView and still want to add arrow keys to move it. But can't help it. This is client's requirement. The following is how I implemented left arrow action:
-(IBAction) onClickLeftArrow
{
NSLog(#"BUTTON VALUE : %i",iButtonValue);
if(iButtonValue <= [m_BCListArray count]) {
NSArray *arr = [scrollDemo subviews];
for(int j = 0; j < [arr count]; j++) {
UIView *view1 = [arr objectAtIndex:j];
[view1 removeFromSuperview];
}
XX = 5.0;
int tagcount = [m_BCListArray count]-iButtonValue;
NSLog(#"%#",m_BCListArray);
for(int i = iButtonValue; i >= 1; i--)
{
UILabel *blabel = [[UILabel alloc] initWithFrame:CGRectMake(XX, 6, 120, 26)];
blabel.text = [m_BCListArray objectAtIndex:[m_BCListArray count]-i];
blabel.backgroundColor = [UIColor clearColor];
blabel.textColor = [UIColor whiteColor];
blabel.font = [UIFont systemFontOfSize:14];
[scrollDemo addSubview:blabel];
//underline code
CGSize expectedLabelSize = [[m_BCListArray objectAtIndex:[m_BCListArray count]-1] sizeWithFont:blabel.font constrainedToSize:blabel.frame.size lineBreakMode:UILineBreakModeWordWrap];
CGRect newFrame = blabel.frame;
newFrame.size.height = expectedLabelSize.height;
blabel.frame = CGRectMake(blabel.frame.origin.x, blabel.frame.origin.y, expectedLabelSize.width+10, expectedLabelSize.height);
blabel.numberOfLines = 1;
[blabel sizeToFit];
int width=blabel.bounds.size.width;
int height=blabel.bounds.size.height;
UIButton *btnContent = [UIButton buttonWithType:UIButtonTypeCustom];
[btnContent addTarget:self action:#selector(SelectButton:)forControlEvents:UIControlEventTouchDown];
btnContent.tag = tagcount+1;
btnContent.frame=CGRectMake(XX, 2.0, width+10, height);
[scrollDemo addSubview:btnContent];
scrollDemo.contentSize = CGSizeMake(XX+50, 32);
//scrollDemo.contentSize = CGSizeMake(expectedLabelSize.width,expectedLabelSize.height);
UIView *viewUnderline=[[UIView alloc] init];
viewUnderline.frame=CGRectMake(XX, 26, width, 1);
viewUnderline.backgroundColor=[UIColor whiteColor];
[scrollDemo addSubview:viewUnderline];
[viewUnderline release];
scrollDemo.contentSize = CGSizeMake(XX+width+10, 32);
XX = XX + width+10;
iRight = iRight + 1;
tagcount ++;
// iRight --;
}
}
iButtonValue = iButtonValue+1;
}
Set the contentOffset of UIScrollView on the Button click action and u'll get your desired effect.