How to make the set of possible values for UISlider an open interval? - swift

I am using a UISlider to allow a user to choose among four options. I ask the user a question of the form "how much do you like this thing?", they respond by moving the slider, and then behind the scenes I map the value of the slider to one of four elements of an array. I want the first quarter of the slider to correspond to the first element of the array, the second quarter of the slider to correspond to the second element of the array, and so on.
By default, the range of a slider is [0,1], so one natural way to do what I want is:
let index = Int(rangedSlider.value * Float(currentAnswers.count) )
assuming that rangedSlider is the name of the outlet connected to the UISlider in question, and currentAnswers is the array of answers I'm mapping to. The issue with this is that if the user moves the slider all the way to the right, that sets the value of the slider to 1, which maps to index 4 by the formula above (assuming there are four answers). I can always add an additional line of code that takes care of that case separately,
if rangedSlider.value == 1 { index = currentAnswers.count - 1 }
but I prefer a one line solution.
I can also set the max value of the slider to 0.99 in the Storyboard and stick with the first line of code I wrote, but I want to know: is there a built-in way to make the range of UISlider values an open interval? Is there a better way to get the behavior I want?

Is this any more complicated than:
let index = Int(rangedSlider.value * Float(currentAnswers.count - 1))
Or have I misunderstood the problem you're having?
That way your index will be returned as a number between 0 and 3 with 0 corresponding to the first answer and 3 corresponding to the fourth answer.

Related

Is there a way to set the amount a sliders value changes when you use it?

I'm am trying to make it so that when you interact with a slider, it goes up or down by 5. I know you can make it go up in whole numbers but can you change the base value that it increases/decreases by?
Not a built-in feature unfortunately. The workaround is to enable Whole Numbers, and set:
Slider min = Actual min / Step
Slider max = Actual max / Step
Then in your code you can do Actual value = Slider value * Step
Or you can write your own slider component with stepping support.
PS: I know, it sucks.

Interface variables - sliders affecting sliders

Let's say I have two sliders on my interface. Slider one goes from 0-500 and slider two goes from 0-100.
Is there anyway of setting it so that the value of slider one influences the possible values for slider two. So if I set slider one to 500, slider two can only be values from 0-30, for example.
Alternatively it would not necessarily have to be a slider affecting another slier, it could be a button. So if I have buttons A and B, if I select A the slider can be 0-10 but if I select B only 20-50.
Bit of a strange query I admit, I've just been tasked with making an interactive "model" for kids to try out different systems.
Thanks for taking the time to read this.
Yes, you can do this. Example: make 3 sliders: test0, test1, test2. Let the min and max values of test0 be test1 and test2. Let the min and max values of test1 be 0 and 5, and set a value of 1. Let the min and max values of test2 be 95 and 100, and set a value of 99. Your test0 slider will now work as hoped.
BUT what happens if you make the min and max values of test0 to test2 and test1? OK, you get odd behavior, which is likely to look broken to your "kids". So ... this arrangement is not a good one. If you describe your goals in more details, maybe we can find a better solution.

Slider settings for GPUImage

I'm making an app which allows the user to apply GPUImage filters to still photos using a UISlider. I'd like for the slider to initially start at the zero point for each filter (i.e. the value at which none of the filter has been applied yet) and I'm wondering how this can be determined? I've used some of the values that are listed in the GPUImage documentation and for certain sliders they start at 0, but others it's hard to determine (and for some, the min and max values are way off for me). The values for something like GPUImagePosterizeFilter seem to be way off for me (set min to 1, max to 128 and initial to 1). I've also checked some of the values in the FilterShowcase test project which are different than the documentation, but still don't always start at 0. Am I just completely missing the point here? Or is there some setting I maybe have to turn on to be in line with the slider values?
Nope, no setting for this. All I can really recommend is that to make this as efficient as possible make a switch statement in a single method and determine what to do by index of the currently selected filter.
From there, I would leave the min/max values of the slider the same so that you don't have to animate from one calculated point to another if the filter changes and mathematically convert the slider's value into units that the current filter understands. i.e. 0-1 --> 1-128
I think I might have been approaching this the wrong way. Rather than looking for a "zero point" on a filter, I think I should focus on applying the filter only when the user applies it, and trying to find a good starting point that is close to how the image looks without the filter for the initial value on the slider.

Check if CGPoint is close to another CGPoint

In my app, I have two buttons and a timer that fires an action every second. Each second, it generates four random numbers. The numbers are used as X and Y values of the buttons, and the buttons move to the new points. However, sometimes the new points are close, so the buttons overlap. How can I check to make sure this doesn't happen? I already tried checking if CGRectIntersectsRect of the button frames, but that does nothing.
Thanks for any help!
Just to verify, your CGRectIntersectsRect code/structure looks somewhat similar to this right:
-(void)update {
// This is the method that gets called every second where your four numbers are randomly generated
// Generate random numbers here
// Position the buttons based on 2 of the random numbers
if (CGRectIntersectsRect(button1.frame, button2.frame) {
// regenerate the four numbers
// reposition your buttons
}
Also, I was just wondering, why each second? Maybe perform it on a different thread if you want, and make it 10 times a second so the button overlap does not show for too long, but it depends on how fast you want it I guess.... also why are you generating 4 random numbers if you are only using two (as far as we know).
But, ultimately we need to see your code, otherwise we cannot see what you may be doing wrong.

How to dynamically present text on application view?

I'm working on an app which look like a paper block which keeps score for one game, when user types the score and taps Insert, I want to that score to become visible on that paper block.
Let's say that there could be maximum od 12 lines of score on the paper, and the user must type those 12 lines, what is the best way to display one line of score at the time?
(He enters the score and there is only one score in the first line, he types the score again, and the second score appears in the second line of the paper)
Should I create 12 labels in a row in the nib, and after users types the first score it will display in the first label, after he types the second score it will display in the second label and so on...
Is there an easier way to do this? Because I can't think of any...
Try to use an UITableView and update it dataSource array.