ios实现CALayer层的动画点击
2012-10-31 17:08
316 查看
from:http://wangjun.easymorse.com/?p=1267
利用CALayer可以实现复杂的动画效果,同时CALayer在运动过程中,需要点击CALayer,同时能够监控到点击的对象。下面是实现的效果和过程。
实现过程:
#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/
相关文章推荐
- ios实现CALayer层的动画点击
- ios实现CALayer层的动画点击
- ios动画-新浪微博app点击发微博按钮动画的实现
- IOS_实现tableview向上滚动全屏效果与点击展开动画
- iOS tabbar点击动画效果实现
- iOS tabbar点击动画效果实现
- Github系列之二:开源 一行代码实现多形式多动画的推送小红点WZLBadge(iOS)
- 李洪强iOS开发之-实现点击单行View显示和隐藏Cell
- Android TextView实现点击展开动画效果
- iOS动画实现方式 animation key path
- iOS UI设计: 在Autolayout的布局上用constraint实现动画
- CAAnimation的iOS动画效果和实现
- IOS开动画效果之──实现 pushViewController 默认动画效果
- (NO.00004)iOS实现打砖块游戏(二):实现游戏主界面动画
- 萌货猫头鹰登录界面动画iOS实现分析
- iOS动画效果和实现
- iOS实现tableViewCell或collectionCell中点击界面按钮跳转
- iOS开发-截获html按钮点击事件,实现js调用native代码
- iOS 实现简单的加载等待动画示例(思路与实现)
- iOS开发UI篇—CAlayer层的属性(position、anchorPoint) 和隐式动画