简体   繁体   English

具有非矩形范围的UIDynamicItem

[英]UIDynamicItem with non-rectangular bounds

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. 因此,我正在研究UIKit Dynamics,遇到的一个问题是如果我想使用自定义的drawRect:创建UIView drawRect:例如,假设我要绘制一个三角形),似乎没有办法指定用于UICollisionBehavior的UIView(或UIDynamicItem)的路径。

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

Watch the WWDC 2013 videos on this topic. 观看有关此主题的WWDC 2013视频。 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. 编辑在iOS 9中,动态项目可以具有自定义的碰撞边界。 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. 您可以具有由框架决定的矩形,由框架决定的椭圆或自定义形状-逆时针凸的简单封闭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. 相关属性collisionBoundsType和(对于自定义形状) collisionBoundingPath是只读的,因此必须对其进行子类化才能设置它们。

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). 如果您真的想碰撞多边形,可以考虑使用SpriteKit及其物理引擎(它似乎与UIDynamics有很多共同点)。 It can mix with UIKit, although maybe not as smoothly as you'd like. 它可以与UIKit混合使用,尽管可能不如您所愿地平滑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM