UICollisionBehavior: Rolling Ball?

858 views Asked by At

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!

2

There are 2 answers

0
Wyetro On

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

In your UIView subclass you can add this:

- (UIDynamicItemCollisionBoundsType)collisionBoundsType
{
    return UIDynamicItemCollisionBoundsTypeEllipse;
}
0
Rafał Sroka On

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.