【问题标题】:iOS CGRect is inside another CGRectiOS CGRect 在另一个 CGRect 里面
【发布时间】:2015-06-08 15:48:25
【问题描述】:

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

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

提前致谢

【问题讨论】:

  • 你有没有试过自己写这样的函数,也许?
  • 你指的是什么偏移量?重叠?在 x?在 y?两者都有?
  • 不,@holex,因为我期待这是一个人们让其他人的工作更轻松的社区。我将编写解决方案并将其发布给其他兴趣。祝你有美好的一天:)

标签: ios uiview cgrect


【解决方案1】:

斯威夫特 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 更新

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

【讨论】:

  • 我的意思是这个函数
  • 感谢您的回答,但是评论旁边的行似乎有错误 *// 它已经到了底部。需要offset.y = (self.origin.y + self.height) - (inRect.origin.y + inRect.height)
【解决方案2】:

也许

CGRectContainsRect(CGRect rect1, CGRect rect2)

要检查 cgrect 是否在另一个内部,此方法返回一个布尔值。

【讨论】:

  • 这只能回答一半的问题。
  • +1 对于像我这样需要知道这种方法存在的人:)。我需要布尔值,而不是重点。
【解决方案3】:

斯威夫特 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

【讨论】:

    【解决方案4】:

    这是 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;
        }
    

    【讨论】:

      【解决方案5】:

      真的吗?好的...在这里:

      + (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,如果它不在容器 rect 内,则给出偏移量,如果在容器内,则为 0,0”。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-09-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多