如何在使用 CABasic 动画从屏幕顶部到底部设置动画时获取图像帧
Posted
技术标签:
【中文标题】如何在使用 CABasic 动画从屏幕顶部到底部设置动画时获取图像帧【英文标题】:how to get frame of the image while animating from top to bottom of screen using CABasic animation 【发布时间】:2011-04-16 14:56:00 【问题描述】:我正在使用 CABasicAnimation 从屏幕的顶部到底部为图像设置动画。我需要 在从上到下动画时获取图像的帧......
代码:
-(id)init
if(self = [super init])
self.title =@"Apple Catch";
mView = [[UIView alloc]initWithFrame:CGRectMake(0,0,320,440)];
// start a timet that will fire 20 times per second
[NSTimer scheduledTimerWithTimeInterval:(0.9) target:self selector:@selector(onTimer)
userInfo:nil repeats:YES];
self.view=mView;
return self;
- (void)onTimer
CGImageRef imageRef = [[UIImage imageNamed:@"apple.png"] CGImage];
int startX = round(random() % 460);
double speed = 1 / round(random() %100) + 1.0;
CALayer *layer = [CALayer layer];
layer.name = @"layer";
layer.contents = imageRef;
layer.frame = CGRectMake(startX, self.view.frame.origin.y, CGImageGetWidth(imageRef),
CGImageGetHeight(imageRef));
[mView.layer addSublayer:layer];
CGPoint start = CGPointMake(startX, 0);
CGPoint end = CGPointMake(startX, self.view.frame.size.height+10);
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"];
animation.delegate = self;
animation.fromValue = [NSValue valueWithCGPoint:start];
animation.toValue = [NSValue valueWithCGPoint:end];
animation.duration = 10*speed;
//animation.repeatCount = repeatCount;
animation.autoreverses = NO;
animation.removedOnCompletion = YES;
animation.fillMode = kCAFillModeForwards;
[layer addAnimation:animation forKey:@"position"];
BOOL intersects = CGRectIntersectsRect(layer.frame , dragger.frame);
printf("\n ==== intersects value :%d",intersects);
我需要在视图路径的每个点上动画的图像帧。
你能推荐一下代码吗?
谢谢你
【问题讨论】:
【参考方案1】:使用动画应用到的 CALayer 的 presentationLayer
属性。
【讨论】:
【参考方案2】:你好朋友 你可以试试 if (CGRectIntersectsRect(layer.frame,dragger.frame) )
nslog(@"hiiii");
【讨论】:
以上是关于如何在使用 CABasic 动画从屏幕顶部到底部设置动画时获取图像帧的主要内容,如果未能解决你的问题,请参考以下文章