iOS 过渡动画的实现
来源:互联网 发布:视频播放加速软件 编辑:程序博客网 时间:2024/05/16 06:15
//// ViewController.m// test_CAGradientLayer//// Created by admin on 3/4/16.// Copyright © 2016 jeffasd. All rights reserved.//#import "ViewController.h"#import "GardView.h"@interface ViewController ()@end@implementation ViewController- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. GardView *view = [[GardView alloc] initWithFrame:CGRectMake(100, 300, 500, 50)];// view.frame = CGRectMake(100, 30, 50, 50); [self.view addSubview:view]; [view setProgress:0.6]; [view performAnimation]; }- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}@end
//// GardView.h// test_CAGradientLayer//// Created by admin on 3/4/16.// Copyright © 2016 jeffasd. All rights reserved.//#import <UIKit/UIKit.h>@interface GardView : UIView- (void)setProgress:(CGFloat)value;- (void)performAnimation;@end
//// GardView.m// test_CAGradientLayer//// Created by admin on 3/4/16.// Copyright © 2016 jeffasd. All rights reserved.//#import "GardView.h"@interface GardView()@property (nonatomic, strong) CALayer *maskLayer;@property (nonatomic, assign) CGFloat progress;@end@implementation GardView+ (Class)layerClass { return [CAGradientLayer class];}//// 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]];- (instancetype)initWithFrame:(CGRect)frame{ self = [super initWithFrame:frame]; if (self != nil) { _maskLayer = [CALayer layer]; [_maskLayer setFrame:CGRectMake(0, 0, 0, frame.size.height)]; [_maskLayer setBackgroundColor:[[UIColor blackColor] CGColor]]; [self.layer setMask:_maskLayer]; // 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]]; } return self;}- (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] ; [mutable removeLastObject]; [mutable insertObject:lastColor atIndex:0]; NSArray *shiftedColors = [NSArray arrayWithArray:mutable]; // 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];}- (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];}@end
参考文章:
http://www.cocoachina.com/industry/20140705/9039.html
0 0
- iOS 过渡动画的实现
- 自定义iOS的过渡动画的效果
- FLIP 动画实现多维网格的过渡
- 用css3的transition实现过渡动画
- iOS动画 过渡/转场动画的简单应用
- [java]过渡动画实现
- iOS学习之——自定义过渡动画的实现和使用
- IOS在控制器间跳转实现过渡动画
- iOS 在控制器间跳转实现过渡动画
- iOS 在控制器间跳转实现过渡动画
- IOS页面切换过渡动画
- ViewPager的过渡动画
- Activity过渡的动画
- Activity的过渡动画
- 过渡动画的使用
- iOS开发中CATransition过渡动画的类型
- vue过渡动画实现原理
- [开源]实现顺滑过渡动画的LoadingView
- javabean中的boolean 类型属性命名
- 理解一下ORACLE数据库
- HDOJ--5595--GTW likes math
- (三)JFreeChar 饼状图
- UIBezierPath精讲
- iOS 过渡动画的实现
- 添加zabbix客户端
- Web Browse Process
- Xp 下使用 Fiddler 抓取android手机包
- ADB命令的使用
- 控制应用访问权限
- 函数与程序结构【重学C之四】
- 换行符号(\n)放到Android当中的TextView显示双斜杠(\\n)
- Oracle和Sql 日期格式与字符串格式相互转换