zoukankan      html  css  js  c++  java
  • CAShapeLayer的使用[1]

    CAShapeLayer的使用[1]

    使用CoreAnimation绘制动画带来的系统开销非常的小,CoreAnimation通常都是使用GPU的.

    CAShapeLayer属于CoreAnimation中很重要的一种layer,无论是作为mask还是作为进度条显示都非常的好用,我用CAShapeLayer实现了如下复杂的效果.

    //
    //  RootViewController.m
    //
    //  Copyright (c) 2014年 Y.X. All rights reserved.
    //
    
    #import "RootViewController.h"
    #import "UIImage+ImageEffects.h"
    
    @interface RootViewController ()
    
    @property (nonatomic, strong) UIView        *showView;
    @property (nonatomic, strong) CAShapeLayer  *oneReferenceLayer;
    @property (nonatomic, strong) CAShapeLayer  *maskLayer;
    
    @end
    
    #define   DEGREES(degrees)  ((M_PI * (degrees))/ 180.f)
    
    @implementation RootViewController
    
    - (void)handlePan:(UIPanGestureRecognizer *)recognizer
    {
        // 拖拽
        CGPoint translation = [recognizer translationInView:self.view];
        recognizer.view.center = CGPointMake(recognizer.view.center.x + translation.x,
                                             recognizer.view.center.y + translation.y);
        [recognizer setTranslation:CGPointMake(0, 0) inView:self.view];
    
        // 关闭CoreAnimation实时动画绘制(核心)
        [CATransaction setDisableActions:YES];
        _maskLayer.position = recognizer.view.center;
    }
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        
        /* ====== 背景View ====== */
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:self.view.bounds];
        imageView.image = [UIImage imageNamed:@"back"];
        [self.view addSubview:imageView];
        
    
        /* ====== 作为mask的View ====== */
        _maskLayer = [CAShapeLayer layer];
        
        // 贝塞尔曲线(创建一个圆)
        UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(0, 0)
                                                            radius:100
                                                        startAngle:DEGREES(0)
                                                          endAngle:DEGREES(360)
                                                         clockwise:YES];
        // 获取path
        _maskLayer.path     = path.CGPath;
        _maskLayer.position = CGPointMake(_showView.bounds.size.width/2.f,
                                          _showView.bounds.size.height/2.f);
        // 设置填充颜色为透明
        _maskLayer.fillColor = [UIColor blackColor].CGColor;
        _maskLayer.position = self.view.center;
        
        UIView *blurView = [[UIView alloc] initWithFrame:self.view.bounds];
        blurView.backgroundColor = [UIColor blackColor];
        [self.view addSubview:blurView];
        blurView.layer.mask = _maskLayer;
        blurView.layer.contents = (__bridge id)([[UIImage imageNamed:@"back"] blurImage].CGImage);
        
        /* ====== 透明的View,用于maskView中的ShapeLayer的参考View(用于拖拽) ====== */
        _showView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
        _showView.backgroundColor = [UIColor clearColor];
        _showView.center = self.view.center;
        [self.view addSubview:_showView];
        
        UIPanGestureRecognizer *recognizer = 
        [[UIPanGestureRecognizer alloc] initWithTarget:self
                                                action:@selector(handlePan:)];
        [_showView addGestureRecognizer:recognizer];
    }
    
    @end

    这个地方很关键哦,没有关闭的话会非常的卡顿的.

    shapeLayer作为mask的一些技巧.

  • 相关阅读:
    【Gerrit】重磅! 2.x 版本升级到 3.x 版本
    【Linux】参数传递之xargs
    Sqlserver账号对应数据库
    限流:计数器、漏桶、令牌桶 三大算法的原理与实战(史上最全)
    C# 运行在ubuntu, linux系统,在linux系统使用HslCommunication组件,.net core发布到ubuntu系统
    使用nmap命令监控远程服务器指定端口状态
    MySQL使用脚本进行整库数据备份【表(结构+数据)、视图、函数、事件】
    MySQL自定义函数与存储过程的创建、使用、删除
    vue响应式的原理
    浏览器渲染机制
  • 原文地址:https://www.cnblogs.com/YouXianMing/p/3777176.html
Copyright © 2011-2022 走看看