CAShapeLayer的使用[1]
生活随笔
收集整理的這篇文章主要介紹了
CAShapeLayer的使用[1]
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
CAShapeLayer的使用[1]
?
使用CoreAnimation繪制動畫帶來的系統(tǒng)開銷非常的小,CoreAnimation通常都是使用GPU的.
CAShapeLayer屬于CoreAnimation中很重要的一種layer,無論是作為mask還是作為進度條顯示都非常的好用,我用CAShapeLayer實現(xiàn)了如下復(fù)雜的效果.
// // 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];// 關(guān)閉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];// 貝塞爾曲線(創(chuàng)建一個圓)UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(0, 0)radius:100startAngle: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);// 設(shè)置填充顏色為透明_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:selfaction:@selector(handlePan:)];[_showView addGestureRecognizer:recognizer]; }@end這個地方很關(guān)鍵哦,沒有關(guān)閉的話會非常的卡頓的.
?
shapeLayer作為mask的一些技巧.
?
?
總結(jié)
以上是生活随笔為你收集整理的CAShapeLayer的使用[1]的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。