// // UIView+RQExtension.m // RQCommon // // Created by 张嵘 on 2018/11/16. // Copyright © 2018 张嵘. All rights reserved. // #import "UIView+RQExtension.h" #import static char kDTActionHandlerTapBlockKey; static char kDTActionHandlerTapGestureKey; static char kDTActionHandlerLongPressBlockKey; static char kDTActionHandlerLongPressGestureKey; @implementation UIView (RQExtension) #pragma mark - Gesture - (void)setTapActionWithBlock:(void (^)(UITapGestureRecognizer*tap))block { self.userInteractionEnabled = YES; UITapGestureRecognizer *gesture = objc_getAssociatedObject(self, &kDTActionHandlerTapGestureKey); if (!gesture) { gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(__handleActionForTapGesture:)]; [self addGestureRecognizer:gesture]; objc_setAssociatedObject(self, &kDTActionHandlerTapGestureKey, gesture, OBJC_ASSOCIATION_RETAIN); } objc_setAssociatedObject(self, &kDTActionHandlerTapBlockKey, block, OBJC_ASSOCIATION_COPY); } - (void)__handleActionForTapGesture:(UITapGestureRecognizer *)gesture { if (gesture.state == UIGestureRecognizerStateRecognized) { void(^action)(UITapGestureRecognizer*tap) = objc_getAssociatedObject(self, &kDTActionHandlerTapBlockKey); if (action) { action(gesture); } } } - (void)setLongPressActionWithBlock:(void (^)(UILongPressGestureRecognizer*longP))block { self.userInteractionEnabled = YES; UILongPressGestureRecognizer *gesture = objc_getAssociatedObject(self, &kDTActionHandlerLongPressGestureKey); if (!gesture) { gesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(__handleActionForLongPressGesture:)]; [self addGestureRecognizer:gesture]; objc_setAssociatedObject(self, &kDTActionHandlerLongPressGestureKey, gesture, OBJC_ASSOCIATION_RETAIN); } objc_setAssociatedObject(self, &kDTActionHandlerLongPressBlockKey, block, OBJC_ASSOCIATION_COPY); } - (void)__handleActionForLongPressGesture:(UITapGestureRecognizer *)gesture { if (gesture.state == UIGestureRecognizerStateBegan) { void(^action)(UILongPressGestureRecognizer*longP) = objc_getAssociatedObject(self, &kDTActionHandlerLongPressBlockKey); if (action) { action((UILongPressGestureRecognizer*)gesture); } } } #pragma mark - Other /** * 判断一个控件是否真正显示在主窗口 */ - (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; } /** * xib中显示的属性 */ - (void)setBorderColor:(UIColor *)borderColor { [self.layer setBorderColor:borderColor.CGColor]; } - (void)setBorderWidth:(CGFloat)borderWidth { [self.layer setBorderWidth:borderWidth]; } - (void)setCornerRadius:(CGFloat)cornerRadius { [self.layer setCornerRadius:cornerRadius]; } - (void)setMasksToBounds:(BOOL)masksToBounds { [self.layer setMasksToBounds:masksToBounds]; } - (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)setX:(CGFloat)x { CGRect frame = self.frame; frame.origin.x = x; self.frame = frame; } - (CGFloat)x { return self.frame.origin.x; } - (void)setY:(CGFloat)y { CGRect frame = self.frame; frame.origin.y = y; self.frame = frame; } - (CGFloat)y { return self.frame.origin.y; } - (void)setOrigin:(CGPoint)origin { CGRect frame = self.frame; frame.origin = origin; self.frame = frame; } - (CGPoint)origin { return self.frame.origin; } - (void)setWidth:(CGFloat)width { CGRect frame = self.frame; frame.size.width = width; self.frame = frame; } - (CGFloat)width { return self.frame.size.width; } - (void)setHeight:(CGFloat)height { CGRect frame = self.frame; frame.size.height = height; self.frame = frame; } - (CGFloat)height { return self.frame.size.height; } - (void)setSize:(CGSize)size { CGRect frame = self.frame; frame.size = size; self.frame = frame; } - (CGSize)size { return self.frame.size; } - (void)setBottom:(CGFloat)bottom { CGRect frame = self.frame; frame.origin.y = bottom - frame.size.height; self.frame = frame; } - (CGFloat)bottom { return CGRectGetMaxY(self.frame); } - (CGFloat)tail { return CGRectGetMaxX(self.frame); } - (void)setTail:(CGFloat)tail { CGRect frame = self.frame; frame.origin.x = tail - frame.size.width; self.frame = frame; } - (void)setMiddleX:(CGFloat)middleX { CGRect frame = self.frame; frame.origin.x = middleX - frame.size.width / 2; self.frame = frame; } - (CGFloat)middleX { return CGRectGetMidX(self.frame); } - (void)setMiddleY:(CGFloat)middleY { CGRect frame = self.frame; frame.origin.y = middleY - frame.size.height / 2 ; self.frame = frame; } - (CGFloat)middleY { return CGRectGetMidY(self.frame); } @end