iOS蒙板:mask初识

参考:http://www.cnblogs.com/gardenLee/archive/2016/04/09/5371377.html

一、初识

  1. 蒙在上面的板子
  2. 一头蒙(懵)逼
  3. 只是mask的一个应用,还未深入研究(原文作者的那句话:先看脸,再看内涵。不过我还没看内涵..)

二、预览

预览.gif

三、原材料

进度条所需原材料:
  • 材料一:创建灰色的进度条背景,CALayer实现;
  • 材料二:创建渐变的进度条背景覆盖在灰色背景上面,CAGradientLayer实现;
  • 材料三:创建CALayer蒙版对象,作为渐变进度条的蒙版,设置蒙版的borderWidth属性,其值最小为进度条宽度的一半;
实现文字渐变所需原材料:
  • 材料一:在进度条上方创建一个同等长度的渐变背景;
1.png
  • 材料二:创建一个UIButton对象作为材料一的蒙版,未设置button的frame时“材料一”会凭空消失,试了用UILabel对象来做蒙版,但达不到效果

四、动起来

包含三组动画:进度条动画、文字位置动画、文字改变动画

五、代码

控制器.m代码

#import "MaskVC.h"
#import "GradientProcessView.h"
#define SCREEN_WIDTH self.view.frame.size.width
#define kColor(r,g,b) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:1]
@interface MaskVC ()
@property (nonatomic, strong) UITextField *valueTF;
@property (nonatomic, strong) GradientProcessView *mGradientView;
@end
@implementation MaskVC

 - (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    _mGradientView = [[GradientProcessView alloc] initWithFrame:CGRectMake(20, 100, SCREEN_WIDTH-40, 50)];
    [self.view addSubview:_mGradientView];
    
    _valueTF = [[UITextField alloc] initWithFrame:CGRectMake(20, 200, 200, 30)];
    _valueTF.placeholder = @"输入1-100之间的整数";
    _valueTF.keyboardType = UIKeyboardTypeNumberPad;
    _valueTF.borderStyle = UITextBorderStyleBezel;
    [self.view addSubview:_valueTF];
    
    UIButton *stareButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [stareButton setFrame:CGRectMake(20.f, 300.f, SCREEN_WIDTH - 40.f, 38.f)];
    [stareButton addTarget:self action:@selector(onStareButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [stareButton setTitle:@"Star" forState:UIControlStateNormal];
    stareButton.layer.cornerRadius = 4.f;
    stareButton.layer.borderWidth = 0.5;
    stareButton.layer.borderColor = [UIColor lightGrayColor].CGColor;
    [self.view addSubview:stareButton];
    
    UIButton *resetButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [resetButton setFrame:CGRectMake(20.f, 350.f, SCREEN_WIDTH - 40.f, 38.f)];
    [resetButton addTarget:self action:@selector(onResetButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [resetButton setTitle:@"Reset" forState:UIControlStateNormal];
    resetButton.layer.cornerRadius = 4.f;
    resetButton.layer.borderWidth = 0.5;
    resetButton.layer.borderColor = [UIColor lightGrayColor].CGColor;
    [self.view addSubview:resetButton];
}

 - (void)onStareButtonClick {
    [_mGradientView beginAnimationWithValue:_valueTF.text];
}

 - (void)onResetButtonClick {
    [_mGradientView reset];
}

自定义进度条view代码
.h

#import <UIKit/UIKit.h>
@interface GradientProcessView : UIView
 - (instancetype)initWithFrame:(CGRect)frame;
 - (void)beginAnimationWithValue:(NSString *)value;
 - (void)reset;
@end

.m

 #import "GradientProcessView.h"
 #import "UIColor+Extensions.h"

 #define kColor(r,g,b) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:1]
 #define kAnimationDuration 3.0
@interface GradientProcessView()
@property (nonatomic, strong) NSArray *colorArray;
@property (nonatomic, strong) NSArray *colorLocationArray;
@property (nonatomic, strong) CALayer *maskLayer;
@property (nonatomic, strong) CAGradientLayer *gradientLayer;
@property (nonatomic, strong) UIButton *numberMark;
@property (nonatomic, strong) NSTimer *numberChangeTimer;
@property (nonatomic, assign) CGFloat numberPercent;
@property (nonatomic, assign) CGFloat percent;
@end
@implementation GradientProcessView

 - (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor ];
        self.colorArray = @[(id)[[UIColor colorWithHex:0xFF6347] CGColor],
                            (id)[[UIColor colorWithHex:0xFFEC8B] CGColor],
                            (id)[[UIColor colorWithHex:0x98FB98] CGColor],
                            (id)[[UIColor colorWithHex:0x00B2EE] CGColor],
                            (id)[[UIColor colorWithHex:0x9400D3] CGColor]];
        self.colorLocationArray = @[@0.1,@0.3,@0.5,@0.7,@1];
        
        [self getGradientLayer];
        [self setNumberMarkLayer];
    }
    return self;
}
//进度条
 - (void)getGradientLayer {
    //1.灰色的进度条背景,CALayer实现
    CALayer *bgLayer = [CALayer layer];
    bgLayer.frame = CGRectMake(0, 30, self.frame.size.width, 6);
    bgLayer.backgroundColor = kColor(242, 242, 242).CGColor;
    bgLayer.masksToBounds = YES;
    bgLayer.cornerRadius = 3;
    [self.layer addSublayer:bgLayer];
    
    //2.创建一个渐变的进度条背景覆盖在灰色背景上面
    self.gradientLayer = [CAGradientLayer layer];
    self.gradientLayer.frame = bgLayer.frame;
    self.gradientLayer.masksToBounds = YES;
    self.gradientLayer.cornerRadius = 3;
    [self.gradientLayer setColors:self.colorArray];
    [self.gradientLayer setLocations:self.colorLocationArray];
    [self.gradientLayer setStartPoint:CGPointMake(0, 0)];
    [self.gradientLayer setEndPoint:CGPointMake(1, 0)];
    
    //3. 创建一个CALayer蒙版对象,作为渐变进度条的蒙版;
    self.maskLayer = [CALayer layer];
    self.maskLayer.frame = CGRectMake(0, 0, 0, 6);
    self.maskLayer.borderWidth = 3;
    
    [self.gradientLayer setMask:self.maskLayer];
    [self.layer addSublayer:self.gradientLayer];
}

 - (void)setNumberMarkLayer {
    //4. 在进度条上方创建一个同等长度的渐变背景
    CAGradientLayer *numberGradientLayer = [CAGradientLayer layer];
    [numberGradientLayer setFrame:CGRectMake(0, 10, self.frame.size.width, 15)];
    [numberGradientLayer setColors:self.colorArray];
    [numberGradientLayer setLocations:self.colorLocationArray];
    [numberGradientLayer setStartPoint:CGPointMake(0, 0)];
    [numberGradientLayer setEndPoint:CGPointMake(1, 0)];
    [self.layer addSublayer:numberGradientLayer];
    
    //5. 创建一个UIButton对象作为蒙版
    _numberMark = [UIButton buttonWithType:UIButtonTypeCustom];
    [_numberMark setTitle:@"0.0" forState:UIControlStateNormal];
    [_numberMark setTitleColor:[UIColor colorWithHex:0xFF6347] forState:UIControlStateNormal];
    _numberMark.titleLabel.font = [UIFont systemFontOfSize:13.f];
    _numberMark.enabled = NO;
    
    [numberGradientLayer setMask:self.numberMark.layer];
}
 - (void)circleAnimation {
    [CATransaction begin];
    [CATransaction setDisableActions:NO];
    [CATransaction setAnimationTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
    [CATransaction setAnimationDuration:kAnimationDuration];
    self.maskLayer.frame = CGRectMake(0, 0, self.frame.size.width * _percent / 100, 6);
    [CATransaction commit];
}
 - (void)beginAnimationWithValue:(NSString *)value {
    _numberPercent = 0;
    _percent = value.floatValue;
    //进度条动画
    [self circleAnimation];
    //文字位置动画
    __weak typeof (self)weakSelf = self;
    [UIView animateWithDuration:kAnimationDuration delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
        weakSelf.numberMark.frame = CGRectMake(self.frame.size.width * _percent / 100 -30, 0, 30, 15);
    } completion:nil];
    //文字改变动画
    self.numberChangeTimer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(changeNumber) userInfo:nil repeats:YES];
}
 - (void)changeNumber {
    self.numberPercent += (CGFloat)_percent/(kAnimationDuration*10);
    if (self.numberPercent > _percent) {
        [self.numberChangeTimer invalidate];
        self.numberChangeTimer = nil;
        [self.numberMark setTitle:[NSString stringWithFormat:@"%ld",(long)_percent] forState:UIControlStateNormal];
        _numberPercent = 0;
        return;
    }
    [self.numberMark setTitle:[NSString stringWithFormat:@"%ld",(long)self.numberPercent] forState:UIControlStateNormal];
}
 - (void)reset {
    _percent = 0;
    [_maskLayer setFrame:CGRectMake(0, 0, 0, 6)];
    [_numberMark setFrame:CGRectMake(0, 0, 30, 15)];
    [_numberMark setTitle:@"0" forState:UIControlStateNormal];
}
@end

UIColor+Extensions

 + (UIColor *)colorWithHex:(long)hexColor {
    CGFloat red = ((CGFloat)((hexColor & 0xFF0000) >> 16))/255.0f;
    CGFloat green = ((CGFloat)((hexColor & 0xFF00) >> 8))/255.0f;
    CGFloat blue = ((CGFloat)(hexColor & 0xFF))/255.0f;
    return [UIColor colorWithRed:red green:green blue:blue alpha:1.0f];
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 203,547评论 6 477
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,399评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 150,428评论 0 337
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,599评论 1 274
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,612评论 5 365
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,577评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,941评论 3 395
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,603评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,852评论 1 297
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,605评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,693评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,375评论 4 318
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,955评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,936评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,172评论 1 259
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 43,970评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,414评论 2 342

推荐阅读更多精彩内容