国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > iOS > 正文

ios layer.mask

2019-11-09 16:51:06
字體:
來源:轉載
供稿:網友

二、文字漸變實現思路:

1.創建一個顏色漸變層,漸變圖層跟文字控件一樣大。

2.用文字圖層裁剪漸變層,只保留文字部分,就會讓漸變層只保留有文字的部分,相當于間接讓漸變層顯示文字,我們看到的其實是被裁剪過后,漸變層的部分內容。

注意:如果用文字圖層裁剪漸變層,文字圖層就不在擁有顯示功能,這個圖層就被弄來裁剪了,不會顯示,在下面代碼中也會有說明。

2.1 創建一個帶有文字的label,label能顯示文字。

2.2 設置漸變圖層的mask為label圖層,就能用文字裁剪漸變圖層了。

3.mask圖層工作原理:

1.根據透明度進行裁剪,只保留非透明部分,顯示底部內容。

4.詳細代碼+解釋

// 創建UILabel    UILabel *label = [[YZLabel alloc] init];     label.text = @"小碼哥,專注于高級iOS開發工程師的培養";     [label sizeToFit];     label.center = CGPointMake(200, 100);     // 疑問:label只是用來做文字裁剪,能否不添加到view上。    // 必須要把Label添加到view上,如果不添加到view上,label的圖層就不會調用drawRect方法繪制文字,也就沒有文字裁剪了。    // 如何驗證,自定義Label,重寫drawRect方法,看是否調用,發現不添加上去,就不會調用    [self.view addSubview:label];     // 創建漸變層    CAGradientLayer *gradientLayer = [CAGradientLayer layer];     gradientLayer.frame = label.frame;     // 設置漸變層的顏色,隨機顏色漸變    gradientLayer.colors = @[(id)[self randomColor].CGColor, (id)[self randomColor].CGColor,(id)[self randomColor].CGColor];     // 疑問:漸變層能不能加在label上    // 不能,mask原理:默認會顯示mask層底部的內容,如果漸變層放在mask層上,就不會顯示了     // 添加漸變層到控制器的view圖層上    [self.view.layer addSublayer:gradientLayer];     // mask層工作原理:按照透明度裁剪,只保留非透明部分,文字就是非透明的,因此除了文字,其他都被裁剪掉,這樣就只會顯示文字下面漸變層的內容,相當于留了文字的區域,讓漸變層去填充文字的顏色。    // 設置漸變層的裁剪層    gradientLayer.mask = label.layer;     // 注意:一旦把label層設置為mask層,label層就不能顯示了,會直接從父層中移除,然后作為漸變層的mask層,且label層的父層會指向漸變層,這樣做的目的:以漸變層為坐標系,方便計算裁剪區域,如果以其他層為坐標系,還需要做點的轉換,需要把別的坐標系上的點,轉換成自己坐標系上點,判斷當前點在不在裁剪范圍內,比較麻煩。      // 父層改了,坐標系也就改了,需要重新設置label的位置,才能正確的設置裁剪區域。

    label.frame = gradientLayer.bounds;

#import "LXGradientPRocessView.h"#import "UIView+Extensions.h"#import "UIColor+Extensions.h"static const CGFloat kProcessHeight = 10.f;static const CGFloat kTopSpaces = 5.f;static const CGFloat kNumberMarkWidth = 60.f;static const CGFloat kNumberMarkHeight = 20.f;static const CGFloat kAnimationTime = 3.f;@interface LXGradientProcessView ()@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, strong) NSArray *colorArray;@property (nonatomic, strong) NSArray *colorLocationArray;@end@implementation LXGradientProcessView- (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.numberMark.frame = CGRectMake(0, kTopSpaces, kNumberMarkWidth, kNumberMarkHeight);        [self addSubview:self.numberMark];        [self setNUmberMarkLayer];        [self getGradientLayer];        self.numberPercent = 0;    }    return self;}- (void)setNUmberMarkLayer { // 提示文字設置漸變色        CAGradientLayer *numberGradientLayer = [CAGradientLayer layer];    numberGradientLayer.frame = CGRectMake(0, kTopSpaces, self.width, kNumberMarkHeight);    [numberGradientLayer setColors:self.colorArray];    [numberGradientLayer setLocations:self.colorLocationArray];    [numberGradientLayer setStartPoint:CGPointMake(0, 0)];    [numberGradientLayer setEndPoint:CGPointMake(1, 0)];    [self.layer addSublayer:numberGradientLayer];    [numberGradientLayer setMask:self.numberMark.layer];    self.numberMark.frame = numberGradientLayer.bounds;        NSLog(@"numberMark = %@ numberGradientLayer = %@ ",NSStringFromCGRect(self.numberMark.frame),NSStringFromCGRect(numberGradientLayer.frame));}- (void)getGradientLayer{ // 進度條設置漸變色        // 灰色進度條背景    CALayer *bgLayer = [CALayer layer];    bgLayer.frame = CGRectMake(kNumberMarkWidth / 2, self.height - kProcessHeight - kTopSpaces, self.width - kNumberMarkWidth / 2, kProcessHeight);    bgLayer.backgroundColor = [UIColor colorWithHex:0xF5F5F5].CGColor;    bgLayer.masksToBounds = YES;    bgLayer.cornerRadius = kProcessHeight / 2;    [self.layer addSublayer:bgLayer];        self.maskLayer = [CALayer layer];    self.maskLayer.frame = CGRectMake(0, 0, (self.width - kNumberMarkWidth / 2) * self.percent / 100.f, kProcessHeight);    self.maskLayer.borderWidth = self.height / 2;        self.gradientLayer =  [CAGradientLayer layer];    self.gradientLayer.frame = CGRectMake(kNumberMarkWidth / 2, self.height - kProcessHeight - kTopSpaces, self.width - kNumberMarkWidth / 2, kProcessHeight);    self.gradientLayer.masksToBounds = YES;    self.gradientLayer.cornerRadius = kProcessHeight / 2;    [self.gradientLayer setColors:self.colorArray];    [self.gradientLayer setLocations:self.colorLocationArray];    [self.gradientLayer setStartPoint:CGPointMake(0, 0)];    [self.gradientLayer setEndPoint:CGPointMake(1, 0)];    [self.gradientLayer setMask:self.maskLayer];    [self.layer addSublayer:self.gradientLayer];        NSLog(@"maskLayer = %@ gradientLayer = %@  bgLayer = %@",NSStringFromCGRect(self.maskLayer.frame),NSStringFromCGRect(self.gradientLayer.frame),NSStringFromCGRect(bgLayer.frame));}- (void)setPercent:(CGFloat)percent {    [self setPercent:percent animated:YES];}- (void)setPercent:(CGFloat)percent animated:(BOOL)animated {        _percent = percent;    [NSTimer scheduledTimerWithTimeInterval:0 target:self selector:@selector(circleAnimation) userInfo:nil repeats:NO];    // 文字動畫    __weak typeof(self)weakSelf = self;    [UIView animateWithDuration:kAnimationTime animations:^{        weakSelf.numberMark.frame = CGRectMake((weakSelf.width - kNumberMarkWidth) * percent / 100, 0, kNumberMarkWidth, kNumberMarkHeight);        //        [weakSelf.numberMark setTitle:[NSString stringWithFormat:@"%.1f分",percent / 20.f] forState:UIControlStateNormal];    }];        self.numberChangeTimer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(changeNumber) userInfo:nil repeats:YES];}- (void)changeNumber { // 每0.1秒改變百分比文字        if (!self.percent) {        [self.numberChangeTimer invalidate];        self.numberChangeTimer = nil;    }    self.numberPercent += (self.percent / (kAnimationTime * 10.f));    if (self.numberPercent > self.percent) {        [self.numberChangeTimer invalidate];        self.numberChangeTimer = nil;        self.numberPercent = self.percent;    }    [self.numberMark setTitle:[NSString stringWithFormat:@"%.1f",self.numberPercent] forState:UIControlStateNormal];}- (void)circleAnimation { // 進度條動畫        [CATransaction begin];    [CATransaction setDisableActions:NO];    [CATransaction setAnimationTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaSEOut]];    [CATransaction setAnimationDuration:kAnimationTime];    self.maskLayer.frame = CGRectMake(0, 0, (self.width - kNumberMarkWidth / 2) * _percent / 100.f, kProcessHeight);    [CATransaction commit];}- (UIButton *)numberMark {        if (nil == _numberMark) {        _numberMark = [UIButton buttonWithType:UIButtonTypeCustom];        [_numberMark setTitle:@"0分" forState:UIControlStateNormal];        [_numberMark setTitleColor:[UIColor colorWithHex:0xFF6347] forState:UIControlStateNormal];        [_numberMark setBackgroundImage:[UIImage imageNamed:@"user_score_bubble"] forState:UIControlStateNormal];        _numberMark.titleLabel.font = [UIFont systemFontOfSize:13.f];        _numberMark.enabled = NO;    }    return _numberMark;}

    // 漸變進度條    self.processView = [[LXGradientProcessView alloc] initWithFrame:CGRectMake(20.f, 100.f, SCREEN_WIDTH - 40.f, 45.f)];    self.processView.percent = 0;    [self.view addSubview:self.processView];        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:@"Stare" forState:UIControlStateNormal];    stareButton.layer.cornerRadius = 4.f;    stareButton.layer.borderWidth = 0.5;    stareButton.layer.borderColor = [UIColor lightGrayColor].CGColor;    [self.view addSubview:stareButton];    

//鏤空

- (void)addMask{    UIButton * _maskButton = [[UIButton alloc] init];    [_maskButton setFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];    [_maskButton setBackgroundColor:[UIColor colorWithWhite:0 alpha:0.7]];    [self.view addSubview:_maskButton];        //create path    UIBezierPath *path = [UIBezierPath bezierPathWithRect:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];        // MARK: circlePath    [path appendPath:[UIBezierPath bezierPathWithArcCenter:CGPointMake(SCREEN_WIDTH / 2, 200) radius:100 startAngle:0 endAngle:2*M_PI clockwise:NO]];        // MARK: roundRectanglePath    [path appendPath:[[UIBezierPath bezierPathWithRoundedRect:CGRectMake(20, 400, SCREEN_WIDTH - 22 * 20, 100) cornerRadius:15] bezierPathByReversingPath]];        CAShapeLayer *shapeLayer = [CAShapeLayer layer];        shapeLayer.path = path.CGPath;        [_maskButton.layer setMask:shapeLayer];        maskLayer = shapeLayer;}//文字變色

        UILabel *_numberMark = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 10, 50)];    _numberMark.textAlignment = NSTextAlignmentLeft;    _numberMark.text = @"aa";        textLabel = _numberMark;        CAGradientLayer *colorLayer = [CAGradientLayer layer];    colorLayer.frame    = CGRectMake(0, 0, 300, 50);    // 顏色分配    colorLayer.colors = @[(__bridge id)[UIColor redColor].CGColor,                          (__bridge id)[UIColor greenColor].CGColor,                          (__bridge id)[UIColor blueColor].CGColor,                          (__bridge id)[UIColor redColor].CGColor];    // 顏色分割線    colorLayer.locations  = @[@(0.25), @(0.5), @(0.75)];    // 起始點    colorLayer.startPoint = CGPointMake(0, 0);    // 結束點    colorLayer.endPoint   = CGPointMake(1, 0);        [self.view.layer addSublayer:colorLayer];        colorLayer.mask = _numberMark.layer;

//繪制wifi圖標

/1?;《绒D角度#define pi 3.14159265359#define RADIANS_TO_DEGREES(radians) ((radians) * (180.0 / M_PI))//2。角度轉弧度// Degrees to radians#define pi 3.14159265359#define   DEGREES_TO_RADIANS(degrees)  ((pi * degrees)/ 180)@implementation LTWiFiScanView- (id)initWithFrame:(CGRect)frame{    if (self = [super initWithFrame:frame]) {            }    return self;}// Only override drawRect: if you perform custom drawing.// An empty implementation adversely affects performance during animation.- (void)drawRect:(CGRect)rect {    // Drawing code        CALayer *superLayer = [CALayer layer];    superLayer.frame = self.frame;        CAShapeLayer *layer = [CAShapeLayer layer];    layer.frame = CGRectMake(0, 0, rect.size.width/6, rect.size.height/6);    UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(150,151) radius:2.5f startAngle:DEGREES_TO_RADIANS(220) endAngle:DEGREES_TO_RADIANS(320) clockwise:YES];    layer.path = path.CGPath;    layer.lineWidth = 5;    layer.strokeColor = [UIColor redColor].CGColor;    layer.fillColor = [UIColor blueColor].CGColor;        CAKeyframeAnimation *opacityAnimation = [CAKeyframeAnimation animation];    opacityAnimation.keyPath = @"opacity";    opacityAnimation.values = @[@1.0,@0.75,@0.5,@0.25,@0.0];    opacityAnimation.keyTimes = @[@0.0,@0.25,@0.5,@0.75,@1];    opacityAnimation.fillMode = kCAFillModeBoth;    opacityAnimation.duration = 1;    opacityAnimation.beginTime  = CACurrentMediaTime() + (float) 0 * 1.5 / 3;    opacityAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];    opacityAnimation.autoreverses = NO;    opacityAnimation.removedOnCompletion = NO;    opacityAnimation.repeatCount = HUGE_VALF;    //    [layer addAnimation:opacityAnimation forKey:@"op"];    [superLayer addSublayer:layer];            CAShapeLayer *layer1 = [CAShapeLayer layer];    layer.frame = CGRectMake(0, 0, rect.size.width/6, rect.size.height/6);    UIBezierPath *path1 = [UIBezierPath bezierPathWithArcCenter:CGPointMake(150,150) radius:10 startAngle:DEGREES_TO_RADIANS(220) endAngle:DEGREES_TO_RADIANS(320) clockwise:YES];    layer1.path = path1.CGPath;    layer1.lineWidth = 5;    layer1.strokeColor = [UIColor blueColor].CGColor;    layer1.fillColor = [UIColor clearColor].CGColor;        CAKeyframeAnimation *opacityAnimation1 = [CAKeyframeAnimation animation];    opacityAnimation1.keyPath = @"opacity";    opacityAnimation1.values = @[@1.0,@0.75,@0.5,@0.25,@0.0];    opacityAnimation1.keyTimes = @[@0.0,@0.25,@0.5,@0.75,@1];    opacityAnimation1.fillMode = kCAFillModeBoth;    opacityAnimation1.duration = 1;    opacityAnimation1.beginTime  = CACurrentMediaTime() + (float) 1 * 1.5 / 3;    opacityAnimation1.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];    opacityAnimation1.autoreverses = NO;    opacityAnimation1.removedOnCompletion = NO;    opacityAnimation1.repeatCount = HUGE_VALF;//    [layer1 addAnimation:opacityAnimation1 forKey:@"op"];        [superLayer addSublayer:layer1];            CAShapeLayer *layer2 = [CAShapeLayer layer];    layer2.frame = CGRectMake(0, 0, rect.size.width/6, rect.size.height/6);    UIBezierPath *path2 = [UIBezierPath bezierPathWithArcCenter:CGPointMake(150,150) radius:20 startAngle:DEGREES_TO_RADIANS(220) endAngle:DEGREES_TO_RADIANS(320) clockwise:YES];    layer2.path = path2.CGPath;    layer2.lineWidth = 5;    layer2.strokeColor = [UIColor blueColor].CGColor;    layer2.fillColor = [UIColor clearColor].CGColor;        CAKeyframeAnimation *opacityAnimation2 = [CAKeyframeAnimation animation];    opacityAnimation2.keyPath = @"opacity";    opacityAnimation2.values = @[@1.0,@0.75,@0.5,@0.25,@0.0];    opacityAnimation2.keyTimes = @[@0.0,@0.25,@0.5,@0.75,@1];    opacityAnimation2.fillMode = kCAFillModeBoth;    opacityAnimation2.duration = 1;    opacityAnimation2.beginTime  = CACurrentMediaTime() + (float) 2 * 1.5 / 3;    opacityAnimation2.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];    opacityAnimation2.autoreverses = NO;    opacityAnimation2.removedOnCompletion = NO;    opacityAnimation2.repeatCount = HUGE_VALF;//    [layer2 addAnimation:opacityAnimation2 forKey:@"op"];        [superLayer addSublayer:layer2];        [self.layer addSublayer:superLayer];


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 乐陵市| 天等县| 成都市| 普陀区| 固阳县| 东安县| 陕西省| 自治县| 法库县| 双峰县| 施秉县| 叶城县| 遂昌县| 清徐县| 昆明市| 丹江口市| 佛冈县| 内黄县| 巴林左旗| 阿克陶县| 嘉义市| 惠安县| 乃东县| 张家口市| 邵东县| 囊谦县| 前郭尔| 板桥市| 惠来县| 双鸭山市| 通州市| 通州区| 韩城市| 兴宁市| 象山县| 友谊县| 汶上县| 三原县| 峨边| 桑日县| 茂名市|