Does anyone know how to make this code stop the knob from rotating past xyz degrees for example stopping the rotation at 340 degrees instead of going the full 360 so you can’t keep turning it around constantly? I’m trying to make a turnable knob. It works but currently it’s not clamped so someone can keep turning it in a complete circle forward and backward. I want to prevent it from doing that so there is a min and max value.
- (void)rotating:(KTOneFingerRotationGestureRecognizer *)recognizer {
[self doRotation:recognizer.rotation];
[recognizer setRotation:0];
}
- (void)doRotation:(float)rot {
CGFloat theAngle = atan2(knob.transform.b, knob.transform.a);
float temp_var = RADIANS_TO_DEGREES(theAngle);
if(temp_var < 0) temp_var = 360 - fabs(temp_var);
float percent = (temp_var / 315) * 100;
float val = ((float)(percent))/100;
knob.transform = CGAffineTransformRotate([knob transform], rot);
if(percent < 100 && percent > 0) {
display.text = [[NSString alloc] initWithFormat:@"%.f", percent];
}
}
I have a rotatable knob example that you can download. It does exactly what you describe.
https://github.com/mattneub/Programming-iOS-4-Book-Examples/blob/master/convertedToIOS5/p618p635knob/p618p635knob/MyKnob.m