gpt4 book ai didi

ios - Swift - UICollisionBehavior 委托(delegate)方法未被调用

转载 作者:行者123 更新时间:2023-11-30 12:08:07 26 4
gpt4 key购买 nike

我正在做一个简单的动画,它需要我处理一些与边界的碰撞。

我有一个类 viewcontroller,我将其扩展为 UICollisionBehaviorDelegate,以便我可以识别和处理 View 冲突。

出于某种原因,当发生冲突时,我的委托(delegate)方法永远不会触发。

class ViewController: UIViewController {

var fallingImageViews: [UIImageView]!
var downAnimator: UIDynamicAnimator!

override func viewDidLoad() {
super.viewDidLoad()

//imagine fallingImageViews Initializers happening here

downAnimator = initializeAnimators()
}

func initializeAnimators() -> UIDynamicAnimator {
let downwardAnimator = UIDynamicAnimator(referenceView: self.view)

downwardAnimator.addBehavior(setBoundaries())
downwardAnimator.addBehavior(setGravity())
downwardAnimator.addBehavior(setBounciness())
downwardAnimator.delegate = self

return downwardAnimator
}

func setBoundaries() -> UICollisionBehavior {
let boundaries = UICollisionBehavior(items: fallingImageViews)
boundaries.collisionDelegate = self

// prevent collisions between items
boundaries.collisionMode = .boundaries

boundaries.setTranslatesReferenceBoundsIntoBoundary = true

return boundaries
}
}

// MARK: Collision Behavior Delegate
extension ViewController: UICollisionBehaviorDelegate, UIDynamicAnimatorDelegate {

func collisionBehavior(_ behavior: UICollisionBehavior, endedContactFor item: UIDynamicItem, withBoundaryIdentifier identifier: NSCopying?) {
print(identifier)
}
func collisionBehavior(_ behavior: UICollisionBehavior, beganContactFor item: UIDynamicItem, withBoundaryIdentifier identifier: NSCopying?, at p: CGPoint) {
print(identifier)
}
}

最佳答案

我完全废弃了旧答案并更新了它。很抱歉误导了您,但这是我修改后的答案:

这个想法是,您向想要动画的对象添加行为(在您的例子中,您的 fallingImageViews)。

因此,这里的所有代码实际上都应该进入一个继承自 UIImageView 的类(在我的示例代码中,您会看到我继承自 CardCtrl 对象,但它也可能是 UIImageView)。

您需要做的唯一更改是您的 UIDynamicAnimator 的引用 View 必须是 superview 并且所有动画行为的所有引用 View 都设置为[自己]

这是我的一个旧项目中的一些示例代码:

@IBDesignable class SlidingCard: CardCtrl, UICollisionBehaviorDelegate
{
...
//MARK: - Private Properties

private var gripHeightAnch: NSLayoutConstraint = NSLayoutConstraint()
private var animator: UIDynamicAnimator!
private var dynamicItem: UIDynamicItemBehavior!
private var collisionBnds: UICollisionBehavior!
private var snap: UISnapBehavior!
private var botBndryPt: CGFloat!
private var gravity: UIGravityBehavior!
private var pan: UIPanGestureRecognizer!

...

//MARK: - Delegate Methods

func collisionBehavior(_ behavior: UICollisionBehavior, beganContactFor item: UIDynamicItem,
withBoundaryIdentifier identifier: NSCopying?, at p: CGPoint)
{
guard let identifier = identifier else {return}
if String(describing: identifier) == "bot"
{
sendActions(for: .touchDragOutside)
}
else
{
sendActions(for: .touchDragInside)
}
}

...

//MARK: - Private Setup Methods

private func defineSlideBehavior()
{
if animator == nil
{
animator = UIDynamicAnimator(referenceView: superview!)
}
animator!.removeAllBehaviors()

//Check to see if behaviors are already installed because .removeAllBehaviors() doesn't
//always work
var addItem = true
var addBounds = true
var addGravity = true
for blarHar in animator!.behaviors
{
if blarHar.isKind(of: UIDynamicItem.self)
{
addItem = false
}
if blarHar.isKind(of: UICollisionBehavior.self)
{
addBounds = false
}
if blarHar.isKind(of: UIGravityBehavior.self)
{
addGravity = false
}
}
//Make it so the card doesn't wobble
if dynamicItem == nil && addItem
{
dynamicItem = UIDynamicItemBehavior(items: [self])
dynamicItem.allowsRotation = false
dynamicItem.elasticity = 0
}
animator!.addBehavior(dynamicItem)

//Add two boundaries for the drawer to collide with
if collisionBnds == nil && addBounds
{
collisionBnds = UICollisionBehavior(items: [self])
collisionBnds.collisionDelegate = self
}
botBndryPt = frame.maxY * 2 + 1.5
collisionBnds.removeAllBoundaries()
collisionBnds.addBoundary(withIdentifier: "top" as NSCopying,
from: CGPoint(x: frame.minX, y: frame.minY - 1.5),
to: CGPoint(x: frame.maxX, y: frame.minY - 1.5))
collisionBnds.addBoundary(withIdentifier: "bot" as NSCopying,
from: CGPoint(x: frame.minX, y: botBndryPt),
to: CGPoint(x: frame.maxX, y: botBndryPt))
animator!.addBehavior(collisionBnds)

//Define the initial gravity that affects the drawer
if addGravity
{
gravity = UIGravityBehavior(items: [self])
gravity.gravityDirection = CGVector(dx: 0, dy: -gravityStrength)
animator!.addBehavior(gravity)
}
}
}

关于ios - Swift - UICollisionBehavior 委托(delegate)方法未被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46437361/

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