一.Objective-C版
.h文件
#import <UIKit/UIKit.h>
@interface UIView (WLKit)
/*属性*/
@property (nonatomic, assign) CGFloat x;
@property (nonatomic, assign) CGFloat y;
@property (nonatomic, assign) CGFloat centerX;
@property (nonatomic, assign) CGFloat centerY;
@property (nonatomic, assign) CGFloat width;
@property (nonatomic, assign) CGFloat height;
@property (nonatomic, assign) CGFloat top;
@property (nonatomic, assign) CGFloat bottom;
@property (nonatomic, assign) CGFloat left;
@property (nonatomic, assign) CGFloat right;
@property (nonatomic, assign) CGSize size;
@property (nonatomic, assign) CGPoint origin;
/*方法*/
/**
* 初始化UIView
*
* @param frame UIView的坐标
* @param backgroundColor UIView的背景颜色
*/
+ (instancetype _Nonnull)viewWithFrame:(CGRect)frame
backgroundColor:(UIColor *_Nonnull)backgroundColor;
/**
* 设置圆角
*
* @param 圆角的值
*/
- (void)setCornerRadius:(CGFloat)radius;
/**
* 在当前View截图
*
* @return 返回截图
*/
- (UIImage *_Nonnull)screenshot;
/**
* 在当前View截图并且保存到系统相册
*
* @return 返回截图
*/
- (UIImage *_Nonnull)saveScreenshot;
/**
* 找到UIView中的UIViewController
*/
- (UIViewController *_Nullable)viewController;
@end
.m文件
#import "UIView+WLKit.h"
@implementation UIView (WLKit)
- (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)setCenterX:(CGFloat)centerX
{
CGPoint center = self.center;
center.x = centerX;
self.center = center;
}
- (CGFloat)centerX
{
return self.center.x;
}
- (void)setCenterY:(CGFloat)centerY
{
CGPoint center = self.center;
center.y = centerY;
self.center = center;
}
- (CGFloat)centerY
{
return self.center.y;
}
- (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)setOrigin:(CGPoint)origin
{
CGRect frame = self.frame;
frame.origin = origin;
self.frame = frame;
}
- (CGPoint)origin
{
return self.frame.origin;
}
- (void)setTop:(CGFloat)top
{
CGRect frame = self.frame;
frame.origin.y = top;
self.frame = frame;
}
- (CGFloat)top
{
return self.frame.origin.y;
}
- (void)setBottom:(CGFloat)bottom
{
CGRect frame = self.frame;
frame.origin.y = bottom - frame.size.height;
self.frame = frame;
}
- (CGFloat)bottom
{
return self.top + self.height;
}
- (void)setLeft:(CGFloat)left
{
CGRect frame = self.frame;
frame.origin.x = left;
self.frame = frame;
}
- (CGFloat)left
{
return self.frame.origin.x;
}
- (void)setRight:(CGFloat)right
{
CGRect frame = self.frame;
frame.origin.x = right - frame.size.width;
self.frame = frame;
}
- (CGFloat)right
{
return self.frame.origin.x + self.frame.size.width;
}
#pragma mark - 初始化方法
+ (instancetype _Nonnull)viewWithFrame:(CGRect)frame backgroundColor:(UIColor *_Nonnull)backgroundColor
{
UIView *view = [[UIView alloc] initWithFrame:frame];
[view setBackgroundColor:backgroundColor];
return view;
}
#pragma mark - 设置圆角
- (void)setCornerRadius:(CGFloat)radius
{
self.layer.cornerRadius = radius;
[self.layer setMasksToBounds:YES];
}
#pragma mark - 获取截图
- (UIImage *_Nonnull)screenshot
{
UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, [UIScreen mainScreen].scale);
[self drawViewHierarchyInRect:self.bounds afterScreenUpdates:YES];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSData *imageData = UIImagePNGRepresentation(image);
image = [UIImage imageWithData:imageData];
return image;
}
- (UIImage *_Nonnull)saveScreenshot
{
UIImage *image = [self screenshot];
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
return image;
}
#pragma mark - 找到UIView中的UIViewController
- (UIViewController *_Nullable)viewController
{
UIResponder *next = self.nextResponder;
do {
if ([next isKindOfClass:[UIViewController class]]) {
return (UIViewController *)next;
}
next = next.nextResponder;
} while (next != nil);
return nil;
}
@end