任何人都可以很好地解決以下任務嗎?iOS CGRect在另一個CGRect
我需要檢查一個CGRect是否在另一個CGRect內,並返回一個CGPoint,如果rect窗口位於包含的任何維度之外,則會給出偏移量。
在此先感謝
任何人都可以很好地解決以下任務嗎?iOS CGRect在另一個CGRect
我需要檢查一個CGRect是否在另一個CGRect內,並返回一個CGPoint,如果rect窗口位於包含的任何維度之外,則會給出偏移量。
在此先感謝
斯威夫特3更新
// Returns how much is the rect outside of the view, 0 if inside
func isRectVisibleInView(rect: CGRect, inRect: CGRect) -> CGPoint {
var offset = CGPoint()
if CGRectContainsRect(inRect, rect) {
return CGPointMake(0, 0)
}
if rect.origin.x < inRect.origin.x {
// It's out to the left
offset.x = inRect.origin.x - rect.origin.x
} else if (rect.origin.x + rect.width) > (inRect.origin.x + inRect.width) {
// It's out to the right
offset.x = (rect.origin.x + rect.width) - (inRect.origin.x + inRect.width)
}
if rect.origin.y < inRect.origin.y {
// It's out to the top
offset.y = inRect.origin.y - rect.origin.y
} else if rect.origin.y + rect.height > inRect.origin.y + inRect.height {
// It's out to the bottom
offset.y = (rect.origin.y + rect.height) - inRect.origin.y + inRect.height
}
return offset
}
雨燕2.1
// Returns how much is the rect outside of the view, 0 if inside
func isRectVisibleInView(rect: CGRect, inRect: CGRect) -> CGPoint {
var offset = CGPoint()
if CGRectContainsRect(inRect, rect) {
return CGPointMake(0, 0)
}
if rect.origin.x < inRect.origin.x {
// It's out to the left
offset.x = inRect.origin.x - rect.origin.x
} else if (rect.origin.x + rect.width) > (inRect.origin.x + inRect.width) {
// It's out to the right
offset.x = (rect.origin.x + rect.width) - (inRect.origin.x + inRect.width)
}
if rect.origin.y < inRect.origin.y {
// It's out to the top
offset.y = inRect.origin.y - rect.origin.y
} else if rect.origin.y + rect.height > inRect.origin.y + inRect.height {
// It's out to the bottom
offset.y = (rect.origin.y + rect.height) - inRect.origin.y + inRect.height
}
return offset
}
我的意思是這個功能 –
感謝您的回答,但它似乎有bug旁邊的評論*//它已經出現在底部 。需要offset.y =(self.origin.y + self.height) - (inRect.origin.y + inRect.height) –
真的嗎? OK ......這就是:
+ (CGPoint) isRect:(CGRect)inRect1 inRect:(CGRect)inRect2
{
CGPoint offsetPoint = {0, 0};
if (!CGRectContainsRect(inRect1, inRect2)) {
offsetPoint.x = inRect1.origin.x - inRect2.origin.x;
offsetPoint.y = inRect1.origin.y - inRect2.origin.y;
}
return offsetPoint;
}
這只是檢查原點,不計算高度,寬度。我會很快發佈我的解決方案。 –
他沒有要求:「返回一個CGPoint,如果它不在容器矩形內,則返回偏移量,如果它在內部,則返回0,0。」 – cbiggin
這裏是公認的答案的C#編譯爲Xamarin民間:
CGPoint isRectVisibleInView(CGRect rect, CGRect inRect) {
var offset = new CGPoint();
if (inRect.Contains(rect)) {
return new CGPoint (0, 0);
}
if (rect.X < inRect.X) {
// It's out to the left
offset.X = inRect.X - rect.X;
} else if (rect.GetMaxX() > inRect.GetMaxX()) {
// It's out to the right
offset.X = rect.GetMaxX() - inRect.GetMaxX();
}
if (rect.Y < inRect.Y) {
// It's out to the top
offset.Y = inRect.Y - rect.Y;
} else if (rect.GetMaxY() > inRect.GetMaxY()) {
// It's out to the bottom
offset.Y = rect.GetMaxY() - inRect.GetMaxY();
}
return offset;
}
斯威夫特4
簡單使用let boolValue = tableView.bounds.contains(cellRect)
// CoreGraphics在基於模塊
@available(iOS 2.0, *)
public func contains(_ point: CGPoint) -> Bool
@available(iOS 2.0, *)
public func contains(_ rect2: CGRect) -> Bool
你試圖寫這樣的功能爲你自己,也許? – holex
你指的是什麼抵消?重疊?在x?在y?同時? – luk2302
沒有@holex,因爲我期待這是一個讓人們更容易工作的社區。我會寫解決方案並將其發佈給其他用戶。祝你有美好的一天:) –