如何检查是否启用了视差

 什锦平 发布于 2023-02-13 09:53

我正在制作一个壁纸应用程序,并想检查用户是否在他的iOS 7设备上启用了视差.在Objective-C中有没有办法让我检查一下?Apple是否授予我们开发人员检查此布尔值的权限?

即如果启用了视差,则执行step1,否则执行step2

2 个回答
  • 根据Gabriele的回答,似乎没有办法直接读取这个值.

    作为一种解决方法,您可以利用这样的事实:UIInterpolatingMotionEffect如果运动是默认值,但如果启用了减少运动则不执行任何操作.

    因此UIView,请UIInterpolatingMotionEffect在应用程序启动时使用自定义类并立即附加实例.如果属性已更改,请设置标志.稍后检查那个标志.

    可能存在一些您可以依赖的其他经验副作用,但表面看来假设您的用户在使用您的应用程序时会移动设备.所以你肯定会知道他们是否有动作并且已经移动了他们的设备,否则你将无法知道他们是否已关闭动作或者没有移动他们的设备.

    也许聪明的人能想出更好的东西?

    编辑:示例代码.所面临的问题正如评论中所讨论的那样:该属性必须是可动画的,其具有需要手动轮询循环的净效果.在应用程序的某个位置添加此视图的实例,理想情况是在启动时,以便它在应用程序的整个生命周期内保持在屏幕上,当然,视图控制器层次结构允许它.然后观看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
    

    2023-02-13 09:58 回答
  • 从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的任何内容,我认为没有一种合法的方式可以说明.

    2023-02-13 09:58 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有