简体   繁体   English

组成员之间(而非组之间)的Swift Xcode冲突

[英]Swift Xcode Collisions between group members, but not groups

I am in a single view application, and I've set up the colliding behavior. 我在单视图应用程序中,并且设置了碰撞行为。 I have two different arrays or groups of UIViews. 我有两个不同的数组或UIViews组。 I want members of the same group to collide with one another, but members of differing groups to pass through one another. 我希望同一组的成员彼此碰撞,但不同组的成员彼此碰撞。 How can I do this? 我怎样才能做到这一点?

collisions 碰撞

If you simply have two arrays of views that you want to check collisions for, this should simply be a matter of testing all pairs between the two groups (or do you meant to find an optimized way of doing that?): 如果您只是有两个要检查冲突的视图数组,那么这应该只是测试两组之间的所有对(或者您是想找到一种优化的方式来做这件事?):

let collidingViews = group1.filter{ (view) in group2.contains{ $0.frame.intersects(view.frame) }} 

If you need to check collisions for a specific view that is part of the hierarchy, you could use the UIView tag as your group identifier and extend UIView to identify the collisions : 如果您需要检查属于层次结构的特定视图的冲突,则可以使用UIView标记作为组标识符,并扩展UIView来识别冲突:

extension UIView
{
    func collidesWith(_ other: UIView)
    {
       return other !== self 
           && tag == other.tag
           && frame.intersects(other.frame)
    }

    var collidingViews:[UIView]
    { return superview.subviews.filter{self.collidesWith($0)} }
}

You can then check for collisions between one view (eg the one being moved) and the others using this function: 然后,您可以使用此功能检查一个视图(例如,一个正在移动的视图)与另一个视图之间的冲突:

 for collidingView in view.collindingViews
 {
    // ... do something with the collindingView
 }

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

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