本文主要是介绍iOS view是否在此时是否屏幕内,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
判断view是否在屏幕内,这种实际情况也经常遇到,可以使用定时器,定时检查
- (void)startTaskWithView:(UIView *)view {_view = view;if (_view) {_timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(beginCheck) userInfo:nil repeats:YES];[_timer fire];}
}
- (void)beginCheck {if([self isDisplayedInScreen]) {NSLog(@"在屏幕内");[_timer invalidate];_timer = nil;} else {NSLog(@"不在屏幕内");}
}// 判断View是否显示在屏幕上
- (BOOL)isDisplayedInScreen
{if (_view == nil) {return NO;}CGRect screenRect = [UIScreen mainScreen].bounds;// 转换view对应window的RectUIWindow *win= [UIApplication sharedApplication].keyWindow;CGRect rect = [_view convertRect:_view.frame toView:win];if (CGRectIsEmpty(rect) || CGRectIsNull(rect)) {return NO;}// 若view 隐藏if (_view.hidden) {return NO;}// 若没有superviewif (_view.superview == nil) {return NO;}// 若size为CGrectZeroif (CGSizeEqualToSize(rect.size, CGSizeZero)) {return NO;}// 获取 该view与window 交叉的 RectCGRect intersectionRect = CGRectIntersection(rect, screenRect);if (CGRectIsEmpty(intersectionRect) || CGRectIsNull(intersectionRect)) {return NO;}return YES;
}
这篇关于iOS view是否在此时是否屏幕内的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!