美文网首页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属性

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