如何检查是否启用视差
我正在制作壁纸应用程序,并希望检查用户是否在其iOS 7设备上启用了视差。 目标C中有一种方法可以检查吗? 苹果是否授予我们开发者访问权限检查这个布尔值?
即如果启用视差,则执行step1,否则执行step2
从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);
对于比iOS 8早的任何东西,我不认为有一个合理的方式来说明。
根据Gabriele的回答,似乎没有办法直接读取数值。
作为一种解决方法,您可以利用UIInterpolatingMotionEffect
做一些事情,如果运动是默认的,但是如果启用了减less运动则什么也不做。
所以使用一个自定义的UIView
类,并在应用程序启动时立即附加一个UIInterpolatingMotionEffect
的实例。 设置一个标志,如果属性更改。 稍后检查该标志。
可能还有一些其他经验性的副作用可以依靠,但初步认为您的用户会在使用您的应用时移动设备。 所以,如果他们有动作并且移动了他们的设备,你肯定会知道,否则你不知道他们是否closures了动作或者没有移动他们的设备。
也许有人更聪明可以拿出更好的东西?
编辑:示例代码。 正如评论中所讨论的那样,所面临的问题是:财产必须具有animation性,其实质上需要手动轮询循环。 在你的应用的某个地方添加一个这个视图的实例,理想的情况是在启动的时候,这样它就保持在你的应用的整个生命周期的屏幕上,当然你的视图控制器层次允许它。 然后观看parallaxHasOccurred属性。 它符合KVO,也可以轮询。 正如所讨论的,它可能会产生假阴性,但不应该产生误报。
@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
对于不支持视差的设备(即iPhone 5之前的任何iPhone型号),只需检查型号并确保没有视差。
对于支持它的设备,您应该以编程方式检查Reduce Motion可访问性设置,但显然没有用于检查该选项是否打开的公共API。
根据UIKit函数参考 ,您可以执行的唯一检查如下
-
UIAccessibilityPostNotification
-
UIAccessibilityIsVoiceOverRunning
-
UIAccessibilityIsClosedCaptioningEnabled
-
UIAccessibilityRequestGuidedAccessSession
-
UIAccessibilityIsGuidedAccessEnabled
-
UIAccessibilityIsInvertColorsEnabled
-
UIAccessibilityIsMonoAudioEnabled
-
UIAccessibilityZoomFocusChanged
-
UIAccessibilityRegisterGestureConflictWithZoom
-
UIAccessibilityConvertFrameToScreenCoordinates
-
UIAccessibilityConvertPathToScreenCoordinates