不规则形状的Mask动画

效果

源码

https://github.com/YouXianMing/Animations

//
//  MaskShapeViewController.m
//  Animations
//
//  Created by YouXianMing on 16/7/10.
//  Copyright © 2016年 YouXianMing. All rights reserved.
//

#import "MaskShapeViewController.h"
#import "AppleSystemService.h"
#import "JSAnimatedImagesView.h"
#import "CutOutClearView.h"
#import "UIView+SetRect.h"
#import "UIFont+Fonts.h"
#import "FBShimmeringView.h"

typedef enum : NSUInteger {

    kUpJsView = 1000,
    kDownJsView,

} EMaskShapeViewControllerValue;

@interface MaskShapeViewController () <JSAnimatedImagesViewDataSource>

@property (nonatomic, strong) JSAnimatedImagesView  *upJsView;
@property (nonatomic, strong) NSArray               *upDataSource;

@property (nonatomic, strong) JSAnimatedImagesView  *downJsView;
@property (nonatomic, strong) NSArray               *downDataSource;

@end

@implementation MaskShapeViewController

- (void)setup {

    [super setup];

    self.backgroundView.backgroundColor = [UIColor blackColor];

    CGFloat gap    = 4.f;
    CGFloat offset = 50.f;

    {
        CutOutClearView *areaView = [[CutOutClearView alloc] initWithFrame:CGRectMake(0, 0, Width, self.contentView.height / 2.f + offset - gap)];
        areaView.fillColor        = [UIColor clearColor];
        areaView.areaColor        = [UIColor blackColor];

        UIBezierPath *path = [UIBezierPath bezierPath];
        [path moveToPoint:CGPointMake(gap, gap)];
        [path addLineToPoint:CGPointMake(Width - gap, gap)];
        [path addLineToPoint:CGPointMake(Width - gap, areaView.height)];
        [path addLineToPoint:CGPointMake(gap, areaView.height - (offset - gap) * 2 - gap)];
        [path closePath];
        areaView.paths = @[path];

        self.upDataSource = @[[UIImage imageNamed:@"1"],
                              [UIImage imageNamed:@"2"],
                              [UIImage imageNamed:@"3"],
                              [UIImage imageNamed:@"4"],
                              [UIImage imageNamed:@"5"]];

        self.upJsView                     = [[JSAnimatedImagesView alloc] initWithFrame:CGRectMake(0, 0, Width, self.contentView.height / 2.f + offset - gap)];
        self.upJsView.transitionDuration  = 2.f;
        self.upJsView.tag                 = kUpJsView;
        self.upJsView.dataSource          = self;
        self.upJsView.layer.masksToBounds = YES;
        self.upJsView.maskView            = areaView;
        [self.contentView addSubview:self.upJsView];
    }

    {
        CutOutClearView *areaView = [[CutOutClearView alloc] initWithFrame:CGRectMake(0, 0, Width, self.contentView.height / 2.f + (offset - gap))];
        areaView.fillColor        = [UIColor clearColor];
        areaView.areaColor        = [UIColor blackColor];

        UIBezierPath *path = [UIBezierPath bezierPath];
        [path moveToPoint:CGPointMake(gap, 0)];
        [path addLineToPoint:CGPointMake(gap, areaView.height - gap)];
        [path addLineToPoint:CGPointMake(Width - gap, areaView.height - gap)];
        [path addLineToPoint:CGPointMake(Width - gap, (offset - gap) * 2 + gap)];
        [path closePath];
        areaView.paths = @[path];

        self.downDataSource = @[[UIImage imageNamed:@"pic_1"],
                                [UIImage imageNamed:@"pic_2"],
                                [UIImage imageNamed:@"pic_3"],
                                [UIImage imageNamed:@"pic_4"]];

        self.downJsView                     = [[JSAnimatedImagesView alloc] initWithFrame:CGRectMake(0, 0, Width, self.contentView.height / 2.f + offset - gap)];
        self.downJsView.transitionDuration  = 2.f;
        self.downJsView.tag                 = kDownJsView;
        self.downJsView.dataSource          = self;
        self.downJsView.layer.masksToBounds = YES;
        self.downJsView.maskView            = areaView;

        self.downJsView.bottom = self.contentView.height;
        [self.contentView addSubview:self.downJsView];
    }
}

#pragma mark - JSAnimatedImagesViewDataSource

- (NSUInteger)animatedImagesNumberOfImages:(JSAnimatedImagesView *)animatedImagesView {

    NSUInteger count = 0;
    animatedImagesView.tag == kUpJsView ? (count = self.upDataSource.count) : (count = self.downDataSource.count);

    return count;
}

- (UIImage *)animatedImagesView:(JSAnimatedImagesView *)animatedImagesView imageAtIndex:(NSUInteger)index {

    UIImage *image = nil;
    animatedImagesView.tag == kUpJsView ? (image = self.upDataSource[index]) : (image = self.downDataSource[index]);

    return image;
}

#pragma mark - Overwrite methods.

- (void)buildTitleView {

    [super buildTitleView];

    // Title label.
    UILabel *headlinelabel      = [UILabel new];
    headlinelabel.font          = [UIFont HeitiSCWithFontSize:20.f];
    headlinelabel.textAlignment = NSTextAlignmentCenter;
    headlinelabel.textColor     = [UIColor cyanColor];
    headlinelabel.text          = self.title;
    [headlinelabel sizeToFit];

    headlinelabel.center = self.titleView.middlePoint;

    FBShimmeringView *shimmeringView           = [[FBShimmeringView alloc] initWithFrame:self.titleView.bounds];
    shimmeringView.shimmering                  = YES;
    shimmeringView.shimmeringBeginFadeDuration = 0.3;
    shimmeringView.shimmeringOpacity           = 0.1f;
    shimmeringView.shimmeringAnimationOpacity  = 1.f;
    [self.titleView addSubview:shimmeringView];

    shimmeringView.contentView = headlinelabel;

    // Line.
    UIView *line         = [[UIView alloc] initWithFrame:CGRectMake(0, 63.5, self.view.width, 0.5f)];
    line.backgroundColor = [[UIColor grayColor] colorWithAlphaComponent:0.25f];
    [self.titleView addSubview:line];
    [self.titleView addSubview:headlinelabel];

    // Back button.
    UIImage  *image      = [UIImage imageNamed:@"backIconVer2"];
    UIButton *backButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 100, 64)];
    backButton.center    = CGPointMake(20, self.titleView.middleY);
    [backButton setImage:image forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(popSelf) forControlEvents:UIControlEventTouchUpInside];
    [backButton.imageView setContentMode:UIViewContentModeCenter];
    [self.titleView addSubview:backButton];
}

- (void)popSelf {

    [self popViewControllerAnimated:YES];
}

@end

细节

//
//  CutOutClearView.h
//  Animations
//
//  Created by YouXianMing on 16/7/10.
//  Copyright © 2016年 YouXianMing. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface CutOutClearView : UIView

/**
 *  The total fill color, you can used it as the view‘s background color.
 */
@property (nonatomic, strong) UIColor  *fillColor;

/**
 *  The paths area color.
 */
@property (nonatomic, strong) UIColor  *areaColor;

/**
 *  Path array.
 */
@property (nonatomic, strong) NSArray  <UIBezierPath *>  *paths;

@end
//
//  CutOutClearView.m
//  Animations
//
//  Created by YouXianMing on 16/7/10.
//  Copyright © 2016年 YouXianMing. All rights reserved.
//

#import "CutOutClearView.h"

@implementation CutOutClearView

- (instancetype)initWithFrame:(CGRect)frame {

    if (self = [super initWithFrame:frame]) {

        self.fillColor       = [UIColor whiteColor];
        self.backgroundColor = [UIColor clearColor];
        self.opaque          = NO;
    }

    return self;
}

- (void)drawRect:(CGRect)rect {

    [super drawRect:rect];

    [self.fillColor setFill];
    UIRectFill(rect);

    CGContextRef context = UIGraphicsGetCurrentContext();

    if (self.areaColor && self.paths.count) {

        UIBezierPath *path = nil;

        for (int i = 0; i < self.paths.count; i++) {

            i == 0 ? path = self.paths[i] : [path appendPath:self.paths[i]];
        }

        CGFloat red   = 0;
        CGFloat green = 0;
        CGFloat blue  = 0;
        CGFloat alpha = 0;
        [self.areaColor getRed:&red green:&green blue:&blue alpha:&alpha];

        CGContextAddPath(context, path.CGPath);
        CGContextSetRGBFillColor(context, red, green, blue, alpha);
        CGContextFillPath(context);

    } else {

        for (UIBezierPath *path in self.paths) {

            CGContextAddPath(context, path.CGPath);
            CGContextSetBlendMode(context, kCGBlendModeClear);
            CGContextFillPath(context);
        }
    }
}

@end
时间: 2024-10-15 18:06:33

不规则形状的Mask动画的相关文章

NGUI不规则形状点击

PolygonCollider2D 我们的地图是这样的不规则形状,最开始简单用Box Collider处理点击,但是点击地图边缘时命中率很差.今天查到Unity有多边形Collider可以支持不规则形状,PolygonCollider2D. 给地图[变异的开端]加上PolygonCollider2D,然后根据地图形状编辑Collider多边形,编辑好之后下图这样子. 点击实现 需要注意的是NGUI中检测射线碰撞用的是Physics.RayCast,不支持2D Collider,也就是无法响应to

css3-radio不规则形状动画

利用质数做随机图形 参考网站 http://2016.uxlondon.com/speakers .list { margin: 10px; display: inline-block; width: 168px; height: 168px; border: 4px solid #00C7E9; border-radius: 87% 91% 98% 100%; -webkit-transition: all .35s; transition: all .35s; overflow: hidde

图片碎片化mask动画

效果 源码 https://github.com/YouXianMing/Animations // // TransformFadeViewController.m // Animations // // Created by YouXianMing on 15/11/17. // Copyright © 2015年 YouXianMing. All rights reserved. // #import "TransformFadeViewController.h" #import

CAD创建不规则形状视口

选择CAD模型空间中多段线,在指定的布局中创建视口,方法如下: 1 /// <summary> 2 /// 创建视口 3 /// </summary> 4 /// <param name="roundLine">模型空间多段线</param> 5 /// <param name="curentLayout">当前布局名称</param> 6 /// <param name="in

任意不规则形状的图片剪裁 .

原理:根据选择的区域(区域的选择需要用到GraphicsPath),根据区域可以得到这部分区域的边境矩形,从而不需要循环整张图片,只需要循环边境矩形,将矩形中的选择区域复制到另一种图片,这样大大提高了效率.根据选择的区域,可以得到复制出位于GraphicsPath中的这部分图片,同时设置这部分图片为透明,同时还要设置另一种图片不在GraphicsPath内的区域为透明,这样看起来的效果就是从图片中扣出来的一样,意思和PhotoShop的套索工具差不多. 说得有点晕了,还是看效果图吧:(左边是剪裁

不规则形状表格合并

在学习表格时我们会遇到一些既跨行又跨列合并的情况,此时可以用下面这种方法来实现. <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title>Document</title> </head> <body> <br> <br> <br> <br> <table width=&q

iOS 不规则的ImageView

我们在做iOS开发的时候,往往需要实现不规则形状的头像,如: 那如何去实现? 通常图片都是矩形的,如果想在客户端去实现不规则的头像,需要自己去实现. 1.使用layer去实现, 见http://blog.csdn.net/johnzhjfly/article/details/39993345 2.使用CAShapeLayer, CALayer如何去实现 我们来看看如何使用CAShapeLayer去实现, 定义一个ShapedImageView,继承于UIView, 代码如下: #import "

【腾讯bugly干货分享】Android自绘动画实现与优化实战——以Tencent OS录音机波形动

前言 本文为腾讯bugly的原创内容,非经过本文作者同意禁止转载,原文地址为:http://bugly.qq.com/bbs/forum.php?mod=viewthread&tid=1180 我们所熟知的,Android 的图形绘制主要是基于 View 这个类实现. 每个 View 的绘制都需要经过 onMeasure.onLayout.onDraw 三步曲,分别对应到测量大小.布局.绘制. Android 系统为了简化线程开发,降低应用开发的难度,将这三个过程都放在应用的主线程(UI 线程)

SVG路径动画解密

原文:SVG路径动画解密 原文链接:http://www.gbtags.com/gb/share/5581.htm SVG路径动画效果现在貌似越来越多网站都使用了,给我的感觉就像是一段时间的流行而已,刚一出来大家都感觉很炫酷,时间久了也就审美疲劳啦!如果还不知道什么是SVG路径动画的童鞋,快来看一个demo吧:http://tympanus.net/Development/SVGDrawingAnimation/ 课程库相关课程:?常用的三种生成SVG图形动画方式 怎么样?是不是很炫酷!另外这种