收录日期:2019/03/26 02:52:33 时间:2009-07-29 18:12:50 标签:iphone,objective-c

I have an application that rotates between landscape and portrait mode, which works great. However, it gets really jumpy and wants to rotate at 45deg at the slightest movement of the device. Is there a way to control the sensitivity of rotation, or prevent it from rotating until it hits certain targets (90, 180, 0) while ignoring the in-betweens?

You can probably keep a counter in the shouldAutorotateToInterfaceOrientation: method so that it needs to trigger a few times before you actually switch. Something like a "debounce" circuit in electronics.

(Aside: In electronics, if you're triggering some action whenever a physical button is pressed, you usually have to debounce it. This is because, since the button is a physical object, it won't make perfect contact when a user hits it. Usually there will be multiple short pulses while it makes contact. So the microprocessor keeps a count, and when the switch has been pressed for a full 10mS or something, then it actually triggers.)

So:

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{
    counter++: //ivar
    if (counter > 2) {
        counter=0;
        return YES;
    } else {
        return NO;
    }
}