// // UIView+RQExtension.m // RQCommon // // Created by 张嵘 on 2018/11/16. // Copyright © 2018 张嵘. All rights reserved. // #import "UIView+RQExtension.h" #import static char kActionHandlerTapBlockKey; static char kActionHandlerTapGestureKey; static char kActionHandlerLongPressBlockKey; static char kActionHandlerLongPressGestureKey; @implementation UIView (RQExtension) /** * 判断一个控件是否真正显示在主窗口 */ - (BOOL)rq_isShowingOnKeyWindow { // 主窗口 UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow; // 以主窗口左上角为坐标原点, 计算self的矩形框 CGRect newFrame = [keyWindow convertRect:self.frame fromView:self.superview]; CGRect winBounds = keyWindow.bounds; // 主窗口的bounds 和 self的矩形框 是否有重叠 BOOL intersects = CGRectIntersectsRect(newFrame, winBounds); return !self.isHidden && self.alpha > 0.01 && self.window == keyWindow && intersects; } /** * xib创建的view */ + (instancetype)rq_viewFromXib { return [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass(self) owner:nil options:nil] lastObject]; } + (instancetype)rq_viewFromXibWithFrame:(CGRect)frame { UIView *view = [self rq_viewFromXib]; view.frame = frame; return view; } - (void)setRq_x:(CGFloat)rq_x { CGRect frame = self.frame; frame.origin.x = rq_x; self.frame = frame; } - (CGFloat)rq_x { return self.frame.origin.x; } - (void)setRq_y:(CGFloat)rq_y { CGRect frame = self.frame; frame.origin.y = rq_y; self.frame = frame; } - (CGFloat)rq_y { return self.frame.origin.y; } - (void)setRq_centerX:(CGFloat)rq_centerX { CGPoint center = self.center; center.x = rq_centerX; self.center = center; } - (CGFloat)rq_centerX { return self.center.x; } - (void)setRq_centerY:(CGFloat)rq_centerY { CGPoint center = self.center; center.y = rq_centerY; self.center = center; } - (CGFloat)rq_centerY { return self.center.y; } - (void)setRq_width:(CGFloat)rq_width { CGRect frame = self.frame; frame.size.width = rq_width; self.frame = frame; } - (CGFloat)rq_width { return self.frame.size.width; } - (void)setRq_height:(CGFloat)rq_height { CGRect frame = self.frame; frame.size.height = rq_height; self.frame = frame; } - (CGFloat)rq_height { return self.frame.size.height; } - (void)setRq_size:(CGSize)rq_size { CGRect frame = self.frame; frame.size = rq_size; self.frame = frame; } - (CGSize)rq_size { return self.frame.size; } - (void)setRq_origin:(CGPoint)rq_origin { CGRect frame = self.frame; frame.origin = rq_origin; self.frame = frame; } - (CGPoint)rq_origin { return self.frame.origin; } - (void)setRq_top:(CGFloat)rq_top { CGRect frame = self.frame; frame.origin.y = rq_top; self.frame = frame; } - (CGFloat)rq_top { return self.frame.origin.y; } - (void)setRq_left:(CGFloat)rq_left { CGRect frame = self.frame; frame.origin.x = rq_left; self.frame = frame; } - (CGFloat)rq_left{ return self.frame.origin.x; } - (void)setRq_bottom:(CGFloat)rq_bottom{ CGRect frame = self.frame; frame.origin.y = rq_bottom - frame.size.height; self.frame = frame; } - (CGFloat)rq_bottom { return self.frame.origin.y + self.frame.size.height; } - (void)setRq_right:(CGFloat)rq_right { CGRect frame = self.frame; frame.origin.x = rq_right - frame.size.width; self.frame = frame; } - (CGFloat)rq_right { return self.frame.origin.x + self.frame.size.width; } - (void)addTapActionWithBlock:(GestureActionBlock)block { UITapGestureRecognizer *gesture = objc_getAssociatedObject(self, &kActionHandlerTapGestureKey); if (!gesture) { gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleActionForTapGesture:)]; [self addGestureRecognizer:gesture]; objc_setAssociatedObject(self, &kActionHandlerTapGestureKey, gesture, OBJC_ASSOCIATION_RETAIN); } objc_setAssociatedObject(self, &kActionHandlerTapBlockKey, block, OBJC_ASSOCIATION_COPY); } - (void)handleActionForTapGesture:(UITapGestureRecognizer*)gesture { if (gesture.state == UIGestureRecognizerStateRecognized) { GestureActionBlock block = objc_getAssociatedObject(self, &kActionHandlerTapBlockKey); if (block) { block(gesture); } } } - (void)addLongPressActionWithBlock:(GestureActionBlock)block { UILongPressGestureRecognizer *gesture = objc_getAssociatedObject(self, &kActionHandlerLongPressGestureKey); if (!gesture) { gesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleActionForLongPressGesture:)]; [self addGestureRecognizer:gesture]; objc_setAssociatedObject(self, &kActionHandlerLongPressGestureKey, gesture, OBJC_ASSOCIATION_RETAIN); } objc_setAssociatedObject(self, &kActionHandlerLongPressBlockKey, block, OBJC_ASSOCIATION_COPY); } - (void)handleActionForLongPressGesture:(UITapGestureRecognizer*)gesture { if (gesture.state == UIGestureRecognizerStateRecognized) { GestureActionBlock block = objc_getAssociatedObject(self, &kActionHandlerLongPressBlockKey); if (block) { block(gesture); } } } @end