How to Check if Parallax is enabled

前端 未结 3 1969
再見小時候
再見小時候 2020-12-17 00:55

I am making a wallpaper app and would like to check if a user has parallax enabled on his iOS 7 device. Is there a way in objective-C that I can check that? Has Apple grante

相关标签:
3条回答
  • 2020-12-17 01:45

    As per Gabriele's answer there seems to be no way to read the value directly.

    As a workaround, you could exploit the fact that UIInterpolatingMotionEffect does something if motion is as default but does nothing if reduce motion is enabled.

    So use a custom UIView class and attach an instance of UIInterpolatingMotionEffect immediately at application start. Set a flag if the property is changed. Check that flag later.

    There may be some other empirical side effects you can rely on but that prima facie assumes your user will move the device while using your app. So you'll know for certain if they have motion on and have moved their device but otherwise you won't know whether they have motion switched off or have just not moved their device.

    Maybe someone smarter can come up with something better?

    EDIT: sample code. Issues faced are as discussed in the comments: the property has to be animatable which has a net effect of requiring a manual polling loop. Add an instance of this view somewhere in your app, ideally at launch and so that it stays on screen for the entire lifetime of your app, subject to your view controller hierarchy allowing it, of course. Then watch the parallaxHasOccurred property. It's KVO compliant, or you can poll. As discussed, it may generate false negatives but should never generate false positives.

    @interface PTParallaxTestView : UIView
    
    // this key is KVO compliant
    @property (nonatomic, assign) BOOL parallaxHasOccurred;
    
    @end
    
    
    @implementation PTParallaxTestView
    {
        CGPoint _basePosition;
        UIMotionEffectGroup *_effectGroup;
    }
    
    - (void)didMoveToSuperview
    {
        // cancel any detection loop we may have ongoing
        [NSObject cancelPreviousPerformRequestsWithTarget:self];
    
        // if anything still in doubt and we're on a view then start the
        // detection loop
        if(!self.parallaxHasOccurred && self.superview)
        {
            // add motion effects if they're not already attached; attach both to the centre property
            if(!_effectGroup)
            {
                UIInterpolatingMotionEffect *horizontalMotionEffect = [[UIInterpolatingMotionEffect alloc] initWithKeyPath:@"center.x" type:UIInterpolatingMotionEffectTypeTiltAlongHorizontalAxis];
                horizontalMotionEffect.minimumRelativeValue = @(0);
                horizontalMotionEffect.maximumRelativeValue = @(100);
    
                UIInterpolatingMotionEffect *verticalMotionEffect = [[UIInterpolatingMotionEffect alloc] initWithKeyPath:@"center.y" type:UIInterpolatingMotionEffectTypeTiltAlongVerticalAxis];
                verticalMotionEffect.minimumRelativeValue = @(0);
                verticalMotionEffect.maximumRelativeValue = @(100);
    
                _effectGroup = [[UIMotionEffectGroup alloc] init];
                _effectGroup.motionEffects = @[verticalMotionEffect, horizontalMotionEffect];
                [self addMotionEffect:_effectGroup];
            }
    
            // kick off inspection in 0.1 seconds; we'll subsequently inspect
            // every 0.5 seconds
            [self performSelector:@selector(beginCheckingPresentationPosition) withObject:nil afterDelay:0.1];
        }
    }
    
    - (void)beginCheckingPresentationPosition
    {
        // set the base position and do the first check in 0.5 seconds
        _basePosition = [[[self layer] presentationLayer] position];
        [self performSelector:@selector(checkPresentationPosition) withObject:nil afterDelay:0.5];
    }
    
    - (void)checkPresentationPosition
    {
        // quick note on presentationLayer:
        //
        //  The property supplied to UIInterpolatingMotionEffect must be animatable. So we can't just create our own.
        //  UIKit will then apply effects directly to the layer. Furthermore, the layer itself will act as if in a
        //  perpetual animation so its properties won't directly be affected. We'll have to query the presentationLayer.
        //  (and that's also why we're pulling rather than using KVO or a suitable subclass to push)
        //
        CGPoint newPosition = [[[self layer] presentationLayer] position];
    
        // if the position has changed since the original test then things are in motion
        if(fabs(newPosition.x - _basePosition.x) > 0.125 || fabs(newPosition.y - _basePosition.y) > 0.125)
            self.parallaxHasOccurred = YES;
    
        // check again in 0.5 seconds only if we don't already know the answer
        if(!self.parallaxHasOccurred)
            [self performSelector:@selector(checkPresentationPosition) withObject:nil afterDelay:0.5];
    }
    
    @end
    
    0 讨论(0)
  • 2020-12-17 01:46

    For devices not supporting the parallax (i.e. any iPhone model before iPhone 5) you can just check the model and be sure that no parallax is on.

    For the device supporting it you should programmatically check the Reduce Motion accessibility setting, but apparently there's no public API for checking whether that option is on.

    According to the UIKit Function Reference, the only checks you can perform are the following

    • UIAccessibilityPostNotification
    • UIAccessibilityIsVoiceOverRunning
    • UIAccessibilityIsClosedCaptioningEnabled
    • UIAccessibilityRequestGuidedAccessSession
    • UIAccessibilityIsGuidedAccessEnabled
    • UIAccessibilityIsInvertColorsEnabled
    • UIAccessibilityIsMonoAudioEnabled
    • UIAccessibilityZoomFocusChanged
    • UIAccessibilityRegisterGestureConflictWithZoom
    • UIAccessibilityConvertFrameToScreenCoordinates
    • UIAccessibilityConvertPathToScreenCoordinates
    0 讨论(0)
  • 2020-12-17 01:55

    As of iOS 8:

    // Returns whether the system preference for reduce motion is enabled
    UIKIT_EXTERN BOOL UIAccessibilityIsReduceMotionEnabled() NS_AVAILABLE_IOS(8_0);
    UIKIT_EXTERN NSString *const UIAccessibilityReduceMotionStatusDidChangeNotification NS_AVAILABLE_IOS(8_0);
    

    For anything earlier than iOS 8, I don't think there's a legit way to tell.

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