【问题标题】:Check Location Of UIImageView检查 UIImageView 的位置
【发布时间】:2018-01-08 12:21:04
【问题描述】:

我需要进行条件检查,以检查我的 imageView 是否最终离开屏幕。换句话说,我希望有人可以帮助我设置一个 if 语句来查看我的 imageView 在屏幕上的位置

if previewImageView......... (something here)

谢谢!

【问题讨论】:

  • if (yourImageView.frame.maxX > self.view.frame.size.width){}
  • 我想我很好奇你为什么需要知道这个。 (1) 如果需要时需要滚动条,可以考虑UIScrollView? (2) 如果您担心图像适合屏幕,请考虑使用正确的contentMode? (3) 您是否面临某种可以使用自动布局轻松解决的布局问题?事实是,有 许多 替代方法来 (a) 找出图像视图是否在屏幕上,以及 (b) 最重要的是,您没有向我们展示任何其他细节的代码。

标签: ios iphone swift uiimageview cgpoint


【解决方案1】:
    if (yourImageView.frame.maxX > self.view.frame.size.width){
       //will enter if image not all inside the screen 
    }

请注意,您可以将 self.view 更改为图像所在的任何父级。

希望这会有所帮助!

【讨论】:

    【解决方案2】:

    您可以检查两个轴上视图的frameminmax 值是否大于/小于ViewControllers view 的相同属性。

    if imageView.frame.maxX > self.view.frame.size.width {
        print("Image goes out of screen on the right")
    } else if imageView.frame.minX < 0 {
        print("Image goes out of screen on the left")
    } else if imageView.frame.maxY > self.view.frame.size.height {
        print("Image goes out of screen on the top")
    } else if imageView.frame.minY < 0 {
        print("Image goes out of screen at the bottom")
    }
    

    如果您不在乎它从哪个方向出视图,您可以将所有语句合并为一个:

    if imageView.frame.maxX > self.view.frame.size.width || imageView.frame.minX < 0 || imageView.frame.maxY > self.view.frame.size.height || imageView.frame.minY < 0 {
            print("Image out of bounds")
        }
    

    【讨论】:

    • 嘿戴夫!谢谢您的帮助!这一切都说得通,我唯一的问题是第一个问题是我的 Xcode 在我输入 self.view 时给了我一个语法错误,所以我无法获得整个视图的宽度。你认为这是因为我在 UIView 的子类而不是 UIViewController 中工作吗?
    • 是的,这就是原因。您只需插入 superview 的名称代替 self.view
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-10-31
    • 1970-01-01
    • 1970-01-01
    • 2014-10-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多