原文网址:使用CAGradientLayer的动画精度条View

Modern software design is getting flatter and thinner all the time. Another trend that follows suit is the thin, one pixel progress bar that you see at the top of websites and apps. You’ve seen in it on Medium blogs, mobile Safari and other apps on iOS 7. I’m going to show you how to create a component like this to use in your own apps. Here is what we’re going to create:

现代软件设计越来越扁平化,更薄所有的时间。下面西装的另一个趋势是,你的网站和应用程序的顶部看到薄,一个像素进度条。你已经看到了它在中等博客,移动Safari和iOS上7的其他应用程序,我将向你展示如何创建这样一个组件在自己的应用程序中使用。下面是我们要创建:

First thing we need to do is create a new UIView subclass and give it a name. Next we need to tell this class to use CAGradientLayer as its backing layer instead of the default CALayer. You can do this by overriding the layerClass method.

我们需要做的第一件事是创建一个新的UIView子类,并给它一个名字。接下来,我们需要告诉这个类使用CAGradientLayer作为其衬里层,而不是默认的CALayer。您可以通过重写做到这一点layerClass方法。

+ (Class)layerClass {
return [CAGradientLayer class];
}

CAGradientLayer is a pretty simple subclass of CALayer that adds a few additional properties. We’re going to be using the colors, startPoint and endPoint properties to create an animated horizontal gradient.

Now there are a couple ways to achieve this rainbow effect. One way, which I am going to use, is to create an array of UIColor objects with incremental hue values. In your initWithFrame: method add the following code:

CAGradientLayer是一个非常简单的子类的CALayer,增加了一些额外的属性。我们将要使用的颜色的startPoint端点属性来创建一个动画水平梯度。

现在有几种方法来实现这个彩虹效果。一种方式,这我会使用,是创建数组的UIColor增量对象的色调值。在您的initWithFrame:方法方法中添加以下代码:

// Use a horizontal gradient
CAGradientLayer *layer = (id)[self layer];
[layer setStartPoint:CGPointMake(0.0, 0.5)];
[layer setEndPoint:CGPointMake(1.0, 0.5)];
 
// Create colors using hues in +5 increments
NSMutableArray *colors = [NSMutableArray array];
for (NSInteger hue = 0; hue <= 360; hue += 5) {
 
UIColor *color;
color = [UIColor colorWithHue:1.0 * hue / 360.0
saturation:1.0
brightness:1.0
alpha:1.0];
[colors addObject:(id)[color CGColor]];
}
[layer setColors:[NSArray arrayWithArray:colors]];

Pretty straightforward. If you parent the view and run it in the simulator you’ll see our view has a horizontal gradient with all the colors in the spectrum.

Next, to create the moving effect we can cycle the colors in the colors array and use a layer animation. A single animation will move one color and repeat the process when finished. The next two methods will do the trick.

很简单。如果父视图并运行它在模拟器上你会看到我们的看法与在光谱中所有颜色水平渐变。

接下来,创建我们可以循环中的颜色移动效果的颜色阵列,并使用图层动画。单个动画将移动一个颜色,完成后重复上述过程。接下来的两个方法就可以了。

- (void)performAnimation {
// Move the last color in the array to the front
// shifting all the other colors.
CAGradientLayer *layer = (id)[self layer];
NSMutableArray *mutable = [[layer colors] mutableCopy];
id lastColor = [[mutable lastObject] retain];
[mutable removeLastObject];
[mutable insertObject:lastColor atIndex:0];
[lastColor release];
NSArray *shiftedColors = [NSArray arrayWithArray:mutable];
[mutable release];
 
// Update the colors on the model layer
[layer setColors:shiftedColors];
 
// Create an animation to slowly move the gradient left to right.
CABasicAnimation *animation;
animation = [CABasicAnimation animationWithKeyPath:@"colors"];
[animation setToValue:shiftedColors];
[animation setDuration:0.08];
[animation setRemovedOnCompletion:YES];
[animation setFillMode:kCAFillModeForwards];
[animation setDelegate:self];
[layer addAnimation:animation forKey:@"animateGradient"];
}
 
- (void)animationDidStop:(CAAnimation *)animation finished:(BOOL)flag {
[self performAnimation];
}

To add the indication of progress, we can use a simple layer mask to block out portions of our gradient. Add the following two properties to your header file:

要添加的进度指示,我们可以用一个简单的图层蒙版来阻挡我们梯度的部分。以下两个属性添加到您的头文件:

@property (nonatomic, readonly) CALayer *maskLayer;
@property (nonatomic, assign) CGFloat progress;

Be sure to @synthesize and then append the following to your initWithFrame:

一定要@synthesize,然后添加以下到您的initWithFrame:方法

maskLayer = [CALayer layer];
[maskLayer setFrame:CGRectMake(0, 0, 0, frame.size.height)];
[maskLayer setBackgroundColor:[[UIColor blackColor] CGColor]];
[layer setMask:maskLayer];

This creates a zero width mask, covering the entire view. The color of the mask doesn’t matter here but it is required to work properly. Now when our progress is updated we want to expand the width of the mask to reflect the value. Override the setProgress: method to contain the following:

这将创建一个零宽度面具,覆盖了整个视图。面膜的肤色并不重要,在这里,但它需要正常工作。现在,当我们的进展被更新我们要扩大掩模的宽度,以反映值。重写setProgress:方法包含以下内容:

- (void)setProgress:(CGFloat)value {
if (progress != value) {
// Progress values go from 0.0 to 1.0
progress = MIN(1.0, fabs(value));
[self setNeedsLayout];
}
}
 
- (void)layoutSubviews {
// Resize our mask layer based on the current progress
CGRect maskRect = [maskLayer frame];
maskRect.size.width = CGRectGetWidth([self bounds]) * progress;
[maskLayer setFrame:maskRect];
}

Now when our progress value is set, we make sure it’s within the 0.0 to 1.0 range and invalidate the layout. Then in the next call to layoutSubviews we resize the mask based on its new value.

That’s it! You can view the entire project on GitHub.

现在,当我们的进展值设置,我们要确保它的0.0至1.0范围内,布局失效。然后到下一次调用layoutSubviews我们调整基于它的新值面具。

而已!您可以查看整个项目GitHub上

Animated progress view with CAGradientLayer(带翻译)<待更新>的更多相关文章

  1. Animated progress view with CAGradientLayer(带翻译)

    Animated progress view with CAGradientLayer(带翻译)  Modern software design is getting flatter and thin ...

  2. App Store Review Guideline(带翻译)

    1. Terms and conditions(法律与条款) 1.1  As a developer of applications for the App Store you are bound b ...

  3. POJ 3784 Running Median (模拟水过带翻译)

    Description Moscow is hosting a major international conference, which is attended by n scientists fr ...

  4. [React Native] Animate Styles of a React Native View with Animated.timing

    In this lesson we will use Animated.timing to animate the opacity and height of a View in our React ...

  5. [翻译] Working with NSURLSession: AFNetworking 2.0

    Working with NSURLSession: AFNetworking 2.0   简单翻译,有很多错误,看官无法理解处请英文原文对照. http://code.tutsplus.com/tu ...

  6. iOS应用架构谈(三):View层的组织和调用方案(下)

    iOS客户端应用架构看似简单,但实际上要考虑的事情不少.本文作者将以系列文章的形式来回答iOS应用架构中的种种问题,本文是其中的第二篇,主要讲View层的组织和调用方案.下篇主要讨论做View层架构的 ...

  7. iOS应用架构谈 view层的组织和调用方案

    当我们开始设计View层的架构时,往往是这个App还没有开始开发,或者这个App已经发过几个版本了,然后此时需要做非常彻底的重构. 一般也就是这两种时机会去做View层架构,基于这个时机的特殊性,我们 ...

  8. 小波说雨燕 第三季 构建 swift UI 之 UI组件集-视图集(六)Picker View视图 学习笔记

    想对PickerView进行操作,只能在代码中操作. 下面 ,再添加三个label组件,然后将所有组件配置到代码中(看代码),然后要实现对PickerView的操作,就要实现它的DataSource协 ...

  9. PresentViewController切换界面(一些系统自带的页面切换动画)

    视图切换,没有NavigationController的情况下,一般会使用presentViewController来切换视图并携带切换时的动画, 其中切换方法如下: – presentViewCon ...

随机推荐

  1. 你不知道的javascript

    一.对象 JavaScript简单类型有数字.字符串.布尔值.null.undefined,其他所有的值都是对象(数组.函数.正则表达式都是对象). 数字.字符串.布尔值虽然拥有方法(包装对象),但并 ...

  2. java io系列01之 "目录"

    java io 系列目录如下: 01. java io系列01之  "目录" 02. java io系列02之 ByteArrayInputStream的简介,源码分析和示例(包括 ...

  3. SharePoint 2013中的爬网最佳做法

    了解在 SharePoint Server 2013 中爬网的最佳做法 搜索系统对内容进行爬网,以构建一个用户可以对其运行搜索查询的搜索索引.本文包含有关如何最有效地管理爬网的建议. 本文内容: 使用 ...

  4. 推荐几款很棒的 JavaScript 表单美化和验证插件

    表单元素让人爱恨交加.作为网页最重要的组成部分,表单几乎无处不在,从简单的邮件订阅.登陆注册到复杂的需要多页填写的信息提交功能,表单都让开发者花费了大量的时间和精力去处理,以期实现好用又漂亮的表单功能 ...

  5. [python]抽象方法

    抽象方法 我的理解抽象方法就是:父类的一个方法,继承的所有子类都必须要实现这个方法,否则报错. 举例说明 class Base(object): def _method(self): raise No ...

  6. sprint3(第四天)

    今天继续完成前台和后台的整合 燃尽图:

  7. [水煮 ASP.NET Web API2 方法论](3-4)设置路由可选项

    问题 怎么样创建一个路由,不管客户端传不传这个参数,都可以被成功匹配. 解决方案 ASP.NET WEB API 的集中式路由和属性路由都支持路由声明可选参数. 在用集中式路由中可以通过 RouteP ...

  8. 【JS复习笔记】02 对象与函数

    好吧,因为很重要的事情,几天没写笔记了. 关于对象: ||可以用来填充默认值,如:myApp.name || "无" &&可以用来避免错误,myApp.NameOb ...

  9. ToDoList:一款非常优秀的任务管理软件 —— 工具类

    ToDoList是一款非常优秀的任务管理软件,用户可以方便地组织和安排计划.这是一个开源的项目,很多细节都考虑到了,推荐大家使用~ ToDoList 帮你把要做的事情列出来,一项一项,类似思维导图. ...

  10. 与众不同 windows phone (49) - 8.1 新增控件: 概述, ContentDialog, MapControl

    [源码下载] 与众不同 windows phone (49) - 8.1 新增控件: 概述, ContentDialog, MapControl 作者:webabcd 介绍与众不同 windows p ...