123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408 |
- //
- // UIView+RQExtension.m
- // RQCommon
- //
- // Created by 张嵘 on 2018/11/16.
- // Copyright © 2018 张嵘. All rights reserved.
- //
- #import "UIView+RQExtension.h"
- #import <objc/runtime.h>
- 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
|