maskview ios android,UIView的 maskView 属性

UIView中有一个maskView属性,这个属性在iOS8之后开始使用,用来表示视图的遮罩。类比于CALayer中的mask,他们的基本原理都是一样的。

1.当一个view设置了maskView后,那么它只会显示与maskView重叠的部分(maskView跟view没有层次,可以理解maskView嵌在View里)。

2.对于maskView与View重叠部分,可以这样理解,是将maskView每个point的alpha赋值给View的重叠部分相对应的point,这样view的重叠每个point都有个alpha值了,view重叠部分就可能显示多种透明色。

例子1:渐进式显示和隐藏UILabel

UIView*maskView = [[UIView alloc]initWithFrame:self.label.bounds];

CAGradientLayer *gradient = [CAGradientLayer layer];

gradient.frame= maskView.bounds;

gradient.colors=@[(__bridgeid)[UIColorclearColor].CGColor,(__bridgeid)[UIColorblackColor].CGColor,(__bridgeid)[UIColorclearColor].CGColor];

gradient.locations = @[@(0.01), @(0.1), @(0.9), @(0.99)];

gradient.startPoint=CGPointMake(0,0);

gradient.endPoint=CGPointMake(1,0);

[maskView.layeraddSublayer:gradient];

self.label.maskView= maskView;

[UIView animateWithDuration:2.0 delay:0.0 options:UIViewAnimationOptionCurveEaseOut animations:^{

CGRect frame =  maskView.frame;

frame.origin.x+= frame.size.width;

maskView.frame= frame;

}completion:^(BOOLfinished) {

[UIView animateWithDuration:2.0 delay:0.0 options:UIViewAnimationOptionCurveEaseOut animations:^{

CGRect frame = maskView.frame;

frame.origin.x-= frame.size.width;

maskView.frame= frame;

}completion:^(BOOLfinished) {

}];

}];

例子2,滑动解锁效果

CAGradientLayer *gradientLayer = [CAGradientLayer layer];

gradientLayer.frame=CGRectMake(0,200,200,64);

gradientLayer.colors=@[(__bridgeid)[UIColorblackColor].CGColor,

(__bridgeid)[UIColorwhiteColor].CGColor,

(__bridgeid)[UIColorblackColor].CGColor];

gradientLayer.locations=@[@0.25,@0.5,@0.75];

gradientLayer.startPoint=CGPointMake(0,0);

gradientLayer.endPoint=CGPointMake(1,0);

[self.view.layeraddSublayer:gradientLayer];

CABasicAnimation *basicAnimation = [CABasicAnimation animationWithKeyPath:@"locations"];

basicAnimation.fromValue=@[@0, @0,@0.25];

basicAnimation.toValue=@[@0.75, @1, @1];

basicAnimation.duration=2.5;

basicAnimation.repeatCount=HUGE;

[gradientLayeraddAnimation:basicAnimationforKey:nil];

UILabel*unlock = [[UILabelalloc]initWithFrame:gradientLayer.bounds];

unlock.alpha=0.5;

unlock.text=@"滑动来解锁 >>";

unlock.textAlignment = NSTextAlignmentCenter;

unlock.font = [UIFont boldSystemFontOfSize:30];

[self.viewaddSubview:unlock];

gradientLayer.mask= unlock.layer;

例子3,两图渐变显示

#import "TestViewController.h"

static const NSInteger horizontalCount =30;

static const NSInteger verticalCount =5;

@interface TestViewController ()

{

NSInteger count;

}

@property (nonatomic,strong) UIView *maskView;

@property (weak, nonatomic) IBOutlet UIImageView *downImage;

@property (weak, nonatomic) IBOutlet UIImageView *upImage;

@end

@implementationTestViewController

- (void)viewDidLoad {

[super viewDidLoad];

// Do any additional setup after loading the view, typically from a nib.

}

- (void)viewDidAppear:(BOOL)animated{

[super viewDidAppear:animated];

self.maskView= [[UIViewalloc]initWithFrame:self.upImage.bounds];

const CGFloat fadeWidth =CGRectGetWidth(self.maskView.bounds) /horizontalCount;

const CGFloat fadeHeight =CGRectGetHeight(self.maskView.bounds) /verticalCount;

for(NSIntegerline =0; line < horizontalCount; line ++) {

for(NSIntegerrow =0;row < verticalCount; row++) {

CGRectframe =CGRectMake(line*fadeWidth, row*fadeHeight, fadeWidth, fadeHeight);

UIView* fadeView = [[UIViewalloc]initWithFrame: frame];

fadeView.tag=  line*verticalCount+row+100;

fadeView.backgroundColor= [UIColor whiteColor];

[self.maskView addSubview: fadeView];

}

}

self.upImage.maskView = self.maskView;

[self animationFromLeft];

}

- (void)animationFromLeft

{

__weak typeof(self) weakSelf = self;

for(NSInteger line =0;line < horizontalCount; line ++) {

for(NSInteger row =0;row < verticalCount; row++) {

NSInteger idx = line*verticalCount+row;

UIView* fadeView = [self.maskView viewWithTag: idx+100];

[UIView animateWithDuration: 0.3 delay: 0.5*0.25*idx options: UIViewAnimationOptionCurveLinear animations: ^{

fadeView.alpha=0;

}completion:^(BOOLfinished) {

self->count++;

if (self->count == verticalCount*horizontalCount) {

[weakSelf animationFromRight];

}

}];

}

}

}

- (void)animationFromRight

{

__weak typeof(self) weakSelf = self;

for(NSInteger line =0;line < horizontalCount; line ++) {

for(NSInteger row =0;row < verticalCount; row++) {

NSInteger idx = verticalCount*horizontalCount-1-line*verticalCount-row;

UIView* fadeView = [self.maskViewviewWithTag: idx+100];

[UIView animateWithDuration: 0.3 delay: 0.5*0.25*(verticalCount*horizontalCount-1-idx) options: UIViewAnimationOptionCurveLinear animations: ^{

fadeView.alpha=1;

}completion:^(BOOLfinished) {

self->count--;

if(self->count==0) {

[weakSelf animationFromLeft];

}

}];

}

}

}

@end

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值