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

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)

Related

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.

Programmatically create UIButtons on CircleImage

I need to create Arch shaped UIButtons, on a circle image.(the O/p need to look like Concentric Circles),
At present I am using 5 images, but in future I may add some more Images, Dynamically I need to fil the circle Image with the added images.
I had a sample Piece of code and O/P is the below image
int numberOfSections = 6;
CGFloat angleSize = 2*M_PI/numberOfSections;
for (int j = 0; j < numberOfSections; ++j) {
UIButton *sectionLabel = [[UIButton alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 150.0f, 2.0f)];
sectionLabel.backgroundColor = [UIColor redColor];
sectionLabel.layer.anchorPoint = CGPointMake(1.0f, 0.5f);
sectionLabel.layer.position = CGPointMake(container.bounds.size.width/2.0, container.bounds.size.height/2.0); // places anchorPoint of each label directly in the center of the circle.
sectionLabel.transform = CGAffineTransformMakeRotation(angleSize*j);
[container addSubview:sectionLabel];
}
I have tried with this piece of code and the O/P is the below image
int numberOfSections = 5;
CGFloat angleSize = 2*M_PI/numberOfSections;
for (int j = 0; j<numberOfSections; ++j) {
UIImage *imageframe = [imagesArray objectAtIndex:j];
OBShapedButton *button = [[OBShapedButton alloc] initWithFrame:CGRectMake(0.0f, 150.0f, 150.0f, 128.0f)];
button.transform = CGAffineTransformMakeRotation(angleSize*j);
[container addSubview:button];
}
I need my output as the below image
How can i Achieve that
O/P after I Tried with Sarah Zuo's code
same width and height buttons
AnyKind of Help is more Appreciated
I can answer only last part,
If you are able to get images for buttons, then you can refer this tutorial. This might help you. Provided, your image formed is having transparent background.
float radius = container.bounds.size.width / 2;
int numberOfSections = 5;
CGFloat angleSize = 2*M_PI/numberOfSections;
for (int j = 0; j<numberOfSections; ++j) {
UIImage *imageframe = [imagesArray objectAtIndex:j];
OBShapedButton *button = [[OBShapedButton alloc] initWithFrame:CGRectMake(0.0f, 150.0f, 150.0f, 128.0f)];
button.transform = CGAffineTransformMakeRotation(2*M_PI-angleSize*j);
button.center = CGPointMake(radius + radius * sin(angleSize * j) / 2, radius + radius * cos(angleSize * j) / 2);
[container addSubview:button];
}
Try this code.
You can use your UIImageView instead. You can give different tag to image view and add Gesture Recognizer. Now you can get touch events like button click event on image views.
If all buttons' image look like the one (same direction), the transform value may be work.
button.transform = CGAffineTransformMakeRotation(2*M_PI-angleSize*j);
button.center = CGPointMake(radius + radius * sin(angleSize * j) / 2, radius + radius * cos(angleSize * j) / 2);

CAKeyframeAnimation repeat after a delay

I have 3 uiviews which have a CAKeyframeAnimation layer added to them to bounce. Currently i add these to each uiview after a delay and set them to repeat.
What i need them to do is repeat but after a delay. so view 1 animates, but waits till after view 2 and 3 have animated before starting again.
I can't seem to read anywhere in the docs that mention repeating a CAKeyframeAnimation with a repeat start time delay.
Can anyone think of an idea to get the delays between the views?
Thanks
Dan
Code so far:
- (CAKeyframeAnimation *)jumpAnimation
{
// these three values are subject to experimentation
CGFloat initialMomentum = 80.0f; // positive is upwards, per sec
CGFloat gravityConstant = 250.0f; // downwards pull per sec
CGFloat dampeningFactorPerBounce = 0.6; // percent of rebound
// internal values for the calculation
CGFloat momentum = initialMomentum; // momentum starts with initial value
CGFloat positionOffset = 0; // we begin at the original position
CGFloat slicesPerSecond = 60.0f; // how many values per second to calculate
CGFloat lowerMomentumCutoff = 5.0f; // below this upward momentum animation ends
CGFloat duration = 0;
NSMutableArray *values = [NSMutableArray array];
do
{
duration += 1.0f/slicesPerSecond;
positionOffset+=momentum/slicesPerSecond;
if (positionOffset<0)
{
positionOffset=0;
momentum=-momentum*dampeningFactorPerBounce;
}
// gravity pulls the momentum down
momentum -= gravityConstant/slicesPerSecond;
CATransform3D transform = CATransform3DMakeTranslation(0, -positionOffset, 0);
[values addObject:[NSValue valueWithCATransform3D:transform]];
} while (!(positionOffset==0 && momentum < lowerMomentumCutoff));
CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:#"transform"];
animation.duration = duration;
animation.fillMode = kCAFillModeForwards;
animation.values = values;
animation.repeatCount = HUGE_VAL;
animation.delegate =self;
animation.autoreverses = YES;
return animation;
}

Changing the Range of a UISlider Programmatically

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

Triple UISwitch

I want to create a custom UISwtich with three positions. Is it possible?
You should be using UISegmentedControlif you want a standard UI-Element or configure a UISlider with a range of 2:
slider.minimumValue = 0;
slider.maximumValue = 2;
slider.continuous = NO;
And then set the minimumValueImage, maximumTrackImage and thumbImage to use appropriate images.
Not using the built-in UISwitch. You'll need to roll your own.
Why not use a UISegmentedControl?
Using UISlider is a good approach. But you would additionally want to adjust the mechanics of your UISlider to be more UISwitch-like. I.e., when you change its position incompletely then it should bounce back to the home position.
Here's what I ended up doing (using a part of FelixLam's answer):
UISlider *slider = [[UISlider alloc] initWithFrame:CGRectMake(screenRect.size.width*0.5-width/2, screenRect.size.height*0.95-height, width, height)];
slider.minimumValue = 0;
slider.maximumValue = 2;
slider.continuous = NO;
slider.value = 1;
[slider addTarget:self action:#selector(sliderAction:) forControlEvents:UIControlEventValueChanged];
Along with...
- (void)sliderAction:(UISlider *)slider {
float origValue = slider.value;
[UIView beginAnimations:nil context:NULL];
if (slider.value<1.9 && slider.value>0.1) slider.value=1;
else if (slider.value>1.9) slider.value=2;
else slider.value=0;
[UIView setAnimationDuration:0.2*fabs(slider.value-origValue)];
[UIView commitAnimations];
}