- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在做一个简单的动画,它需要我处理一些与边界的碰撞。
我有一个类 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/
我正在尝试使用UIKit Dynamics的iOS iPhone应用程序进行物理动画处理。这是一场篮球比赛,您可以将球扔进篮筐。但是,我不知道如何使球变圆(UICollisionBehavior和UI
我的应用程序有一个自定义的 UIButton,它有两个 subview ,它们看起来像是被绳子挂起来了。当用户点击按钮时,一个会被拉回,然后与另一个发生碰撞。这在第一次碰撞时对我有用,但是当我第二次点
如果我定义一个开放的 UIBezierPath 并将其设置为碰撞边界: _containerPath = [UIBezierPath bezierPathWithArcCenter:center
我在导航 Controller 中有一个 View 。 然后我向该 View 添加一个 subview 并偏移其原点高度,使其仅覆盖屏幕的一半(另一半溢出屏幕底部)。然后我希望能够向上拖动此 View
我正在尝试弄清楚如何使用 UIKit Dynamics 成功地碰撞两个具有自定义边界形状的 UIView。 我能想到的最基本的例子来解释我的问题是让两个圆碰撞(考虑到它们的圆角)而不是它们的方形边界。
作为实现 UICollisionBehaviour 的一部分,我为屏幕边缘设置了边界。然后我添加了一些 View ,最后将 UIPanGestureRecognizer 附加到其中一个。 现在我可以使
在我的应用程序中,我目前正在绘制一个基本 View ,然后我尝试使用 CAShapeLayer 将它的角变圆,但 UICollisionBehavior 仍然像绘制的正方形一样使用react。 这是我
我想使用 UIKit Dynamics 组合 UIAttachmentBehavior 和 UICollisionBehavior,以便用户可以拖动 View (使用 UIPanGestureReco
我有一个 View ,其边界设置为碰撞( setTranslatesReferenceBoundsIntoBoundaryWithInsets )和一个带有重力的 subview 设置,以便它可以与
我正在做一个简单的动画,它需要我处理一些与边界的碰撞。 我有一个类 viewcontroller,我将其扩展为 UICollisionBehaviorDelegate,以便我可以识别和处理 View
我最近尝试使用 UIKitDynamics 创建一个类似乒乓球的小游戏。我成功地构建了核心游戏。现在我想添加一些额外的机制。 例如,一种行为会减小 Racket 的大小。我添加了一个执行此类操作的 N
是否可以更改 UIView 的边界(它使用 UIAttachmentBehaviors 附加到其他一些 UIView)并让 UICollisionBehavior 结合 UIAttachmentBeh
我目前正在为一个播放 connect 4 的框架设计一个 UI。这个框架封装在一个名为 GameSession 的类中。我不会描述它的 API 的来龙去脉以及它是如何运作的。我不相信这很重要。 我相信
我正在尝试在 UIKit Dynamics 中使用 UICollisionBehavior 来确定我何时将 View 抛出屏幕(使用 UIAttachmentBehavior 和 UIPushBeha
在我的应用程序中,我使用包含 UICollisionBehavior 的 UIKit Dynamics 使菜单在打开和关闭时弹跳。我为此使用的代码如下。这在 iOS8 上运行良好。然而,对于 iOS9
我是一名优秀的程序员,十分优秀!