【问题标题】:Get Visible rectangle of UIView added as a subview获取作为子视图添加的 UIView 的可见矩形
【发布时间】:2013-07-17 10:00:11
【问题描述】:

如上图所示,UIView A 和 UIView C 添加在 UIView B 上。对于 B,ClipToBounds 为 YES,因此红色区域不可见。

是否有可能得到 A & C 的可见矩形(用线条显示)

当我触摸例如 View A 时,我需要在可见区域显示 Rectangle。就是这样。

【问题讨论】:

  • 你到底想要什么??您想将该视图捕获为图像吗??
  • 不,只需要获取 View A 和 View B 的可见矩形。
  • 更新了我的问题。

标签: iphone ios


【解决方案1】:

你可以使用CGRectIntersection()方法得到两个矩形的交集矩形

CGRect intersectionRect = CGRectIntersection(viewA.frame, viewB.frame);
if(CGRectIsNull(intersectionRect)) {
   //Rects do not intersect
}

【讨论】:

    【解决方案2】:

    是的,您可以使用此功能:

    CGRect CGRectIntersection (
       CGRect r1,
       CGRect r2
    );
    

    如果你确切地告诉你想要做什么可能是更好的方法,例如我需要类似的东西,而不是手动裁剪,我只是将 UIView B 内容捕获为图像。

    【讨论】:

    • 我想如果您在其上使用触摸事件,您可以将视图 A 保持为 alpha 0。如果你在 B 上使用事件,你可以检查你是否用 CGRectContainsPoint 触摸了 A,并使用 hidden 属性来显示或隐藏 A。
    【解决方案3】:

    用于此

    [UIView convertRect:<#(CGRect)#> fromView:<#(UIView *)#>]
    [UIView convertRect:<#(CGRect)#> toView:<#(UIView *)#>]
    

    和CGRectIntersection函数

    【讨论】:

      【解决方案4】:

      touchesEnded: 方法上,您可以像下面这样找到它..

        -(void) touchesEnded:(NSSet *) touches {
          if(CGRectIntersectsRect([ViewA frame], [ViewB frame]) {
            //Do something here
          }
        }
      

      阅读更多关于 RectInterSect 的信息来自This Link

      希望对你有帮助...

      【讨论】:

      猜你喜欢
      • 2011-05-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-18
      • 1970-01-01
      • 1970-01-01
      • 2015-01-03
      相关资源
      最近更新 更多