如何使用 CABasic 动画从屏幕顶部到底部制作动画时获取图像帧

发布于 2024-11-01 22:38:57 字数 1805 浏览 0 评论 0原文

我正在使用 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);
    }

我需要在视图上路径的每个点处进行动画处理的图像的框架。

你能建议一下这个代码吗?

感谢你

I am using CABasicAnimation for animating an image from top to bottom of the screen. I need to
get frame of the image while it is animating from top to bottom......

Code:

-(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);
    }

I need the frame of the image that is animating at each point of it's path on the view.

Can u please suggest the code for this.

Thank U

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我很OK 2024-11-08 22:38:58

使用 presentationLayer 应用动画的 CALayer 属性。

Use the presentationLayer property of the CALayer to which the animation is applied.

爱的那么颓废 2024-11-08 22:38:58

朋友你好
你可以尝试 if (CGRectIntersectsRect(layer.frame,dragger.frame) ) {

nslog(@"hiiii");

}

Hi friend
you can try if (CGRectIntersectsRect(layer.frame,dragger.frame) ) {

nslog(@"hiiii");

}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文