UIDynamicAnimator停止对重力变化作出反应

时间:2015-04-17 00:28:17

标签: ios swift game-physics uidynamicbehavior uidynamicanimator

我遇到了https://stackoverflow.com/questions/28275004/uidynamicanimator-stop-reacting-to-uigravitybehavior中所述的完全相同的问题。我也无法解释它。

有谁知道为什么UIDynamicAnimator会突然停止为附加了UIGravityBehavior的对象/项目制作动画?

修改

我正在运行Big Nerd Ranch

中的示例

我修改了示例,因此只有两个立方体,以便更容易地再现错误。立方体正在倒下并对重力作出反应就好了,但是如果你倾斜手机使立方体最终落在角落里,相互接触,那么就会停止任何运动,从而不再对重力行为作出反应。

我也想知道,如果这是一个Swift问题。也许我应该尝试在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;
    }
}

EDIT2

我刚注意到他们不必互相触碰以停止响应UIGravityBehavior。

EDIT3

好吧,似乎是Swift中的一个错误。我在ObjC中实现了相同的功能,没有任何问题。

1 个答案:

答案 0 :(得分:2)

问题是重力更新会导致在主线程以外的线程上调用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()