我得到了与https://stackoverflow.com/questions/28275004/uidynamicanimator-stop-reacting-to-uigravitybehavior中所述完全相同的问题。我也无法解释得更清楚。
有人知道为什么UIDynamicAnimator会突然停止对附加了UIGravityBehavior的对象/项设置动画吗?
编辑
我正在运行Big Nerd Ranch中的示例
我修改了示例,因此只有两个立方体,以便更容易地再现错误。立方体正在下降,对重力的反应很好,但如果你倾斜手机,使立方体最终在角落里,相互接触,然后它停止任何运动,从而不再对重力行为作出反应。
我也在想,这是否是一个迅速的问题。也许我应该尝试在Obj-C中实现它,看看错误是否仍然存在。
下面是一个例子:

//
//  ViewController.swift
//  Rock Box
//
//  Created by Steve Sparks on 7/11/14.
//  Copyright (c) 2014 Big Nerd Ranch. All rights reserved.
//

import UIKit
import CoreMotion

class ViewController: UIViewController {
    // Most conservative guess. We'll set them later.
    var maxX : CGFloat = 320;
    var maxY : CGFloat = 320;
    let boxSize : CGFloat = 30.0
    var boxes : Array<UIView> = []

    // For getting device motion updates
    let motionQueue = NSOperationQueue()
    let motionManager = CMMotionManager()

    override func viewDidLoad() {
        super.viewDidLoad()
        maxX = super.view.bounds.size.width - boxSize;
        maxY = super.view.bounds.size.height - boxSize;
        // Do any additional setup after loading the view, typically from a nib.
        createAnimatorStuff()
        generateBoxes()
    }

    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        NSLog("Starting gravity")
        motionManager.startDeviceMotionUpdatesToQueue(motionQueue, withHandler: gravityUpdated)
    }

    override func viewDidDisappear(animated: Bool)  {
        super.viewDidDisappear(animated)
        NSLog("Stopping gravity")
        motionManager.stopDeviceMotionUpdates()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        NSLog("Memory warning")
        // Dispose of any resources that can be recreated.
    }

    func randomColor() -> UIColor {
        let red = CGFloat(CGFloat(arc4random()%100000)/100000)
        let green = CGFloat(CGFloat(arc4random()%100000)/100000)
        let blue = CGFloat(CGFloat(arc4random()%100000)/100000)

        return UIColor(red: red, green: green, blue: blue, alpha: 0.85);
    }

    func doesNotCollide(testRect: CGRect) -> Bool {
        for box : UIView in boxes {
            var viewRect = box.frame;
            if(CGRectIntersectsRect(testRect, viewRect)) {
                return false
            }
        }
        return true
    }

    func randomFrame() -> CGRect {
        var guess = CGRectMake(9, 9, 9, 9)

        do {
            let guessX = CGFloat(arc4random()) % maxX
            let guessY = CGFloat(arc4random()) % maxY;
            guess = CGRectMake(guessX, guessY, boxSize, boxSize);
        } while(!doesNotCollide(guess))

        return guess
    }

    func addBox(location: CGRect, color: UIColor) -> UIView {
        let newBox = UIButton(frame: location)
        newBox.backgroundColor = color

        view.addSubview(newBox)
        addBoxToBehaviors(newBox)
        self.boxes.append(newBox)
        newBox.tag = Int(arc4random())
        newBox.addTarget(self, action:"tapped:", forControlEvents: .TouchUpInside)
        return newBox
    }

    func tapped(sender: UIButton) {
        println("sender.tag: ", Int(sender.tag))
    }

    func generateBoxes() {
        for i in 0..<2 {
            var frame = randomFrame()
            var color = randomColor()
            var newBox = addBox(frame, color: color);
        }
    }

    var animator:UIDynamicAnimator? = nil;
    let gravity = UIGravityBehavior()
    let collider = UICollisionBehavior()
    let itemBehavior = UIDynamicItemBehavior()

    func createAnimatorStuff() {
        animator = UIDynamicAnimator(referenceView:self.view);

        gravity.gravityDirection = CGVectorMake(0, 0.8)
        animator?.addBehavior(gravity)

        // We're bouncin' off the walls
        collider.translatesReferenceBoundsIntoBoundary = true
        animator?.addBehavior(collider)

        itemBehavior.friction = 0.7
        itemBehavior.elasticity = 0.1
        animator?.addBehavior(itemBehavior)
    }

    func addBoxToBehaviors(box: UIView) {
        gravity.addItem(box)
        collider.addItem(box)
        itemBehavior.addItem(box)
    }

    //----------------- Core Motion
    func gravityUpdated(motion: CMDeviceMotion!, error: NSError!) {
        let grav : CMAcceleration = motion.gravity;

        let x = CGFloat(grav.x);
        let y = CGFloat(grav.y);
        var p = CGPointMake(x,y)

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

        // Have to correct for orientation.
        var orientation = UIApplication.sharedApplication().statusBarOrientation;

        if(orientation == UIInterfaceOrientation.LandscapeLeft) {
            var t = p.x
            p.x = 0 - p.y
            p.y = t
        } else if (orientation == UIInterfaceOrientation.LandscapeRight) {
            var t = p.x
            p.x = p.y
            p.y = 0 - t
        } else if (orientation == UIInterfaceOrientation.PortraitUpsideDown) {
            p.x *= -1
            p.y *= -1
        }

        var v = CGVectorMake(p.x, 0 - p.y);
        gravity.gravityDirection = v;
    }
}

编辑2
我只是注意到他们不需要互相接触就可以停止对超重力病毒的反应。
编辑3
好吧,好像是斯威夫特身上的虫子。我在ObjC中实现了相同的功能,没有任何问题。

最佳答案

问题是,重力更新将导致在主线程以外的线程上调用gravityUpdated()。您应该保持对主线程的UI更新。
要修复它,请切换到gravityUpdated()中的主线程,如下所示:

func gravityUpdated(motion: CMDeviceMotion!, error: NSError!) {
    dispatch_async(dispatch_get_main_queue()) {
        // rest of your code here
    }
}

或者在主队列上执行CMMotionManager,如下所示:
这条线
let motionQueue = NSOperationQueue()

更改为
let motionQueue = NSOperationQueue.mainQueue()

关于ios - UIDynamicAnimator停止对重力变化使用react,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29688217/

10-12 07:14