首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >基于设备运动辊的UIDynamicAnimator移动视图

基于设备运动辊的UIDynamicAnimator移动视图
EN

Stack Overflow用户
提问于 2015-07-22 23:20:03
回答 1查看 604关注 0票数 2

我正在尝试阅读我的设备运动,特别是当设备处于景观模式时的滚动,并将返回的角度转换为一个UIView的位置(基本上使屏幕上的“水平”显示用户手机处于一个理想的角度)。

此代码提供了所需的滚动结果,但由于某些原因,没有按预期更新levelIndicator视图。我没有收到任何错误,所以我一定是不正确地使用UIPushBehavior,但我不清楚我需要修复什么。我不确定是否设置为新的Y位置的指示器上的运动更新。

代码语言:javascript
复制
import UIKit
import AVFoundation
import CoreMotion
import GLKit

class CameraView: BaseViewController {

    var animator : UIDynamicAnimator? = nil;
    var currentRoll     : Float = 0.0;
    let manager = CMMotionManager()
    let motionQueue = NSOperationQueue()

    var countingDown:Bool = false;

    @IBOutlet weak var levelIndicator: UIView!
    @IBOutlet weak var level: UIView!

    override func viewDidLoad() {
        super.viewDidLoad()
        self.animator = UIDynamicAnimator(referenceView: self.level)
        let continuousPush: UIPushBehavior = UIPushBehavior(items: [levelIndicator], mode: UIPushBehaviorMode.Continuous)
        self.animator?.addBehavior(continuousPush)
    }

    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(true)
        self.startReadingMotion()
    }

    func startReadingMotion() {
        if manager.deviceMotionAvailable {
            manager.deviceMotionUpdateInterval = 0.1
            manager.startDeviceMotionUpdatesToQueue(motionQueue, withHandler: checkStability)
        }
    }

    func checkStability(motion: CMDeviceMotion!, error: NSError!) {

        var orientation = UIDevice.currentDevice().orientation

        if (error != nil) {
            NSLog("\(error)")
        }

        var quat = motion.attitude.quaternion

        //We Probably only need to check the Angle of the Roll (Phone Angle in Landscape mode)
        var roll = GLKMathRadiansToDegrees(Float(atan2(2*(quat.y*quat.w - quat.x*quat.z), 1 - 2*quat.y*quat.y - 2*quat.z*quat.z))) ;

        //Other Angles are available for more refinement to stabilty

        //var pitch = GLKMathRadiansToDegrees(Float(atan2(2*(quat.x*quat.w + quat.y*quat.z), 1 - 2*quat.x*quat.x - 2*quat.z*quat.z)));
        //var yaw = GLKMathRadiansToDegrees(Float(asin(2*quat.x*quat.y + 2*quat.w*quat.z)));

        if(orientation == .LandscapeLeft) {
            roll *= -1
        }

        if(roll > 100) {
            roll = 100
        } else if(roll < 0) {
            roll = 0
        }

        self.currentRoll = roll

        var pos = self.level.frame.height*CGFloat(roll/100)
        var rect = self.levelIndicator.frame
        rect.origin.y = pos
        self.levelIndicator.frame = rect

        if(roll > 85 && roll < 87) {
            if(!countingDown) {
                //This is the ideal roll position of the phone
                self.levelIndicator.backgroundColor = UIColor.redColor()
            }
        } else {
            countingDown = false;
            self.levelIndicator.backgroundColor = UIColor.blackColor()
        }
    }

    func stopReading() {
        manager.stopDeviceMotionUpdates();
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-08-17 22:31:23

对于感兴趣的人,我最终没有使用UIDynamicAnimator,但找到了一个简单得多的解决方案,转换返回的姿态变化的半径,并使用它来检查设备的滚动。还向主队列添加了一个分派,以更新屏幕级别的UI。

代码语言:javascript
复制
func checkStability(motion: CMDeviceMotion!, error: NSError!) {

    var orientation = UIDevice.currentDevice().orientation

    if (error != nil) {
        NSLog("\(error)")
    }

    var roll:Float = 0.0
    if let attitude = motion.attitude {
        roll = GLKMathRadiansToDegrees(Float(attitude.roll))
    }

    dispatch_async(dispatch_get_main_queue()) {
        var pos = self.level.frame.height*CGFloat(roll/100)
        var rect = self.levelIndicator.frame
        rect.origin.y = self.level.frame.height-pos
        self.levelIndicator.frame = rect

        if(roll > 85 && roll < 90) {
            //This is where I want the Roll to be
        } else {
            //The Roll is not correct yet
        }
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31575647

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档