UIDynamicItem with non-rectangular bounds

2019-08-28 02:12发布

So I'm looking into UIKit Dynamics and the one problem I have run into is if I want to create a UIView with a custom drawRect: (for instance let's say I want to draw a triangle), there seems to be no way to specify the path of the UIView (or rather the UIDynamicItem) to be used for a UICollisionBehavior.

My goal really is to have polygons on the screen that collide with one another exactly how one would expect.

I came up with a solution of stitching multiple views together but this seems like overkill for what I want.

Is there some easy way to do this, or do I really have to stitch views together?

Dan

2条回答
The star\"
2楼-- · 2019-08-28 02:24

Watch the WWDC 2013 videos on this topic. They are very clear: for the sake of efficiency and speed, only the (rectangular) bounds of the view matter during collisions.

EDIT In iOS 9, a dynamic item can have a customized collision boundary. You can have a rectangle dictated by the frame, an ellipse dictated by the frame, or a custom shape — a convex counterclockwise simple closed UIBezierPath. The relevant properties, collisionBoundsType and (for a custom shape) collisionBoundingPath, are read-only, so you will have to subclass in order to set them.

查看更多
\"骚年 ilove
3楼-- · 2019-08-28 02:34

If you really want to collide polygons, you might consider SpriteKit and its physics engine (it seems to share a lot in common with UIDynamics). It can mix with UIKit, although maybe not as smoothly as you'd like.

查看更多
登录 后发表回答