1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

グラデーションをラップした (My Graduation Tips.)

Posted at

かなり面倒くさかったので、



#import "Paint.h"

@implementation Paint
{
    CAGradientLayer *grad;
}

-(id)init
{
    self = [super init];
    grad = [CAGradientLayer layer];
    return self;
    
    
}


-(void)gradWith:(UIView *)target from:(UIColor *)c1 to:(UIColor *)c2
{
    // if grad already exist then remove this. 
    if (grad){ 
        [grad removeFromSuperlayer];
    }
    
    
    grad.frame = target.bounds;
    
    grad.colors = [NSArray arrayWithObjects:
                       (id)c1.CGColor,
                       (id)c2.CGColor,
                       nil];
    [target.layer insertSublayer:grad atIndex:0];
    
}

-(void)gradWith:(UIView *)target by:(NSMutableArray *)colors
{
    if (grad){
        [grad removeFromSuperlayer];
    }
    
    grad.frame = target.bounds;
    
    NSMutableArray *res = [NSMutableArray array];
    for (id color in (NSMutableArray *)colors){
        [res addObject:(id)[color CGColor]];
    }
    
    grad.colors = [NSArray arrayWithArray:res];
    [target.layer insertSublayer:grad atIndex:0];
    
}


こんなふうなクラスを書いて、


        [paint gradWith:_paintedLabel by:[NSArray arrayWithObjects:[UIColor redColor],[UIColor yellowColor], nil]];


[paint gradWith:_paintedLabel from:[UIColor colorWithRed:0.0 green:0.3 blue:1.0 alpha:1.0] to:[UIColor colorWithRed:0.0 green:0.7 blue:0.933 alpha:1.0]];

こんなふうに呼び出すようにしてみた。やっつけだけど、頑張って働いてくれています。

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?