简体   繁体   English

动态创建的UIView在碰撞后不会动画

[英]Dynamically Created UIView Will Not Animate After Collision

thanks for reading this post. 感谢您阅读这篇文章。

Here is a summary of my program: 这是我的程序的摘要:

  1. I dynamically add a couple UIViews as subviews. 我动态地将几个UIViews添加为子视图。

  2. I have objects that move around and collide 我的物体会四处移动并发生碰撞

  3. When a collision happens I want one of the created UIViews to fade out and then I want to remove one of the collided UIViews. 发生碰撞时,我希望其中一个创建的UIView淡出,然后我要删除其中一个碰撞的UIView。

I am able to remove the created UIView with the following: dynamicAnimator?.referenceView?.viewWithTag(tagOfView)?.removeFromSuperview() 我可以使用以下dynamicAnimator?.referenceView?.viewWithTag(tagOfView)?.removeFromSuperview()删除创建的UIView: dynamicAnimator?.referenceView?.viewWithTag(tagOfView)?.removeFromSuperview()

However, when I try to add an animation to fade out the view, I am not getting any type of fade/animation. 但是,当我尝试添加动画以淡化视图时,没有得到任何类型的淡入/动画。 What am I doing incorrectly? 我做错了什么?

UIView.animateWithDuration(1.5, animations: {
    self.dynamicAnimator?.referenceView?.viewWithTag(tagOfView)?.alpha = 0.0 
})

In case needed, here is the entire collisionBehavior() function. 在需要的情况下,这里是整个collisionBehavior()函数。

func collisionBehavior(behavior: UICollisionBehavior, beganContactForItem item: UIDynamicItem, withBoundaryIdentifier identifier: NSCopying, atPoint p: CGPoint) {
    let identOpt : NSCopying? = identifier

    if let ident = identOpt as? NSNumber {
        switch ident {
        case Identifiers.paddleAtStartup:
            break
        default:
            ballCollider.removeBoundaryWithIdentifier(ident)
            UIView.animateWithDuration(1.5, animations: {
                self.dynamicAnimator?.referenceView?.viewWithTag(Int(ident))?.alpha = 0.0
                })
            dynamicAnimator?.referenceView?.viewWithTag(Int(ident))?.removeFromSuperview()       
        }
    }
}

You remove view immediately after collision, and there's nothing to animate. 碰撞后立即删除视图,没有任何动画。 You better remove it in animation's completion handler. 您最好在动画的完成处理程序中将其删除。

Here is the corrected code for anyone with the same problem: 以下是针对存在相同问题的任何人的更正代码:

 func collisionBehavior(behavior: UICollisionBehavior, beganContactForItem item: UIDynamicItem, withBoundaryIdentifier identifier: NSCopying, atPoint p: CGPoint) {
    let identOpt : NSCopying? = identifier

    if let ident = identOpt as? NSNumber {
        switch ident {
        case Identifiers.paddleAtStartup:
            break
        default:
            UIView.animateWithDuration(0.1, delay: 0, options: UIViewAnimationOptions.CurveEaseInOut, animations: { ()
                self.dynamicAnimator?.referenceView?.viewWithTag(Int(ident))?.alpha = 0.0
                }, completion: {
                    if $0 {
                        self.dynamicAnimator?.referenceView?.viewWithTag(Int(ident))?.removeFromSuperview()
                        self.ballCollider.removeBoundaryWithIdentifier(ident)
                    }
            })
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM