XMG CAKeyFrameAnimation
2016-05-19 11:47
387 查看
用贝泽尔曲线描绘了一个路径。然后让CAKeyFrameAnimation的path是这个。就可以描绘出这个曲线路径了
CAKeyframeAnimation*keyFrameAnimation=[CAKeyframeAnimation
animation];
keyFrameAnimation.keyPath=@"position";
keyFrameAnimation.duration=2;
//贝泽尔曲线描绘的路径
keyFrameAnimation.path=_bezierPath.CGPath;
keyFrameAnimation.repeatCount=MAXFLOAT;
[self.singleImageView.layer
addAnimation:keyFrameAnimation
forKey:nil];
#import "DrawView.h"
@interface DrawView()
@property (weak,
nonatomic) IBOutlet
UIImageView *singleImageView;
//@property (weak,nonatomic)UIImageView*imageView;U
@property(nonatomic,strong)UIBezierPath*bezierPath;
@end
@implementation DrawView
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code
}
*/
-(void)awakeFromNib{
[self
addGestureRecognizer:[[UIPanGestureRecognizer
alloc]initWithTarget:self
action:@selector(panClick:)]];
}
-(void)panClick:(UIPanGestureRecognizer*)pan{
CGPoint currentP=[pan
locationInView:self];
if (pan.state==UIGestureRecognizerStateBegan) {
_bezierPath=[UIBezierPath
bezierPath];
[_bezierPath
moveToPoint:currentP];
_bezierPath.lineWidth=5;
}else
if(pan.state==UIGestureRecognizerStateChanged){
[_bezierPath
addLineToPoint:currentP];
//每次添加线的时候
画一次
[self
setNeedsDisplay];
}else
if(pan.state==UIGestureRecognizerStateEnded){
CAKeyframeAnimation*keyFrameAnimation=[CAKeyframeAnimation
animation];
keyFrameAnimation.keyPath=@"position";
keyFrameAnimation.duration=2;
//贝泽尔曲线描绘的路径
keyFrameAnimation.path=_bezierPath.CGPath;
keyFrameAnimation.repeatCount=MAXFLOAT;
[self.singleImageView.layer
addAnimation:keyFrameAnimation
forKey:nil];
}
// [self setNeedsDisplay];
}
-(void)drawRect:(CGRect)rect
{
[[UIColor
redColor]setStroke];
[self.bezierPath
stroke];
}
CAKeyframeAnimation*keyFrameAnimation=[CAKeyframeAnimation
animation];
keyFrameAnimation.keyPath=@"position";
keyFrameAnimation.duration=2;
//贝泽尔曲线描绘的路径
keyFrameAnimation.path=_bezierPath.CGPath;
keyFrameAnimation.repeatCount=MAXFLOAT;
[self.singleImageView.layer
addAnimation:keyFrameAnimation
forKey:nil];
#import "DrawView.h"
@interface DrawView()
@property (weak,
nonatomic) IBOutlet
UIImageView *singleImageView;
//@property (weak,nonatomic)UIImageView*imageView;U
@property(nonatomic,strong)UIBezierPath*bezierPath;
@end
@implementation DrawView
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code
}
*/
-(void)awakeFromNib{
[self
addGestureRecognizer:[[UIPanGestureRecognizer
alloc]initWithTarget:self
action:@selector(panClick:)]];
}
-(void)panClick:(UIPanGestureRecognizer*)pan{
CGPoint currentP=[pan
locationInView:self];
if (pan.state==UIGestureRecognizerStateBegan) {
_bezierPath=[UIBezierPath
bezierPath];
[_bezierPath
moveToPoint:currentP];
_bezierPath.lineWidth=5;
}else
if(pan.state==UIGestureRecognizerStateChanged){
[_bezierPath
addLineToPoint:currentP];
//每次添加线的时候
画一次
[self
setNeedsDisplay];
}else
if(pan.state==UIGestureRecognizerStateEnded){
CAKeyframeAnimation*keyFrameAnimation=[CAKeyframeAnimation
animation];
keyFrameAnimation.keyPath=@"position";
keyFrameAnimation.duration=2;
//贝泽尔曲线描绘的路径
keyFrameAnimation.path=_bezierPath.CGPath;
keyFrameAnimation.repeatCount=MAXFLOAT;
[self.singleImageView.layer
addAnimation:keyFrameAnimation
forKey:nil];
}
// [self setNeedsDisplay];
}
-(void)drawRect:(CGRect)rect
{
[[UIColor
redColor]setStroke];
[self.bezierPath
stroke];
}
相关文章推荐
- 程序运行完后不让窗口一闪而过
- 进程与线程的一个简单解释
- JSON 解析
- JAVAScript和JAVA有区别吗
- Mysql having 与group的综合练习
- Java学习之反射机制笔记--4
- 关于scale和zoom的区别
- Junit单元测试注意事项
- Jenkis 打包 AS 项目
- AndroidSocketClient开源库的使用
- Win10 14342系统的任务栏和开始菜单属性在哪里?
- linux 下 vi vim 的笔记
- android 发送短信的两种方式
- String类的实现 三大复制控制函数
- 【Leetcode】之Unique Paths II
- SQL语句insert into 不存在则插入 or 存在则修改
- 微软Web服务器受到欢迎的原因
- c++第6次实验-项目2-数组合并
- 349. Intersection of Two Arrays [easy] (Python)
- c++第六次实验