2017-08-01 52 views
-1

我需要做一個條件檢查,看看我的一個imageView是否最終離開了屏幕。換句話說,我希望有人可以幫助我設置if語句來查看我的imageView在屏幕上的位置檢查UIImageView的位置

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

謝謝!

+0

如果(yourImageView.frame.maxX> self.view.frame。 size.width){} –

+0

我想我很好奇爲什麼你需要知道這一點。 (1)如果你需要一個滾動條,需要考慮一個'UIScrollView'? (2)如果您擔心在屏幕上放置圖像,請考慮使用正確的「contentMode」? (3)您是否面臨某種可以使用自動佈局輕鬆解決的佈局問題?事實是,(a)找出圖像視圖是否在屏幕上有很多*選擇,(b)最重要的是,你沒有向我們展示任何其他細節的代碼。 – dfd

回答

0

您可以檢查您的視圖的frame的兩個軸minmax值大於你的ViewController小號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") 
    } 
+0

嘿戴夫!謝謝您的幫助!這一切都是有道理的,我唯一的問題是第一個問題是當我衝入self.view時,我的Xcode給了我一個語法錯誤,所以我無法獲得整個視圖的寬度。你認爲這是因爲我在UIView的子類而不是UIViewController? – benjamin852

+0

是的,這就是爲什麼。你只需要將'superview'的名稱替換爲'self.view'。 –

+1

完美謝謝! – benjamin852

0
if (yourImageView.frame.maxX > self.view.frame.size.width){ 
     //will enter if image not all inside the screen 
    } 

請注意,您可以將self.view更改爲圖像所在的任何父級。

希望這會有所幫助!