我正在做一个简单的动画,需要我处理一些与边界的碰撞。
我有一个类viewcontroller,我将其扩展为UICollisionBehaviorDelegate,以便识别和处理视图冲突。
出于某种原因,当碰撞发生时,我的委托方法永远不会触发。
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)
}
}
答案 0 :(得分:0)
我完全废弃了我的旧答案并对其进行了更新。对不起,我误导了你,但这是我修改过的答案:
您的想法是向要动画的对象添加行为(在您的情况下,您的fallingImageViews
)。
所以这里的所有代码实际上应该进入一个继承自UIImageView的类(在我的示例代码中,您将看到我继承自CardCtrl对象,但它也可能是UIImageView)。
您必须做的唯一更改是UIDynamicAnimator
的参考视图必须为superview
,并且所有动画行为的所有参考视图都设置为[self]
。
以下是我旧项目中的一些示例代码:
@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)
}
}
}
答案 1 :(得分:0)
尝试将let downAnimator = initializeAnimators()
中的行viewDidLoad()
更改为downAnimator = initializeAnimators()