gpt4 book ai didi

ios - 我如何使圆形 View 与桨碰撞

转载 作者:搜寻专家 更新时间:2023-10-31 08:22:51 24 4
gpt4 key购买 nike

每当圆形 View 与 Storyboard上的桨碰撞时,它实际上与 View 的矩形框架发生碰撞。我该如何解决这个问题。我还尝试了 UIBezierPathCAShapeLayer,但似乎没有用。

红色箭头是碰撞点,因为它似乎并没有与球碰撞,而是与 View 的框架发生碰撞。一次,框架与桨接触,它会反弹回来等等。

screen snapshot

最佳答案

如果您正在使用 UIDynamicAnimator,您可以为将发生碰撞的项目定义 collisionBoundsTypecollisionBoundingPath,例如:

@IBDesignable public class BallView : UIView {

@IBInspectable public var fillColor: UIColor = UIColor.blackColor() {
didSet { setNeedsLayout() }
}

override public var collisionBoundsType: UIDynamicItemCollisionBoundsType {
return .Path
}

override public var collisionBoundingPath: UIBezierPath {
let radius = min(bounds.size.width, bounds.size.height) / 2.0

return UIBezierPath(arcCenter: CGPointZero, radius: radius, startAngle: 0, endAngle: CGFloat(M_PI * 2.0), clockwise: true)
}

private var shapeLayer: CAShapeLayer!

public override func layoutSubviews() {
if shapeLayer == nil {
shapeLayer = CAShapeLayer()
shapeLayer.strokeColor = UIColor.clearColor().CGColor
layer.addSublayer(shapeLayer)
}

let radius = min(bounds.size.width, bounds.size.height) / 2.0

shapeLayer.fillColor = fillColor.CGColor
shapeLayer.path = UIBezierPath(arcCenter: CGPoint(x: bounds.size.width / 2.0, y: bounds.size.height / 2.0), radius: radius, startAngle: 0, endAngle: CGFloat(M_PI * 2.0), clockwise: true).CGPath
}
}

@IBDesignable public class PaddleView : UIView {

@IBInspectable public var cornerRadius: CGFloat = 5 {
didSet { setNeedsLayout() }
}

@IBInspectable public var fillColor: UIColor = UIColor.blackColor() {
didSet { setNeedsLayout() }
}

override public var collisionBoundsType: UIDynamicItemCollisionBoundsType {
return .Path
}

override public var collisionBoundingPath: UIBezierPath {
return UIBezierPath(roundedRect: CGRect(x: -bounds.size.width / 2.0, y: -bounds.size.height / 2.0, width: bounds.width, height: bounds.height), cornerRadius: cornerRadius)
}

private var shapeLayer: CAShapeLayer!

public override func layoutSubviews() {
if shapeLayer == nil {
shapeLayer = CAShapeLayer()
shapeLayer.strokeColor = UIColor.clearColor().CGColor
layer.addSublayer(shapeLayer)
}

shapeLayer.fillColor = fillColor.CGColor
shapeLayer.path = UIBezierPath(roundedRect: CGRect(x: 0, y: 0, width: bounds.width, height: bounds.height), cornerRadius: cornerRadius).CGPath
}
}

如果您这样做,然后将这些项目添加到您的 UICollisionBehavior,它将遵循您为项目定义的碰撞边界。例如:

collision animation


上面说明了一个掠过的打击,就像在你的原始图像中一样。如果你希望它即使碰到 Racket 的边缘也能反弹,你可以为 UICollisionBehavior 指定 collisionDelegate 并给它一个新的方向:

let collision = UICollisionBehavior(items: [paddleView, ballView])
collision.translatesReferenceBoundsIntoBoundary = true
collision.collisionDelegate = self
self.animator.addBehavior(collision)

并符合UICollisionBehaviorDelegate,然后实现collisionBehavior(beganContactForItem:withItem:atPoint:):

func collisionBehavior(behavior: UICollisionBehavior, beganContactForItem item1: UIDynamicItem, withItem item2: UIDynamicItem, atPoint p: CGPoint) {
let postCollisionDirection = UIDynamicItemBehavior(items: [ballView])
postCollisionDirection.addLinearVelocity(CGPoint(x: ballView.center.x - paddleView.center.x, y: -200), forItem: ballView)
animator.addBehavior(postCollisionDirection)
}

这会产生类似的东西:

enter image description here

很明显,您必须多次尝试才能获得预期的效果,但它说明了检测碰撞并相应地向球添加线速度的基本思想。

关于ios - 我如何使圆形 View 与桨碰撞,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36110371/

24 4 0
文章推荐: java - 将对象旋转到面点
文章推荐: html - 如何在我的导航栏中将 'nav' 元素移动到 'navbar-brand' 下
文章推荐: html - 使用 CSS 检测