2011-04-16 76 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或隱或顯式。

相關問題