2011-04-16 45 views
0

我正在使用一个应用程序,在那个图像是使用动画从屏幕的顶部到底部进行动画。需要动画时获得图像的帧坐标

代码:

- (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的一致值。

任何人都可以帮忙。 感谢U.

+0

当你想要那些坐标?在每个循环之后(调用onTimer)? – Ravin 2011-04-16 07:02:51

+0

是的,我想在每个循环之后进行坐标。 – Lakshmi 2011-04-16 09:05:50

回答

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

} 

感谢,

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?

如果仍然不能与基于块的动画工作,尝试将层的委托设置为nil或使用CABasicAnimation或隐或显式。