利用 CALayer 可以实现复杂的动画效果
来源:互联网 发布:雷姆雷姆软件汉化 编辑:程序博客网 时间:2024/05/17 02:51
实现过程:
#import "AnimView.h"
@implementation AnimView
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
[self setBackgroundColor:[UIColor clearColor]];
UITapGestureRecognizer *tapGesture=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(touchesPoint:)];
[self addGestureRecognizer:tapGesture];
[tapGesture release];
}
return self;
}
-(void) drawRect:(CGRect)rect
{
[NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(stratAnim:) userInfo:nil repeats: NO];
}
-(void)stratAnim:(id)sender
{
//添加层
layer2 = [CALayer layer];
[layer2 setBackgroundColor:[[UIColor redColor] CGColor]];
layer2.bounds = CGRectMake(0, 0, 60,40);//层设置为图片大小
layer2.position = CGPointMake(25,25);//层在view的位置
[self.layer addSublayer:layer2];//将层加到当前View的默认layer下
[self startFlyStarAnimation];
}
-(void) startFlyStarAnimation
{
//运动轨迹
CGMutablePathRef thePath=CGPathCreateMutable();
CGPathMoveToPoint(thePath,NULL,self.center.x,self.center.y);
CGPathAddLineToPoint(thePath, NULL, self.center.x, self.center.y-45);
CGPathAddLineToPoint(thePath, NULL, self.center.x, self.center.y+45);
CGPathAddLineToPoint(thePath, NULL, self.center.x, self.center.y);
//添加动画
CAKeyframeAnimation * animation;
animation=[CAKeyframeAnimation animationWithKeyPath:@"position"];
animation.path=thePath;
animation.duration=3.0;
animation.repeatCount=2;
CFRelease(thePath);
[animation setDelegate:self];
//[self animationDidStop:animation finished:YES];
[animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear]];
[layer2 addAnimation:animation forKey:kCATransition];
}
//动画停止
-(void) animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
{
NSLog(@">>>>动画停止了");
}
//touch事件
-(void)touchesPoint:(UITapGestureRecognizer *)gestureRecognizer
{
CGPoint locationInView = [gestureRecognizer locationInView:self];
//presentationLayer layer的动画层
CALayer *layer1=[[layer2 presentationLayer] hitTest:locationInView];
if (layer1!=nil) {
NSLog(@"点击了运动的layer");
}
}
- (void)dealloc {
[super dealloc];
}
@end
其中 presentationLayer 是 CALayer 动画的位置层。源代码下载:http://easymorse-iphone.googlecode.com/svn/trunk/iphone.presentlayer/
- 利用 CALayer 可以实现复杂的动画效果
- 使用CAlayer实现动画效果
- 使用CALayer的Mask实现注水动画效果
- 使用CALayer的Mask实现注水动画效果
- 使用CALayer的Mask实现注水动画效果
- 使用CALayer的Mask实现注水动画效果
- 使用CALayer的Mask实现注水动画效果
- ios动画中抖动动画效果的实现与CALayer动画的基本理解
- CALayer利用锚点实现简单动画
- 实现 圆形加载view 动画效果(用CALayer,CABasicAnimation实现)
- 实现 CALayer 层动画点击的代码
- 实现 CALayer 层动画点击的代码
- 实现 CALayer 层动画点击的代码
- ios实现CALayer层的动画点击
- 实现 CALayer 层动画点击的代码
- 利用OpenGL实现动画效果
- 设计复杂的 iOS 动画效果
- CALayer 的简介 和Core Animation动画效果 A
- MFC点点滴滴——CImageList
- 打豆豆
- 快速排序的优化
- hibernate 缓存
- C#编程
- 利用 CALayer 可以实现复杂的动画效果
- 每天多学习一点,再多学习一点
- Ubuntu下Aircrack-ng工具包的使用
- java判断与循环语句
- java中线程池
- 模拟退火算法解决TSP问题
- UDP丢包问题
- GA的cookie内容详细解析
- 红黑树