gpt4 book ai didi

iOS CGRect 在另一个 CGRect 里面

转载 作者:可可西里 更新时间:2023-11-01 04:07:48 26 4
gpt4 key购买 nike

有人对以下任务有好的解决方案吗?

我需要检查一个 CGRect 是否在另一个 CGRect 内部并返回一个 CGPoint,如果矩形窗口在包含的任何维度之外,它会给出偏移量。

提前致谢

最佳答案

Swift 4.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 inRect.contains(rect) {
return CGPoint(x: 0, y: 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
}

Swift 3 update

// 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
}

关于iOS CGRect 在另一个 CGRect 里面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30713693/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com