Search This Blog

Wednesday, March 17, 2010

iPhone: Activate UISlider and set its value to the location of the current touch programatically

Programmer Question

Is it possible to set an UISlider as first responder and set its current value to the location of the current touch programatically?



The way my app is set up I have a UIView container that takes up the whole screen. Inside the container I have another UIView offscreen at the bottom edge (I'll call this bottomBar). Inside the bottomBar there is a UISlider element.



The idea is that when the user swipes along the bottom edge of the screen, the bottomBar with the slider move into the view. What I am trying to achieve is to activate the UISlider, and set the position of the slider (the value) to the position of the users touch. Is this possible?



I did figure out a pretty ugly hack to get this done. But there has to be a more elegant solution than this?



CGPoint sliderPos = [touch locationInView:pageSlider];
float newSliderValue = sliderPos.x/pageSlider.frame.size.width*pageSlider.maximumValue;
if (sliderPos.x<=0) {
pageSlider.value = 0;
}
else if(newSliderValue>=pageSlider.maximumValue) {
pageSlider.value = pageSlider.maximumValue;
}
else {
pageSlider.value = newSliderValue;
}


Two specific problems I have with the code above is that it a) doesn't position the thumb exactly right because it ignores the length of the thumb, and b) the thumb isn't highlighted as it moves.



Find the answer here

No comments:

Post a Comment

Related Posts with Thumbnails