UIKit动力学与视图交互效果详解
1. 碰撞行为与旋转效果
在处理视图的动态效果时,碰撞行为( UICollisionBehavior )扮演着重要角色。当碰撞发生时, UICollisionBehavior 会向其委托发送消息。我们可以利用这一点,为视图添加旋转效果。
func collisionBehavior(_ behavior: UICollisionBehavior,
beganContactFor item: UIDynamicItem,
withBoundaryIdentifier identifier: NSCopying?,
at p: CGPoint) {
// look for the dynamic item behavior
let b = self.anim.behaviors
if let bounce = (b.compactMap {$0 as? UIDynamicItemBehavior}).first {
let v = bounce.angularVelocity(for:item)
if v <= 6 {
bounce.addAngularVelocity(6, for:item)
}
}
}
上述代码中,当碰撞开始时,会查找动态项行为,并检查其角速度。如果角速度小于等于 6,则为该动态项添加 6 的角速
超级会员免费看
订阅专栏 解锁全文
9

被折叠的 条评论
为什么被折叠?



