/**
 *  用CALayer定制下载进度条控件
 *  1.单独创建出CALayer
 *  2.直接修改CALayer的frame值,执行隐式动画,实现进度条效果
 *  3.用定时器(NSTimer)模拟网络下载时提供的百分比数据
 *  4.将CALayer封装进UIView子类中定制进度条控件

*/

自定义一个UIView,创建CALayer

#import <UIKit/UIKit.h>

@interface WJProgressView : UIView

@property (nonatomic,assign)CGFloat progress;//进度参数取值范围0~100
@property (nonatomic,strong)UIColor *progressColor;//颜色 @end
#import "WJProgressView.h"

@interface WJProgressView ()

@property (nonatomic,strong)CALayer *progressLayer;
@property (nonatomic,assign)CGFloat currentViewWidth; @end @implementation WJProgressView - (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.progressLayer = [CALayer layer];
        self.backgroundColor = [UIColor grayColor];
        self.progressLayer.backgroundColor = [UIColor redColor].CGColor;
        self.progressLayer.frame = CGRectMake(, , , frame.size.height);
        [self.layer addSublayer:self.progressLayer];
        //储存当前view的宽度值
        self.currentViewWidth = frame.size.width;
    }
    return self;
} #pragma mark - 重写setter,getter方法 @synthesize progress = _progress;
- (void)setProgress:(CGFloat)progress {
    _progress = progress;
    if (progress <= ) {
        self.progressLayer.frame = CGRectMake(, , , self.frame.size.height);
    }else if (progress <= ) {
        self.progressLayer.frame = CGRectMake(, , progress *self.currentViewWidth, self.frame.size.height);
    }else {
        self.progressLayer.frame = CGRectMake(, , self.currentViewWidth, self.frame.size.height);
    }
} - (CGFloat)progress {
    return _progress;
} @synthesize progressColor = _progressColor;
- (void)setProgressColor:(UIColor *)progressColor {
    _progressColor = progressColor;
    self.progressLayer.backgroundColor = progressColor.CGColor;
} - (UIColor *)progressColor {
    return _progressColor;
}

向上面那样封装的话,只需改变progress进度属性就能改变CALayer的frame(隐式动画)

self.progressView.progress = arc4random()%/.f;

效果图:

iOS-CALayer实现简单进度条的更多相关文章

  1. 用 CALayer 定制下载进度条控件

    // // RPProgressView.h // CALayer定制下载进度条控件 // // Created by RinpeChen on 16/1/2. // Copyright © 2016 ...

  2. 用CALayer实现下载进度条控件

    用CALayer实现下载进度条 效果: 源码: // // ViewController.m // ProgressView // // Created by YouXianMing on 14/11 ...

  3. wpf 导出Excel Wpf Button 样式 wpf简单进度条 List泛型集合对象排序 C#集合

    wpf 导出Excel   1 private void Button_Click_1(object sender, RoutedEventArgs e) 2 { 3 4 ExportDataGrid ...

  4. 【iOS】环形渐变进度条实现

    之前有人在找渐变进度条的效果,闲来无事就顺手写了一个,然后画了视图层级,方便讲解. 环境信息: Mac OS X 10.10.3 Xcode 6.3.1 iOS 8.3 效果图: 源码下载地址: ht ...

  5. asp.net 的一个简单进度条功能

    我们先看下效果 我点击了按钮后他会显示进度页面,进度完成后,进度条消失,其实也是比较简单的了. 我们需要一个进度条代码文件ProgressBar.htm(注意:是没有head这些标签的) <sc ...

  6. js(ext)中,设置[!!异步!!]上传的简单进度条

    代码在updateHmis的历史记录中,此处存档 handler : function() { //显示进度条 Ext.MessageBox.wait('数据上传中...','提示'); //上传数据 ...

  7. iOS UIWebView 加载进度条的使用-WKWebView的使用,更新2017.6.26

    1.由于项目中加载网络插件,直接使用了webview加载.使用了三方NJKWebViewProgress进度条的使用,近期在测试时发现,网络缓慢时出现白屏,有卡顿现象. 于是采用了WKWebView进 ...

  8. JGUI源码:实现简单进度条(19)

    程序效果如下 实现进度条动画主要有两种方法:(1)使用缓动,(2)使用Jquery Animate,本文使用第二种方法,先实现代码,后续进行控件封装 <style> .jgui-proce ...

  9. python3 简单进度条代码

    进度条代码函数实现 import sys, time class ShowProcess(object): """ 显示处理进度的类 调用该类相关函数即可实现处理进度的显 ...

随机推荐

  1. entityFramework使用 codefirst

    新建项目 用nuget安装entityFramework,Install-Package Entityframework 建一个model和context //[Table("Custome ...

  2. Zygote(app_process)相关分析1

    首先我们从Init.c中来看,当Init中解析完init.rc文件时会得到一系列的action,通过action去调用一些函数. Zygote是在init.rc中service section中 se ...

  3. Android--用DownLoadManager下载完成后启动安装

    当我们用系统的服务DownLoadManager下载完成后,系统会发送一个广播,我们只需要注册一个广播,然后在广播里面写如一些相应的操作. 1.注册广播 completeReceiver = new ...

  4. hdu 2035 人见人爱A^B

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=2035 人见人爱A^B Description 求A^B的最后三位数表示的整数.说明:A^B的含义是“A ...

  5. ExtJs4学习MVC中的Store

    Ext.data.Store是extjs中用来进行数据交换和数据交互的标准中间件,无论是Grid还是ComboBox,都是通过它实现数据读取.类型转换.排序分页和搜索等操作的. 1 2 3 4 5 6 ...

  6. Sharepoint 2010 RBS 的安装和配置

    基本上按照微软官方的配置方法配置就行了. 给下地址:微软官方     涂大神 找RBS的安装文件也是个问题,微软的链接好像失效了.我使用的是RBS下载.这里也有. 我的环境是数据库两台故障群集转移,微 ...

  7. 文件读写 swift

    // // ViewController.swift // 文件读写 // // Created by mac on 15/7/12. // Copyright (c) 2015年 fangyuhao ...

  8. 立即执行函数(IIFE)的理解与运用

    作为JavaScript的常用语法,立即执行函数IIFE(Immediately-Invoked Function Expression)是值得我们认真去学习探究的. 一.创建函数的两种方式 我们先从 ...

  9. zookeeper数据迁移

    在不停机的情况下,实现集群之间数据迁移代码: private void create(ZooKeeper zk1, ZooKeeper zk2, String path) throws Excepti ...

  10. pagebar

    public static string GetPageBarStr(int pageIndex, int pageSize, int pageCount, int recordCount)     ...