iOS 自定义控件 自定义进度条

在日常iOS开发中,系统提供的控件常常无法满足业务功能,这个时候需要我们实现一些自定义控件。自定义控件能让我们完全控制视图的展示内容以及交互操作。本篇将介绍一些自定义控件的相关概念,探讨自定义控件开发的基本过程及技巧。

在开始之前我们先介绍一个类UIVew,它在iOS APP中占有绝对重要的地位,因为几乎所有的控件都是继承自UIView类。
UIView表示屏幕上的一个矩形区域,负责渲染区域内的内容,并且响应区域内发生的触摸事件。
在UIView的内部有一个CALayer,提供内容的绘制和显示,包括UIView的尺寸样式。UIView的frame实际上返回的CALayer的frame。
UIView继承自UIResponder类,它能接收并处理从系统传来的事件,CALayer继承自NSObject,它无法响应事件。所以UIView与CALayer的最大区别在于:UIView能响应事件,而CALayer不能。
效果图:

屏幕快照 2016-11-11 下午5.27.40.png

//实现思路就是在一个view界面定制

#import <UIKit/UIKit.h>

@interface circleImageView : UIView

- (void)configeWithImage:(UIImage *)image;

@end
#import "circleImageView.h"

@interface circleImageView ()
{
    UIImageView *_imageView;
}
@end

@implementation circleImageView

-(instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        _imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
        _imageView.contentMode = UIViewContentModeScaleAspectFill;
        _imageView.layer.masksToBounds = YES;
        _imageView.layer.cornerRadius = frame.size.width/2;
        [self addSubview:_imageView];
    }
    return self;
}

- (void)configeWithImage:(UIImage *)image {
    _imageView.image = image;
}

在viewController界面设置view的位置大小

#import "ViewController.h"
#import "circleImageView.h"
@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    UIImage *image = [UIImage imageNamed:@"haha.png"];
    circleImageView * ImageView = [[circleImageView alloc] initWithFrame:CGRectMake(150, 180, 150, 150)];
    [ImageView configeWithImage:image];
    [self.view addSubview:ImageView];

}
                 

进度条

效果图:

Untitled11111.gif

其实进度条就是在view界面画图,想要了解一些关于画图的东西可以查看我的前几篇文章
第一种进度条:
在view界面,在.h文件,我们定义4个属性,进度,未过滑道时的背景颜色,走过滑道的背景颜色,线条的宽度

#import <UIKit/UIKit.h>

@interface JJHCircleProgressView : UIView

//进度
@property (nonatomic)CGFloat progress;

//未过滑道时的背景颜色,默认是灰色
@property (nonatomic,strong)UIColor *trackBackgroundColor;

//走过滑道的背景颜色,默认是黄色
@property (nonatomic,strong)UIColor *trackColor;

//线条的宽度  默认是10
@property (nonatomic,assign)float lineWidth;


@end

在.m界面,如果要是布局控件,必须在layoutSubviews这里面写他的位置

#import "JJHCircleProgressView.h"

@interface JJHCircleProgressView ()

@property (strong, nonatomic) UILabel *progressLabel;

@end

@implementation JJHCircleProgressView

//可以在xib storyBoard  以及代码中都可以实现
- (instancetype)init
{
    self = [super init];
    if (self) {
        [self commonInit];
    }
    return self;
}
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self commonInit];
    }
    return self;
}
- (instancetype)initWithCoder:(NSCoder *)coder
{
    self = [super initWithCoder:coder];
    if (self) {
        [self commonInit];
    }
    return self;
}



//设置各种属性,以及把label添加上
- (void)commonInit {
    _lineWidth = 10.0;
    _trackBackgroundColor = [UIColor grayColor];
    _trackColor = [UIColor yellowColor];
}

//要是布局控件,必须在这里面写他的位置
- (void)layoutSubviews{
    [super layoutSubviews];
    UILabel *progressLabel = [[UILabel alloc] init];
    progressLabel.textColor = [UIColor blackColor];
    progressLabel.backgroundColor = [UIColor clearColor];
    progressLabel.textAlignment = NSTextAlignmentCenter;
    progressLabel.text = @"0.0%";
    _progressLabel = progressLabel;
    [self addSubview:_progressLabel];
    CGFloat slideLength = self.bounds.size.width;
    self.progressLabel.frame = CGRectMake(_lineWidth, _lineWidth, slideLength-2*_lineWidth, slideLength-2*_lineWidth);
}

//画图
- (void)drawRect:(CGRect)rect{
    [super drawRect:rect];
    
    
    //获取图形上下文
    CGContextRef contextRef = UIGraphicsGetCurrentContext();
    CGFloat slideLength = self.bounds.size.width;
    CGFloat centerX = slideLength/2;
    CGFloat centerY = slideLength/2;
    //  x,y为圆点坐标,radius半径,startAngle为开始的弧度,endAngle为 结束的弧度,clockwise 0为顺时针,1为逆时针。
    //    CGContextAddArc(<#CGContextRef  _Nullable c#>, <#CGFloat x#>, <#CGFloat y#>, <#CGFloat radius#>, <#CGFloat startAngle#>, <#CGFloat endAngle#>, <#int clockwise#>)
    //添加背景轨道
    CGContextAddArc(contextRef, centerX, centerY, (slideLength - _lineWidth)/2, 0, 2 * M_PI, 0);
    

    //设置背景的宽度
    CGContextSetLineWidth(contextRef, _lineWidth);
    //设置背景颜色
    [_trackBackgroundColor setStroke];
    //绘制轨道
    CGContextStrokePath(contextRef);
    
   
    
    
    //绘制进度颜色===================
    
    //添加背景轨道
    //角度
    float endAngle = _progress * (2 * M_PI);
    CGContextAddArc(contextRef, centerX, centerY, (slideLength - _lineWidth)/2, 0, endAngle, 0);
    
    
    //设置背景的宽度
    CGContextSetLineWidth(contextRef, _lineWidth);
    //设置背景颜色
    [_trackColor setStroke];
    //绘制轨道
    CGContextStrokePath(contextRef);
}

- (void)setProgress:(CGFloat)progress {
    
    if (progress > 1 || progress < 0) return;
    _progress = progress;
    self.progressLabel.text = [NSString stringWithFormat:@"%.1f%%", progress*100];
    // 标记为需要重新绘制, 将会在下一个绘制循环中, 调用drawRect:方法重新绘制
    [self setNeedsDisplay];
    
}
@end

第二种进度条
在.h界面,我们定义4个属性,进度,边框线的颜色,设置边框线的宽度,里面填充的颜色 默认是红色

#import <UIKit/UIKit.h>

@interface JJHBreadProgressView : UIView
//进度
@property (nonatomic)CGFloat progress;
//边框线的颜色  默认是灰色
@property (nonatomic)UIColor *lineColor;
//设置边框线的宽度  默认是10.0
@property (nonatomic)CGFloat lineWidth;
//里面填充的颜色  默认是红色
@property (nonatomic)UIColor *BreadClolor;
@end

在.m文件

#import "JJHBreadProgressView.h"
@implementation JJHBreadProgressView
//可以在xib storyBoard  以及代码中都可以实现
- (instancetype)init
{
    self = [super init];
    if (self) {
        [self commonInit];
    }
    return self;
}
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self commonInit];
    }
    return self;
}
- (instancetype)initWithCoder:(NSCoder *)coder
{
    self = [super initWithCoder:coder];
    if (self) {
        [self commonInit];
    }
    return self;
}


- (void)commonInit{
    _lineColor = [UIColor grayColor];
    _BreadClolor = [UIColor redColor];
    _lineWidth = 2.0;
}




- (void)drawRect:(CGRect)rect {
    [super drawRect:rect];
    
    
    //获取图形上下文
    CGContextRef contextRef = UIGraphicsGetCurrentContext();
    CGFloat slideLength = self.bounds.size.width;
    CGFloat centerX = slideLength/2;
    CGFloat centerY = slideLength/2;
    //  x,y为圆点坐标,radius半径,startAngle为开始的弧度,endAngle为 结束的弧度,clockwise 0为顺时针,1为逆时针。
    //    CGContextAddArc(<#CGContextRef  _Nullable c#>, <#CGFloat x#>, <#CGFloat y#>, <#CGFloat radius#>, <#CGFloat startAngle#>, <#CGFloat endAngle#>, <#int clockwise#>)
    //添加背景轨道
    CGContextAddArc(contextRef, centerX, centerY, (slideLength - _lineWidth)/2, 0, 2 * M_PI, 0);
    
    
    //设置背景的宽度
    CGContextSetLineWidth(contextRef, _lineWidth);
    //设置背景颜色
    [_lineColor setStroke];
    //绘制轨道
    CGContextStrokePath(contextRef);
    
    
    //===============划填充物=============
    
    CGContextMoveToPoint(contextRef, centerX, centerY);
    //结束时的转的弧度
    float endBread = _progress * 2 * M_PI;
    CGContextAddArc(contextRef, centerX, centerY, (slideLength-2*_lineWidth)/2, 0, endBread, 0);
    CGContextSetFillColorWithColor(contextRef, _BreadClolor.CGColor);
   
    CGContextFillPath(contextRef);
    
    

}
 
@end

iOS,小小白,有空就写一些小东西。肯定会有很多不足的地方,希望各位大神多多指教。。。。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 151,688评论 1 330
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 64,559评论 1 273
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 101,749评论 0 226
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 42,581评论 0 191
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 50,741评论 3 271
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 39,684评论 1 192
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,122评论 2 292
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 29,847评论 0 182
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 33,441评论 0 228
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 29,939评论 2 232
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 31,333评论 1 242
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 27,783评论 2 236
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 32,275评论 3 220
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 25,830评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,444评论 0 180
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 34,553评论 2 249
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 34,618评论 2 249

推荐阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 11,551评论 4 58
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 170,471评论 25 707
  • 争取到了一个单人间,开心的恨不得感觉一辈子都尘埃落地了,到了晚上却发现一个人诚惶诚恐。我还是有着人最本质的属性吧,群居。
    绿树底下乘凉阅读 184评论 0 2
  • 你有没有想过改变现状,让自己更有用,更有价值。你现在拥有的生活,是你想要的吗? 刚参加工作那会,对一切都挺很陌生,...
    大黑牛阅读 182评论 0 0
  • 本文参与#漫步青春#征文活动,作者:钱婧,本人承诺,文章内容为原创,且未在其他平台发布。 墨绿色的旷野上, 蓝色的...
    一碗鱼丸面阅读 167评论 0 0