美文网首页iOS开发
iOS浅谈UIView的maskView属性

iOS浅谈UIView的maskView属性

作者: 西河老伯 | 来源:发表于2016-11-30 16:35 被阅读1433次

使用maskView来实现下面的效果:


效果图
首先我们要知道,maskView的定义,这是一个视图的遮罩视图,要实现以上的效果需要和CAGradientLayer配合使用,话不多说,直接上代码.
为了方便使用,对这种效果进行了封装,类名为LayerContentView,继承于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;
//向右动画
- (void)fadeRight;
@end
LayerContentView.m文件
#import "LayerContentView.h"

@implementation LayerContentView
@synthesize text = _text;

- (instancetype)initWithFrame:(CGRect)frame {
    if ([super initWithFrame:frame]) {
        //创建label
        [self creatLabel:self.bounds];
        //创建遮罩图层
        [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];
    //首先要创建出渐变的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];
    //设置self的遮罩视图为_contentMaskView
    self.maskView = _contentMaskView;
}

//动画
- (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(@"完成");
        //不需要循环动画的可以将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
//控制器中的调用
#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开发";
    [self.view addSubview:_layerContentView];
    //设置动画开始
    [_layerContentView fadeRight];
}

如果你感觉对你有帮助,请留言或者关注我的微信公众号//西河老伯iOS开发//来支持我!

相关文章

  • iOS浅谈UIView的maskView属性

    使用maskView来实现下面的效果: 效果图首先我们要知道,maskView的定义,这是一个视图的遮罩视图,要实...

  • MaskView

    UIView在iOS 8.0之后新增的属性maskView的alpha=1时显示view的部分,alpha=0时不...

  • UIView之maskView使用

    UIView中有一个maskView属性,这个属性在iOS8之后开始使用,用来表示视图的遮罩。类比于CALayer...

  • UIView的 maskView 属性

    UIView中有一个maskView属性,这个属性在iOS8之后开始使用,用来表示视图的遮罩。类比于CALayer...

  • 绘图-视图遮罩MaskView的使用

    在UIView中有一个maskView属性,我们可以利用这个属性很方便的做出一些有意思的效果 这个属性在iOS8之...

  • Mask动画深入学习

    请下载Demo CALayer有一个属性叫做mask(对应UIView中maskView属性,下文说的maskVi...

  • sweet笔记_UIView的transform属性

    iOS开发UIView的transform属性详解 本文主要是详解iOS开发UIView的transform属性,...

  • MaskAnimation动画

    demo地址 Mask基本知识 在UIView中有一个maskView属性,我们可以利用这个属性很方便的做出一些有...

  • 两个动画来理解maskView

    属性说明 maskView是UIView的一个属性,对应的CALayer也有一个mask,他们两个的作用是一样的,...

  • maskView详解及基本使用

    UIView有个maskView属性(与layer.mask 使用一样),最近研究了一下,在此分享下。 官方定义:...

网友评论

    本文标题:iOS浅谈UIView的maskView属性

    本文链接:https://www.haomeiwen.com/subject/axetmttx.html