[英]Swift Xcode Collisions between group members, but not groups
如果您只是有兩個要檢查沖突的視圖數組,那么這應該只是測試兩組之間的所有對(或者您是想找到一種優化的方式來做這件事?):
let collidingViews = group1.filter{ (view) in group2.contains{ $0.frame.intersects(view.frame) }}
如果您需要檢查屬於層次結構的特定視圖的沖突,則可以使用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)} }
}
然后,您可以使用此功能檢查一個視圖(例如,一個正在移動的視圖)與另一個視圖之間的沖突:
for collidingView in view.collindingViews
{
// ... do something with the collindingView
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.