1

I am trying to work on an iOS iPhone app that uses UIKit Dynamics in order to do the physics animation. It's a basketball game where you can throw the ball into the hoop. However, I can't figure out how to make the ball circular (treated this way by the UICollisionBehavior and UIDynamicAnimator)?

I've tried: ball.layer.cornerRadius = ball.frame.size.width / 2.0;

But this only draws the ball circular. It doesn't actually behave this way. I also know I can draw boundaries using: addBoundaryWithIdentifier:forPath:

But I can't figure out how to put bounds around a UIView (UIImageView in my case) that aren't just a rectangle? I know I could do this using SpriteKit, but I've already written a few hours of code for the basketball game using UIKit Dynamics and don't want to switch over if there is a simple fix. Thank you!

Wyetro
  • 8,439
  • 9
  • 46
  • 64
Kjell
  • 801
  • 7
  • 19
  • UIKit Dynamics doesn't do arbitrary shapes -- as you've noted, Sprite Kit is the API you should use for game animations. – rickster Nov 05 '13 at 18:45

2 Answers2

3

In iOS 9 you can now do exactly what you want.

In your UIView subclass you can add this:

- (UIDynamicItemCollisionBoundsType)collisionBoundsType
{
    return UIDynamicItemCollisionBoundsTypeEllipse;
}
Wyetro
  • 8,439
  • 9
  • 46
  • 64
2

You cannot do it using UIKit Dynamics. UIDynamicItem protocol implementation requires the object that implements the protocol to provide a bounds property which is a CGRect.

You have to go for SpriteKit.

Rafał Sroka
  • 39,540
  • 23
  • 113
  • 143