gpt4 book ai didi

ios - UIDynamicAnimator 停止对重力变化使用react

转载 作者:行者123 更新时间:2023-11-28 13:14:24 28 4
gpt4 key购买 nike

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

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

编辑

我正在运行 Big Nerd Ranch 中的示例

我修改了示例,因此只有两个立方体,以便更容易重现错误。立方体正在下落并且对重力的 react 很好,但是如果你倾斜手机使立方体最终在角落里,相互接触,然后它停止任何运动,因此不再对重力行为使用react。

我也想知道,这是否是 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 中实现了相同的功能,没有任何问题。

最佳答案

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

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com