zoukankan      html  css  js  c++  java
  • iOS 渐变进度条

    #import <UIKit/UIKit.h>

    @interface JianBianView : UIView

    //为了增加一个表示进度条的进行,可们可以使用mask属性来屏蔽一部分

    @property (nonatomic, strong) CALayer *maskLayer;

    @property (nonatomic, assign) CGFloat progress;

    //动画方法

    -(void)performAnimation;

    -(void)setProgress:(CGFloat)value;

    @end

    #import "JianBianView.h"

    @implementation JianBianView

    @synthesize maskLayer,progress;

    +(Class)layerClass

    {

        //设置默认是 CAGradientLayer

        return [CAGradientLayer class];

    }

    -(id)initWithFrame:(CGRect)frame

    {

        self = [super initWithFrame:frame];

        if (self)

        {

            maskLayer = [CALayer layer];

            [maskLayer setFrame:CGRectMake(0, 0, 0, frame.size.height)];

            [maskLayer setBackgroundColor:[UIColor blackColor].CGColor];

            

            CAGradientLayer *layer = (id)[self layer];

            [layer setStartPoint:CGPointMake(0.0, 0.5)];

            [layer setEndPoint:CGPointMake(1.0, 0.5)];

            

            NSMutableArray *colors = [[NSMutableArray alloc] init];

            for (NSInteger hue = 0; hue < 360; hue += 5)

            {

                UIColor *color;

                //hue 色调 saturation 饱和度 brightness 亮度

                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]];

            [layer setMask:maskLayer];

        }

        return self;

    }

    //创建一个宽度为0的mask覆盖整个View,mask的颜色不重要,当我们progress属性更新的时候我们会增加它的宽度 然后在initWithFrame:里面添加:

    /*

     maskLayer = [CALayer layer];

     [maskLayer setFrame:CGRectMake(0, 0, 0, frame.size.height)];

     [maskLayer setBackgroundColor:[UIColor blackColor].CGColor];

     */

    //所以重写setProgress:

    -(void)setProgress:(CGFloat)value

    {

        if (progress != value)

        {

            progress = MIN(1.0, fabs(value));

            [self setNeedsLayout];

        }

    }

    -(void)layoutSubviews

    {

        CGRect maskRect = [maskLayer frame];

        maskRect.size.width = CGRectGetWidth([self bounds]) * progress;

        [maskLayer setFrame:maskRect];

    }

    -(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 *shiftColors = [NSArray arrayWithArray:mutable];

        [layer setColors:shiftColors];

        

        CABasicAnimation *animation;

        animation = [CABasicAnimation animationWithKeyPath:@"Colors"];

        [animation setToValue:shiftColors];

        [animation setDuration:0.08];

        [animation setRemovedOnCompletion:YES];

        [animation setFillMode:kCAFillModeForwards];

        [animation setDelegate:self];

        [layer addAnimation:animation forKey:@""];

        

    }

    -(void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag

    {

        [self performAnimation];

    }

    @end

     视图控制器 调用

    - (void)viewDidLoad

    {

        [super viewDidLoad];

        // Do any additional setup after loading the view, typically from a nib.

        self.title = @"渐变测试";

        self.view.backgroundColor = [UIColor whiteColor];

        

    //    [self jianBianMethord];

        [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timeMethod) userInfo:nil repeats:YES];

        

        [self addJianBianView];

    }

    -(void)timeMethod

    {

        NSLog(@"进入");

        progress += 0.1;

        [self.jianBianView setProgress:progress];

    }

    //-----------添加渐变view

    -(void)addJianBianView

    {

        if (self.jianBianView == nil)

        {

            self.jianBianView = [[JianBianView alloc] initWithFrame:CGRectMake(0, 100, self.view.frame.size.width, 10)];

            [self.view addSubview:self.jianBianView];

            [self.jianBianView performAnimation];

      

        }

    }

  • 相关阅读:
    vscode Git提交
    安装 nrm
    vue+element ui项目总结点(六)table编辑当前行、删除当前行、新增、合计操作
    Filebrowser安装教程
    vue数据处理:把数组处理成适用于tree组件的数据
    WC2016模拟Divisor
    对拍模板
    题解(5031. 【NOI2017模拟3.27】B)(数论,组合数学)
    Codeforces #657 Div2C choosing flowers
    关于一个大菜鸡的记录
  • 原文地址:https://www.cnblogs.com/camillezlh/p/4801413.html
Copyright © 2011-2022 走看看