动画时需要获得图像的帧坐标

时间:2011-04-16 06:48:36

标签: iphone objective-c

我正在使用一个应用程序,因为该图像是使用动画从屏幕的顶部到底部动画。

代码:

- (void)onTimer
{
    // build a view from our flake image
    flakeView = [[UIImageView alloc] initWithImage:flakeImage];
    flakeView.backgroundColor=[UIColor blueColor];
    // use the random() function to randomize up our flake attributes
    int startX =round(random() % 460);  
    printf("\n ===== startX :%d",startX);
    int endX = round(random() % 460);
    printf("\n ===== endX :%d",endX);
    double scale = 1 / round(random() % 100) - 1.0;
    double speed = 1 / round(random() %100) + 1.0;
    // set the flake start position
    flakeView.frame = CGRectMake(startX, -100.0, 25.0 * scale, 25.0 * scale);
    flakeView.alpha = 1.0;
    // put the flake in our main view
    [mView addSubview:flakeView];
    [UIView beginAnimations:nil context:flakeView];
    [UIView setAnimationDuration:20 * speed];
    // code to get current position of image on view
    CALayer mainLayer = flakeView.layer.presentationLayer;
    CGRect layerFrame = mainLayer.frame;
    BOOL intersects = CGRectIntersectsRect(layerFrame, dragger.frame);
    // end position on screen
    flakeView.frame = CGRectMake(endX, 500.0, 15.0 * scale, 15.0 * scale);
    // set a stop callback so we can cleanup the flake when it reaches the
    // end of its animation
    [UIView setAnimationDidStopSelector:@selector(onAnimationComplete:finished:context:)];
    [UIView setAnimationDelegate:self];
    [UIView commitAnimations];

}

- (id)init
{

    if(self = [super init])
    {
        mView = [[UIView alloc]initWithFrame:CGRectMake(0,0,320,440)];
        mView.backgroundColor = [UIColor whiteColor];
        mainLayer =[CALayer layer];
        // load our flake image we will use the same image over and over
        flakeImage = [UIImage imageNamed:@"apple.png"];
        [mView.layer addSublayer:mainLayer];
        // start a timet that will fire 20 times per second
        [NSTimer scheduledTimerWithTimeInterval:(0.8) target:self selector:@selector(onTimer) userInfo:nil repeats:YES];
        self.view=mView;
    }
    return self;
}

我用它来获取从屏幕的顶部到底部动画的图像的位置。 但我得到了动画图像的x和y的恒定值。

任何人都可以为此提供帮助。 谢谢你。

2 个答案:

答案 0 :(得分:0)

我从上面的代码中注意到的一件事是你每次都在mView上添加flakeView。你想要吗?请尝试使用以下代码。

- (void)onTimer
{

    int startX =round(random() % 460);  
    printf("\n ===== startX :%d",startX);
    int endX = round(random() % 460);
    printf("\n ===== endX :%d",endX);
    double scale = 1 / round(random() % 100) - 1.0;
    double speed = 1 / round(random() %100) + 1.0;
  if(flakeView == nil)
  {
    // build a view from our flake image
    flakeView = [[UIImageView alloc] initWithImage:flakeImage];
    flakeView.backgroundColor=[UIColor blueColor];
    // use the random() function to randomize up our flake attributes

    // set the flake start position
    flakeView.frame = CGRectMake(startX, -100.0, 25.0 * scale, 25.0 * scale);
    flakeView.alpha = 1.0;
    // put the flake in our main view
    [mView addSubview:flakeView];
  }
    [UIView beginAnimations:nil context:flakeView];
    [UIView setAnimationDuration:20 * speed];
    // code to get current position of image on view
    CALayer mainLayer = flakeView.layer.presentationLayer;
    CGRect layerFrame = mainLayer.frame;
    BOOL intersects = CGRectIntersectsRect(layerFrame, dragger.frame);
    // end position on screen
    flakeView.frame = CGRectMake(endX, 500.0, 15.0 * scale, 15.0 * scale);
    // set a stop callback so we can cleanup the flake when it reaches the
    // end of its animation
    [UIView setAnimationDidStopSelector:@selector(onAnimationComplete:finished:context:)];
    [UIView setAnimationDelegate:self];
    [UIView commitAnimations];

    NSLog(@"Co-ordinates of imageview : (%f, %f, %f, %f) %@",flakeView.frame.origin.x,flakeView.frame.origin.y,flakeView.frame.size.width,flakeView.frame.size.height);

}

谢谢,

答案 1 :(得分:0)

  1. 我想知道为什么你可以使用点符号来访问presentationLayer。这是一种方法,而不是CALayer的属性。
  2. 注意:“在iPhone OS 4.0及更高版本中不鼓励使用[UIView beginAnimations:context:]。您应该使用基于块的动画方法。” (Apple Docs)。如果您不知道基于块的动画是什么,请阅读:What are block-based animation methods in iPhone OS 4.0?
  3. 如果它仍无法与基于块的动画一起使用,请尝试将图层的委托设置为nil或隐式或明确使用CABasicAnimation