iOS CGRect 在另一个 CGRect 中
iOS CGRect is inside another CGRect
有人对以下任务有好的解决方案吗?
我需要检查一个 CGRect 是否在另一个 CGRect 和 return 一个 CGPoint 中,如果 rect window 在包含的任何维度之外,它给出偏移量。
提前致谢
可能
CGRectContainsRect(CGRect rect1, CGRect rect2)
检查 cgrect 是否在另一个里面,这个方法 return a bool.
真的吗?好的...这是:
+ (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;
}
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
}
这是为 Xamarin 用户接受的答案的 C# 翻译:
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;
}
Swift 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
有人对以下任务有好的解决方案吗?
我需要检查一个 CGRect 是否在另一个 CGRect 和 return 一个 CGPoint 中,如果 rect window 在包含的任何维度之外,它给出偏移量。
提前致谢
可能
CGRectContainsRect(CGRect rect1, CGRect rect2)
检查 cgrect 是否在另一个里面,这个方法 return a bool.
真的吗?好的...这是:
+ (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;
}
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
}
这是为 Xamarin 用户接受的答案的 C# 翻译:
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;
}
Swift 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