实现 CALayer 层动画点击的代码

from: http://www.cocoachina.com/iphonedev/sdk/2011/0506/2840.html

 

利用 CALayer 可以实现复杂的动画效果,同时 CALayer 在运动过程中,需要点击 CALayer,同时能够监控到点击的对象。下面是实现的效果和过程。 实现过程: #import AnimView.h @implementation Ani

 

利用 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/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值