標頭檔案:
@inte***ce
uiview (uiscreendisplaying)
// 判斷view是否顯示在螢幕上
- (bool)isdisplayedinscreen;
@end
實現檔案:
@implementation
uiview (uiscreendisplaying)
// 判斷view是否顯示在螢幕上
- (bool)isdisplayedinscreen
cgrect screenrect = [uiscreen mainscreen].bounds;
// 轉換view對應window的rect
cgrect rect = [self convertrect:self
.frame fromview:nil];
if (cgrectisempty(rect) || cgrectisnull(rect))
// 若view 隱藏
if (self
.hidden)
// 若沒有superview
if (self
.superview == nil)
// 若size為cgrectzero
if (cgsizeequaltosize(rect.size, cgsizezero))
// 獲取 該view與window 交叉的 rect
cgrect intersectionrect = cgrectintersection(rect, screenrect);
if (cgrectisempty(intersectionrect) || cgrectisnull(intersectionrect))
return
true;
}
測試用例:
uiview *v = [[uiview alloc] initwithframe:cgrectmake(0, 0, 40, 40)];
// 無父檢視
bool b1 = [v isdisplayedinscreen];
nslog(@"b1: %d", b1);
//[self
.view addsubview:v];
bool b2 = [v isdisplayedinscreen];
nslog(@"b2: %d", b2);
v.frame = cgrectzero;
bool b3 = [v isdisplayedinscreen];
nslog(@"b3: %d", b3);
cgrect screenrect = [uiscreen mainscreen].bounds;
cgfloat screenwidth = screenrect.size
.width;
cgfloat screenheight = screenrect.size
.height;
v.frame = cgrectmake(-screenwidth, -screenheight, screenwidth, screenheight);
bool b4 = [v isdisplayedinscreen];
nslog(@"b4: %d", b4);
判斷元素是否在螢幕中
此方法返回乙個getclientrect集合,包含元素距離dom可見視口top,left,bottom,right屬性及width和height屬性。注意這裡的bottom指的是元素最下面距離視口頂部的距離。function isscrolly function isscrollx function...
unity 判斷物體是否在螢幕內
方法一 使用unity系統函式 onbecamevisible 和 onbecameinvisible 物體在螢幕內 void onbecamevisible 物體在螢幕外 void onbecameinvisible 注意 物體只要有一部分在螢幕內onbecamevisible都會執行,只有物體完...
Fragment 判斷是否顯示當前Fragment
fragment雖然有onresume和onpause的,但是這兩個方法是activity的方法,呼叫時機也是與activity相同,和viewpager搭配使用這個方法就很雞肋了,根本不是你想要的效果,這裡介紹一種方法。在fragment顯示出來呼叫的方法 public void setuserv...