Can I disable UIPickerView scroll sound?

后端 未结 8 1471
臣服心动
臣服心动 2020-11-30 10:16

I want to disable the annoying clicks that the UIPickerView generates upon scrolling up and down. Is there a way to do this? I want to play short sounds for each item that t

相关标签:
8条回答
  • 2020-11-30 10:56

    I found small quickie solution for this try below

            UIPickerView *pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, yPickerView, VIEW_WIDTH, PICKERVIEW_HEIGHT)];
            pickerView.delegate = self;
            pickerView.dataSource = self;
            pickerView.showsSelectionIndicator = YES;
            pickerView.alpha = 0.8f;
            pickerView.tag = fieldTag;
            [pickerView selectRow:pickerViewSelectedIndex inComponent:0 animated:NO];
    

    set the animated:NO for selectRow: method

    0 讨论(0)
  • 2020-11-30 10:58

    I've been struggling with a UIPickerView sound issue, and even though it's only partially relevant to the original question, I'm posting the problem/solution here because this topic keeps coming up in my search results so I think anyone else in the same boat may end up here too…

    I needed to initialize a UIPickerView to restore the currently selected row from saved data. Simple, right? In viewDidLoad, just call the selectRow:inComponent:animated method of UIPickerView:

    [myPicker selectRow:currentRowIndex inComponent:0 animated:NO];
    

    This works as expected, but has a side effect that it generates a single "click" sound as if the user had scrolled the control. The click sound only occurs when running on a device (not the simulator), and only if the device has iOS 3.x installed (I tested with 3.1.3 and 3.2). This was apparently a bug in iOS that was fixed starting with iOS 4.0. But if you need to target Gen1 iPhone, you're stuck with iOS 3.1.3 where this problem is present.

    I discussed the issue with Apple DTS, but they were unable to suggest any workaround other than upgrading to 4.0. I asked if they would make an exception and permit the use of the undocumented setSoundsEnabled mentioned above (which does actually solve the problem). The answer was, "There are no exceptions."

    After some additional detective work, I discovered that you can prevent the sound from occurring by temporarily removing the UIPickerView from the superview, call selectRow, then re-add it to the superview. For example, in viewDidLoad:

    UIView *superview = [myPicker superview];
    [myPicker removeFromSuperview];
    
    [myPicker reloadAllComponents];
    [myPicker selectRow:currentRowIndex inComponent:0 animated:NO];
    
    [superview addSubview:myPicker];
    

    This gets rid of the extraneous click sound without using undocumented/private APIs so should pass Apple's approval process.

    0 讨论(0)
  • 2020-11-30 11:00

    There is an undocumented way (I'm actually not sure if it is still available in iphone 3.0) but here it is any way

    #import <UIKit/UIKit.h>
    
    @interface SilintUIPickerView: UIPickerView
    { }
    
    - (void) setSoundsEnabled: (BOOL) enabled;
    @end
    

    use this subclass instead and call [view setSoundsEnabled: NO]

    I'm interested in knowing how it goes in the latest SDK, give it a shot and let us know.

    0 讨论(0)
  • 2020-11-30 11:04

    Someone I know says he got this past the App Store review just last week:

    // Hide private API call from Apple static analyzer
    SEL sse = NSSelectorFromString([NSString stringWithFormat:@"%@%@%@", @"set",@"Sounds",@"Enabled:"]);
    if ([UIPickerView instancesRespondToSelector:sse]) {
        IMP sseimp = [UIPickerView instanceMethodForSelector:sse];
        sseimp(self.thePicker, sse, NO);
    }
    
    0 讨论(0)
  • 2020-11-30 11:10

    After using this specific undocumented api for over a year on the App Store Apple finally asked me to remove it from my App. It is very frustrating for audio apps to have that damn click sound. The best advice is to share with users that the picker sound can be disabled globally in the settings application under "Sounds" and setting "Keyboard Clicks" to "Off". I also strongly recommend visiting https://bugreport.apple.com/ and filing a bug for UIPickerView, as it can cause distortion in audio applications when the picker click is played.

    0 讨论(0)
  • 2020-11-30 11:11

    Could this trick work? Someone was able to suppress the camera shutter sound effect by playing an inverted copy of the sound at the same moment: https://stackoverflow.com/a/23758876/214070

    0 讨论(0)
提交回复
热议问题