iOS 漸層進度條

來源:互聯網
上載者:User

標籤:

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

  

    }

}

 

iOS 漸層進度條

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.