iOS淺談UIView的maskView屬性

使用maskView來實(shí)現(xiàn)下面的效果:


效果圖
首先我們要知道,maskView的定義,這是一個(gè)視圖的遮罩視圖,要實(shí)現(xiàn)以上的效果需要和CAGradientLayer配合使用,話不多說,直接上代碼.
為了方便使用,對(duì)這種效果進(jìn)行了封裝,類名為L(zhǎng)ayerContentView,繼承于UIView.

LayerContentView

LayerContentView.h文件
#import <UIKit/UIKit.h>
@interface LayerContentView : UIView
@property (nonatomic, strong) UILabel *label;
//遮罩視圖
@property (nonatomic, strong) UIView *contentMaskView;
@property (nonatomic, strong) NSString *text;
//向右動(dòng)畫
- (void)fadeRight;
@end
LayerContentView.m文件
#import "LayerContentView.h"

@implementation LayerContentView
@synthesize text = _text;

- (instancetype)initWithFrame:(CGRect)frame {
    if ([super initWithFrame:frame]) {
        //創(chuàng)建label
        [self creatLabel:self.bounds];
        //創(chuàng)建遮罩圖層
        [self creatMaskView:self.bounds];
    }
    return self;
}

- (void)creatLabel:(CGRect)frame {
    _label = [[UILabel alloc] initWithFrame:frame];
    _label.font = [UIFont boldSystemFontOfSize:20];
    _label.textColor = [UIColor redColor];
    _label.textAlignment = NSTextAlignmentCenter;
    [self addSubview:_label];
}

- (void)creatMaskView:(CGRect)frame {
    _contentMaskView = [[UIView alloc] initWithFrame:frame];
    //首先要?jiǎng)?chuàng)建出漸變的layer(核心代碼)
    CAGradientLayer *gradientLayer = [CAGradientLayer layer];
    gradientLayer.frame = frame;
    gradientLayer.colors = @[(__bridge id)[UIColor clearColor].CGColor, (__bridge id)[UIColor blackColor].CGColor, (__bridge id)[UIColor clearColor].CGColor];
    gradientLayer.locations = @[@(0.01), @(0.1), @(0.9), @(0.99)];
    gradientLayer.startPoint = CGPointMake(0, 0);
    gradientLayer.endPoint = CGPointMake(1, 0);
    //將gradientLayer添加到我們的遮罩的view的layer上
    [_contentMaskView.layer addSublayer:gradientLayer];
    //設(shè)置self的遮罩視圖為_contentMaskView
    self.maskView = _contentMaskView;
}

//動(dòng)畫
- (void)fadeRight {
    [UIView animateWithDuration:2.0 delay:0.0 options:UIViewAnimationOptionCurveEaseOut animations:^{
        CGRect frame = _contentMaskView.frame;
        frame.origin.x += frame.size.width;
        _contentMaskView.frame = frame;
    } completion:^(BOOL finished) {
        NSLog(@"完成");
        //不需要循環(huán)動(dòng)畫的可以將finished的block中的代碼注釋掉(也就是下面的代碼)
        [UIView animateWithDuration:2.0 delay:0.0 options:UIViewAnimationOptionCurveEaseOut animations:^{
            CGRect frame = _contentMaskView.frame;
            frame.origin.x -= frame.size.width;
            _contentMaskView.frame = frame;
        } completion:^(BOOL finished) {
            [self fadeRight];
        }];
    }];
}

- (void)setText:(NSString *)text {
    _text = text;
    _label.text = text;
}

- (NSString *)text {
    return _text;
}
@end
//控制器中的調(diào)用
#import "ThirdViewController.h"
#import "LayerContentView.h"

@interface ThirdViewController ()
@property (nonatomic, strong) LayerContentView *layerContentView;
@end

@implementation ThirdViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"maskView";
    self.view.backgroundColor = [UIColor whiteColor];
    _layerContentView = [[LayerContentView alloc] initWithFrame:CGRectMake(0, 200, iPhoneWidth, 30)];
    _layerContentView.text = @"西河老伯iOS開發(fā)";
    [self.view addSubview:_layerContentView];
    //設(shè)置動(dòng)畫開始
    [_layerContentView fadeRight];
}

如果你感覺對(duì)你有幫助,請(qǐng)留言或者關(guān)注我的微信公眾號(hào)//西河老伯iOS開發(fā)//來支持我!

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容