You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

286 lines
13 KiB

/**
* Tencent is pleased to support the open source community by making QMUI_iOS available.
* Copyright (C) 2016-2020 THL A29 Limited, a Tencent company. All rights reserved.
* Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
* http://opensource.org/licenses/MIT
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
*/
//
// UIView+QMUI.h
// qmui
//
// Created by QMUI Team on 15/7/20.
//
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
@interface UIView (QMUI)
/**
initWithFrame:CGRectMake(0, 0, size.width, size.height)
@param size size
@return
*/
- (instancetype)qmui_initWithSize:(CGSize)size;
/**
frame CGRectApplyAffineTransformWithAnchorPoint
*/
@property(nonatomic, assign) CGRect qmui_frameApplyTransform;
/**
iOS 11 self.safeAreaInsets UIEdgeInsetsZero
*/
@property(nonatomic, assign, readonly) UIEdgeInsets qmui_safeAreaInsets;
/**
tintColor superview.tintColor
*/
@property(nonatomic, assign, readonly) BOOL qmui_tintColorCustomized;
/**
subviews
*/
- (void)qmui_removeAllSubviews;
/// 同 [UIView convertPoint:toView:],但支持在分属两个不同 window 的 view 之间进行坐标转换,也支持参数 view 直接传一个 window。
- (CGPoint)qmui_convertPoint:(CGPoint)point toView:(nullable UIView *)view;
/// 同 [UIView convertPoint:fromView:],但支持在分属两个不同 window 的 view 之间进行坐标转换,也支持参数 view 直接传一个 window。
- (CGPoint)qmui_convertPoint:(CGPoint)point fromView:(nullable UIView *)view;
/// 同 [UIView convertRect:toView:],但支持在分属两个不同 window 的 view 之间进行坐标转换,也支持参数 view 直接传一个 window。
- (CGRect)qmui_convertRect:(CGRect)rect toView:(nullable UIView *)view;
/// 同 [UIView convertRect:fromView:],但支持在分属两个不同 window 的 view 之间进行坐标转换,也支持参数 view 直接传一个 window。
- (CGRect)qmui_convertRect:(CGRect)rect fromView:(nullable UIView *)view;
+ (void)qmui_animateWithAnimated:(BOOL)animated duration:(NSTimeInterval)duration delay:(NSTimeInterval)delay options:(UIViewAnimationOptions)options animations:(void (^)(void))animations completion:(void (^ __nullable)(BOOL finished))completion;
+ (void)qmui_animateWithAnimated:(BOOL)animated duration:(NSTimeInterval)duration animations:(void (^ __nullable)(void))animations completion:(void (^ __nullable)(BOOL finished))completion;
+ (void)qmui_animateWithAnimated:(BOOL)animated duration:(NSTimeInterval)duration animations:(void (^ __nullable)(void))animations;
+ (void)qmui_animateWithAnimated:(BOOL)animated duration:(NSTimeInterval)duration delay:(NSTimeInterval)delay usingSpringWithDamping:(CGFloat)dampingRatio initialSpringVelocity:(CGFloat)velocity options:(UIViewAnimationOptions)options animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion;
@end
@interface UIView (QMUI_Block)
/**
UIView frame block setFrame:setBounds:setCenter:setTransform: rect frame [super setFrame:] 使 block setTransform: frame
@param view view 便使 weak
@param followingFrame setFrame: frame rect
@return 使 frame
@note followingFrame self.frame
*/
@property(nullable, nonatomic, copy) CGRect (^qmui_frameWillChangeBlock)(__kindof UIView *view, CGRect followingFrame);
/**
UIView frame block setFrame:setBounds:setCenter:setTransform:便 layoutSubviews 使 block
@param view view 便使 weak
@param precedingFrame frame
*/
@property(nullable, nonatomic, copy) void (^qmui_frameDidChangeBlock)(__kindof UIView *view, CGRect precedingFrame);
/**
UIView layoutSubviews runloop runloop layoutSubviews subviews
@param view view 便使 weak
@note view layoutSubviews super block
*/
@property(nullable, nonatomic, copy) void (^qmui_layoutSubviewsBlock)(__kindof UIView *view);
/**
tintColorDidChange block
@param view view 便使 weak
*/
@property(nullable, nonatomic, copy) void (^qmui_tintColorDidChangeBlock)(__kindof UIView *view);
/**
hitTest:withEvent: block
@param point point
@param event
@param super
*/
@property(nullable, nonatomic, copy) __kindof UIView * (^qmui_hitTestBlock)(CGPoint point, UIEvent *event, __kindof UIView *originalView);
@end
@interface UIView (QMUI_ViewController)
/**
view view UIViewController [viewWillAppear, viewWillDisappear)
*/
@property(nonatomic, assign, readonly) BOOL qmui_visible;
/**
view UIViewController.view
*/
@property(nonatomic, assign) BOOL qmui_isControllerRootView;
/**
view UIViewController superview使
*/
@property(nullable, nonatomic, weak, readonly) __kindof UIViewController *qmui_viewController;
@end
@interface UIView (QMUI_Runtime)
/**
* UIView
* @param selector
* @return YES NO 使 UIView
*/
- (BOOL)qmui_hasOverrideUIKitMethod:(SEL)selector;
@end
typedef NS_OPTIONS(NSUInteger, QMUIViewBorderPosition) {
QMUIViewBorderPositionNone = 0,
QMUIViewBorderPositionTop = 1 << 0,
QMUIViewBorderPositionLeft = 1 << 1,
QMUIViewBorderPositionBottom = 1 << 2,
QMUIViewBorderPositionRight = 1 << 3
};
typedef NS_ENUM(NSUInteger, QMUIViewBorderLocation) {
QMUIViewBorderLocationInside,
QMUIViewBorderLocationCenter,
QMUIViewBorderLocationOutside
};
/**
* UIView (QMUI_Border) UIView 便
*
* UIView view.layer view.layer UIView (QMUI_Border) qmui_borderPosition
* @warning UIView 使 view.layer UIView (QMUI_Border) QMUIViewBorderPosition QMUIViewBorderPositionAll
*/
@interface UIView (QMUI_Border)
/// 设置边框的位置,默认为 QMUIViewBorderLocationInside,与 view.layer.border 一致。
@property(nonatomic, assign) QMUIViewBorderLocation qmui_borderLocation;
/// 设置边框类型,支持组合,例如:`borderPosition = QMUIViewBorderPositionTop|QMUIViewBorderPositionBottom`。默认为 QMUIViewBorderPositionNone。
@property(nonatomic, assign) QMUIViewBorderPosition qmui_borderPosition;
/// 边框的大小,默认为PixelOne。请注意修改 qmui_borderPosition 的值以将边框显示出来。
@property(nonatomic, assign) IBInspectable CGFloat qmui_borderWidth;
/// 边框的颜色,默认为UIColorSeparator。请注意修改 qmui_borderPosition 的值以将边框显示出来。
@property(nullable, nonatomic, strong) IBInspectable UIColor *qmui_borderColor;
/// 虚线 : dashPhase默认是0,且当dashPattern设置了才有效
/// qmui_dashPhase 表示虚线起始的偏移,qmui_dashPattern 可以传一个数组,表示“lineWidth,lineSpacing,lineWidth,lineSpacing...”的顺序,至少传 2 个。
@property(nonatomic, assign) CGFloat qmui_dashPhase;
@property(nullable, nonatomic, copy) NSArray <NSNumber *> *qmui_dashPattern;
/// border的layer
@property(nullable, nonatomic, strong, readonly) CAShapeLayer *qmui_borderLayer;
@end
/**
* 便 UIView UIImage UIView transform UIView
*/
@interface UIView (QMUI_Snapshotting)
- (UIImage *)qmui_snapshotLayerImage;
- (UIImage *)qmui_snapshotImageAfterScreenUpdates:(BOOL)afterScreenUpdates;
@end
/**
UIView setFrame: sizeThatFits view
@code
// 以前这么写
CGSize size = [view sizeThatFits:CGSizeMake(width, CGFLOAT_MAX)];
view.frame = CGRectMake(x, y, width, size.height);
// 现在可以这么写:
view.frame = CGRectMake(x, y, width, QMUIViewSelfSizingHeight);
@endcode
*/
extern const CGFloat QMUIViewSelfSizingHeight;
/**
* view.frame 便 view view
*/
@interface UIView (QMUI_Layout)
/// 等价于 CGRectGetMinY(frame)
@property(nonatomic, assign) CGFloat qmui_top;
/// 等价于 CGRectGetMinX(frame)
@property(nonatomic, assign) CGFloat qmui_left;
/// 等价于 CGRectGetMaxY(frame)
@property(nonatomic, assign) CGFloat qmui_bottom;
/// 等价于 CGRectGetMaxX(frame)
@property(nonatomic, assign) CGFloat qmui_right;
/// 等价于 CGRectGetWidth(frame)
@property(nonatomic, assign) CGFloat qmui_width;
/// 等价于 CGRectGetHeight(frame)
@property(nonatomic, assign) CGFloat qmui_height;
/// 保持其他三个边缘的位置不变的情况下,将顶边缘拓展到某个指定的位置,注意高度会跟随变化。
@property(nonatomic, assign) CGFloat qmui_extendToTop;
/// 保持其他三个边缘的位置不变的情况下,将左边缘拓展到某个指定的位置,注意宽度会跟随变化。
@property(nonatomic, assign) CGFloat qmui_extendToLeft;
/// 保持其他三个边缘的位置不变的情况下,将底边缘拓展到某个指定的位置,注意高度会跟随变化。
@property(nonatomic, assign) CGFloat qmui_extendToBottom;
/// 保持其他三个边缘的位置不变的情况下,将右边缘拓展到某个指定的位置,注意宽度会跟随变化。
@property(nonatomic, assign) CGFloat qmui_extendToRight;
/// 获取当前 view 在 superview 内水平居中时的 left
@property(nonatomic, assign, readonly) CGFloat qmui_leftWhenCenterInSuperview;
/// 获取当前 view 在 superview 内垂直居中时的 top
@property(nonatomic, assign, readonly) CGFloat qmui_topWhenCenterInSuperview;
@end
@interface UIView (CGAffineTransform)
/// 获取当前 view 的 transform scale x
@property(nonatomic, assign, readonly) CGFloat qmui_scaleX;
/// 获取当前 view 的 transform scale y
@property(nonatomic, assign, readonly) CGFloat qmui_scaleY;
/// 获取当前 view 的 transform translation x
@property(nonatomic, assign, readonly) CGFloat qmui_translationX;
/// 获取当前 view 的 transform translation y
@property(nonatomic, assign, readonly) CGFloat qmui_translationY;
@end
/**
* Debug UIView view subviews view
*/
@interface UIView (QMUI_Debug)
/// 是否需要添加debug背景色,默认NO
@property(nonatomic, assign) BOOL qmui_shouldShowDebugColor;
/// 是否每个view的背景色随机,如果不随机则统一使用半透明红色,默认NO
@property(nonatomic, assign) BOOL qmui_needsDifferentDebugColor;
/// 标记一个view是否已经被添加了debug背景色,外部一般不使用
@property(nonatomic, assign, readonly) BOOL qmui_hasDebugColor;
@end
NS_ASSUME_NONNULL_END