How to Check if Parallax is enabled

前端 未结 3 1970
再見小時候
再見小時候 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
    

提交回复
热议问题