示例代码

//

#import <UIKit/UIKit.h>

@interface UIView (LPCView)

/** 上 */
@property CGFloat top;
/** 下 */
@property CGFloat bottom;
/** 左 */
@property CGFloat left;
/** 右 */
@property CGFloat right;
/** 中心x */
@property CGFloat centerX;
/** 中心y */
@property CGFloat centerY;
/** 高 */
@property CGFloat height;
/** 宽 */
@property CGFloat width;
/** 角标数 */
@property NSInteger brigeNum;
/** 尺寸 */
@property CGSize size;
/** 坐标 */
@property (nonatomic, assign) CGPoint origin;
/** 偏移x */
@property (nonatomic, assign) CGFloat tx;
/** 偏移y */
@property (nonatomic, assign) CGFloat ty;
/** 是否圆角-高的十分之一 */
@property BOOL isCornerRadiusHeight10;
/** 是否切圆-高的一半 */
@property BOOL isCornerRadiusHalfWidth;
/** 边框颜色 */
@property (nonatomic, strong) UIColor * borderColor;
/** 虚线边框 */
- (void)LPCDashedLine:(UIColor *)lineColor lineWidth:(CGFloat)lineWidth spaceAry:(NSArray<NSNumber *> *)spaceAry;
/** 实线边框 */
- (void)LPCSolidLine:(UIColor *)lineColor lineWidth:(CGFloat)lineWidth;
/** 点击事件-目标-事件 */
- (void)addTarget:(id)target action:(SEL)action;
/** 当前View的视图控制器 */
- (nullable UIViewController *)viewController;

NS_ASSUME_NONNULL_BEGIN
//__nullable表示对象可以是NULL或nil,而__nonnull表示对象不应该为空
/** 避免出现警告 */
NS_ASSUME_NONNULL_END

@end
////////////////////////////m//////////////////////////

//

#import "UIView+LPCView.h"

@implementation UIView (LPCView)

- (CGFloat)top {
    return self.frame.origin.y;
}

- (void)setTop:(CGFloat)top {
    CGRect frame = self.frame;
    frame.origin.y = top;
    self.frame = frame;
}

- (CGFloat)bottom {
    return self.top + self.height;
}

- (void)setBottom:(CGFloat)bottom {
    self.top = bottom - self.height;
}

- (CGFloat)left {
    return self.frame.origin.x;
}

- (void)setLeft:(CGFloat)left {
    CGRect frame = self.frame;
    frame.origin.x = left;
    self.frame = frame;
}

- (CGFloat)right {
    return self.left + self.width;
}

- (void)setRight:(CGFloat)right {
    self.left = right - self.width;
}

- (CGFloat)centerX {
    return self.center.x;
}

- (void)setCenterX:(CGFloat)centerX {
    self.center = CGPointMake(centerX, self.centerY);
}

- (CGFloat)centerY {
    return self.center.y;
}

- (void)setCenterY:(CGFloat)centerY {
    self.center = CGPointMake(self.centerX, centerY);
}

- (CGFloat)height {
    return self.frame.size.height;
}

- (void)setHeight:(CGFloat)height {
    CGRect frame = self.frame;
    frame.size.height = height;
    self.frame = frame;
}

- (CGFloat)width {
    return self.frame.size.width;
}

- (void)setWidth:(CGFloat)width {
    CGRect frame = self.frame;
    frame.size.width = width;
    self.frame = frame;
}
- (CGSize)size {
    return self.frame.size;
}
- (void)setSize:(CGSize)size {
    CGRect frame = self.frame;
    frame.size = size;
    self.frame = frame;
}

- (CGPoint)origin {
    return self.frame.origin;
}

- (void)setOrigin:(CGPoint)origin {
    self.frame = CGRectMake(origin.x, origin.y, self.width, self.height);
}

- (CGFloat)tx {
    return self.transform.tx;
}

- (void)setTx:(CGFloat)tx {
    CGAffineTransform transform = self.transform;
    transform.tx = tx;
    self.transform = transform;
}

- (CGFloat)ty {
    return self.transform.ty;
}

- (void)setTy:(CGFloat)ty {
    CGAffineTransform transform = self.transform;
    transform.ty = ty;
    self.transform = transform;
}

- (nullable UIViewController *)viewController {
    UIResponder *responder = self;
    while ((responder = [responder nextResponder]))
        if ([responder isKindOfClass: [UIViewController class]]) {
            return (UIViewController *)responder;
        }
    return nil;
}

//角标
- (NSInteger)brigeNum {
    if ([[self viewWithTag:48511] isKindOfClass:[UILabel class]]) {
        UILabel * tempLabel = [self viewWithTag:48511];
        return [tempLabel.text integerValue];
    }
    return 0;
}
//角标
- (void)setBrigeNum:(NSInteger)brigeNum {
    NSInteger length = [NSString stringWithFormat:@"%ld", (long)brigeNum].length;
    brigeNum = brigeNum > 99 ? 99 : brigeNum;
    if (0 == length && 0 == brigeNum) {
        return;
    }
    CGRect tempFrame = CGRectMake(self.left + self.width - 6,self.top, 12, 12);
    if ([[self.superview viewWithTag:48511] isKindOfClass:[UILabel class]]) {
        UILabel * tempLabel = [self.superview viewWithTag:48511];
        tempLabel.frame = tempFrame;
        tempLabel.text = [NSString stringWithFormat:@"%ld", (long)brigeNum];
        if (0 == brigeNum) {
            tempLabel.alpha = 0;
        }else {
            tempLabel.alpha = 1;
        }
    }else {
        UILabel * la = [[UILabel alloc] initWithFrame:tempFrame];
        la.text = [NSString stringWithFormat:@"%ld", (long)brigeNum];
        la.font = [UIFont systemFontOfSize:9];
        la.textAlignment = NSTextAlignmentCenter;
        la.textColor = [UIColor whiteColor];
        la.layer.cornerRadius = 6;
        la.layer.masksToBounds = YES;
        
        la.tag = 48511;
        la.backgroundColor = [UIColor redColor];
        
        if (length > 2) {
            la.frame = CGRectMake(tempFrame.size.width - 6, tempFrame.origin.y, 12, 12);
        }
        [self.superview addSubview:la];
        if (0 == brigeNum) {
            la.alpha = 0;
        }else {
            la.alpha = 1;
        }
    }
}

//边框颜色
- (UIColor *)borderColor {
    return [UIColor colorWithCGColor:self.layer.borderColor];
}
- (void)setBorderColor:(UIColor *)borderColor {
    self.layer.borderColor = [borderColor CGColor];
}
//圆角度
- (BOOL)isCornerRadiusHeight10 {
    if (self.height / 10.0 == self.layer.cornerRadius) {
        return YES;
    }
    return NO;
}

- (void)setIsCornerRadiusHeight10:(BOOL)isCornerRadiusHeight10 {
    if (isCornerRadiusHeight10) {
        self.layer.cornerRadius = self.height * kScale_Height / 10.0;
        self.layer.masksToBounds = YES;
    }else {
        isCornerRadiusHeight10 = NO;
    }
}
//切圆
- (BOOL)isCornerRadiusHalfWidth {
    if (self.layer.cornerRadius == self.height / 2.0) {
        return YES;
    }
    return NO;
}

- (void)setIsCornerRadiusHalfWidth:(BOOL)isCornerRadiusHalfWidth {
    if (isCornerRadiusHalfWidth) {
        self.layer.cornerRadius = self.height / 2.0;
        self.layer.masksToBounds = YES;
    }else {
        isCornerRadiusHalfWidth = NO;
    }
}

//虚线边框
- (void)LPCDashedLine:(UIColor *)lineColor lineWidth:(CGFloat)lineWidth spaceAry:(NSArray<NSNumber *> *)spaceAry {
    
    CAShapeLayer *borderLayer = [CAShapeLayer layer];
    
    borderLayer.bounds = CGRectMake(0, 0, self.width , self.height);
    
    borderLayer.position = CGPointMake(CGRectGetMidX(self.bounds), CGRectGetMidY(self.bounds));
    
    //不带圆角
//    borderLayer.path = [UIBezierPath bezierPathWithRect:borderLayer.bounds].CGPath;
    //带圆角
    borderLayer.path = [UIBezierPath bezierPathWithRoundedRect:borderLayer.bounds cornerRadius:self.layer.cornerRadius].CGPath;
    borderLayer.lineWidth = lineWidth / [[UIScreen mainScreen] scale];
    //虚线边框
    borderLayer.lineDashPattern = spaceAry;
    borderLayer.fillColor = [UIColor clearColor].CGColor;
    borderLayer.strokeColor = lineColor.CGColor;
    [self.layer addSublayer:borderLayer];
}
//实线变框
- (void)LPCSolidLine:(UIColor *)lineColor lineWidth:(CGFloat)lineWidth {
    CAShapeLayer *borderLayer = [CAShapeLayer layer];
    
    borderLayer.bounds = CGRectMake(0, 0, self.width , self.height);
    
    borderLayer.position = CGPointMake(CGRectGetMidX(self.bounds), CGRectGetMidY(self.bounds));
    
    //不带圆角
    //    borderLayer.path = [UIBezierPath bezierPathWithRect:borderLayer.bounds].CGPath;
    //带圆角
    borderLayer.path = [UIBezierPath bezierPathWithRoundedRect:borderLayer.bounds cornerRadius:self.layer.cornerRadius].CGPath;
    borderLayer.lineWidth = lineWidth / [[UIScreen mainScreen] scale];
    //实线边框
    borderLayer.lineDashPattern = nil;
    borderLayer.fillColor = [UIColor clearColor].CGColor;
    borderLayer.strokeColor = lineColor.CGColor;
    [self.layer addSublayer:borderLayer];
}

/** 点击事件-目标-事件 */
- (void)addTarget:(id)target action:(SEL)action {
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:target action:action];
    self.userInteractionEnabled = YES;
    [self addGestureRecognizer:tap];
}

@end

iOS 自定义方法 - UIView扩展的更多相关文章

  1. iOS开发-UIView扩展CGRect

    关于UIView的位置都会遇到,一般需要改变UIView的位置,需要先获取原有的frame位置,然后在frame上面修改,有的时候如果只是改变了一下垂直方向的位置,宽度和高度的一种,这种写法很麻烦.下 ...

  2. 开发腾讯移动游戏平台SDK ios版Ane扩展 总结

    本文记录了在开发 腾讯移动游戏平台SDK(MSDK) ios版Ane扩展 过程中所遇到的问题 文中非常多问题都是基础的问题.对object c和xcode配置了解不深入导致的.(没办法,开发ane的程 ...

  3. iOS 通知中心扩展制作初步-b

    涉及的 Session 有 Creating Extensions for iOS and OS X, Part 1 Creating Extensions for iOS and OS X, Par ...

  4. IOS中类的扩展(协议,分类)

    IOS中类的扩展(协议,分类) 扩展类,我们可以使用协议和分类这两种方法,下面我们来分别实现这两种方法: 参考网址:http://www.cnblogs.com/wendingding/p/37095 ...

  5. iOS学习系列 - 扩展机制category与associative

    iOS学习系列 - 扩展机制category与associative category与associative作为objective-c的扩展机制的两个特性,category即类型,可以通过它来扩展方 ...

  6. 荼菜的iOS笔记--UIView的几个Block动画

    前言:我的第一篇文章荼菜的iOS笔记–Core Animation 核心动画算是比较详细讲了核心动画的用法,但是如你上篇看到的,有时我们只是想实现一些很小的动画,这时再用coreAnimation就会 ...

  7. iOS学习——UIView的研究

    在iOS开发中,我们知道有一个共同的基类——NSObject,但是对于界面视图而言,UIView是非常重要的一个类,UIView是很多视图控件的基类,因此,对于UIView的学习闲的非常有必要.在iO ...

  8. ios 关于UIView 的multipleTouchEnabled 和 exclusiveTouch

    做项目时发现,在一个界面上的2个button竟然可以同时点击,依次push进去了2个 controller!我就产生了疑问,一个view的multipleTouchEnabled属性默认是false啊 ...

  9. iOS 使用UIView的一种有效方法

    在一个典型的MVC结构 中,Model部分负责保存目标数据,View部分主要负责实现数据的界面以及将数据显示出来,二者在Controller的操作下协同工作.在iOS应用中,View的实现主要由UIV ...

随机推荐

  1. linux下mono,powershell安装教程

    1简介 简单来说pash就是bash+powershell 2官网 https://github.com/Pash-Project/Pash 3下载fedora20---lxde桌面---32位版. ...

  2. DBCP 配置备注

    <property name="initialSize" value="5"></property> <property name ...

  3. SQL Server 服务器磁盘测试之SQLIO篇(一)

    数据库调优工作中,有一部分是需要排查IO问题的,例如IO的速度或者RAID级别无法响应高并发下的快速请求.最常见的就是查看磁盘每次读写的响应速度,通过性能计数器Avg.Disk sec/Read(Wr ...

  4. Entity Framework 6 Recipes 2nd Edition(10-2)译 -> 返回输出参数

    10-2. 返回输出参数 问题 想获取存储过程里的一个或多个输出参数的值 解决方案 假设我们有一个像Figure 10-1所示的,出租车辆与租金收入的模型 Figure 10-1.出租车辆与租金收入的 ...

  5. Entity Framework 6 Recipes 2nd Edition(10-5)译 -> 在存储模型中使用自定义函数

    10-5. 在存储模型中使用自定义函数 问题 想在模型中使用自定义函数,而不是存储过程. 解决方案 假设我们数据库里有成员(members)和他们已经发送的信息(messages) 关系数据表,如Fi ...

  6. 浅析Java 泛型

    泛型是JavaSE5引入的一个新概念,但是这个概念在编程语言中却是很普遍的一个概念.下面,根据以下内容,我们总结下在Java中使用泛型. 泛型使用的意义 什么是泛型 泛型类 泛型方法 泛型接口 泛型擦 ...

  7. Composer概述及其自动加载探秘

    composer概述 一开始,最吸引我的当属 Composer 了,因为之前从没用过 Composer . Composer 是PHP中用来管理依赖关系的工具,你只需在自己的项目中声明所依赖的外部工具 ...

  8. .NET Core采用的全新配置系统[4]: “Options模式”下各种类型的Options对象是如何绑定的?

    旨在生成Options对象的配置绑定实现在IConfiguration接口的扩展方法Bind上.配置绑定的目标类型可以是一个简单的基元类型,也可以是一个自定义数据类型,还可以是一个数组.集合或者字典类 ...

  9. 图的生成树(森林)(克鲁斯卡尔Kruskal算法和普里姆Prim算法)、以及并查集的使用

    图的连通性问题:无向图的连通分量和生成树,所有顶点均由边连接在一起,但不存在回路的图. 设图 G=(V, E) 是个连通图,当从图任一顶点出发遍历图G 时,将边集 E(G) 分成两个集合 T(G) 和 ...

  10. CSS3与页面布局学习总结(一)——概要、选择器、特殊性与刻度单位

    web前端开发者最最注的内容是三个:HTML.CSS与JavaScript,他们分别在不同方面发挥自己的作用,HTML实现页面结构,CSS完成页面的表现与风格,JavaScript实现一些客户端的功能 ...