2014-04-06 82 views
-1

我需要一些圖像隨機出現在屏幕上,然後開始移動,並檢測結束移動。我如何得到這個?我的意思是檢測結束移動UIView?檢測越界屏幕

這些我的代碼:

imageNames = @[@"some_array_picture"]; 

    image = [[NSMutableArray alloc] init]; 
    for (int i = 0; i < imageNames.count; i++) { 
     [image addObject:[UIImage imageNamed:[imageNames objectAtIndex:i]]]; 
    } 

    leftX = arc4random_uniform(6) + 10; 
    leftY = arc4random()%500; 
    rightX = arc4random_uniform(220) + 20; 
    rightY = arc4random()%500; 
    tempX = rightX; 
    tempY = rightY; 

    animationImageView = [[UIImageView alloc] initWithFrame:CGRectMake(rightX, rightY, 75, 57.5)]; 
    animationImageView.animationImages = image; 
    animationImageView.animationDuration = 1; 

    [self.view addSubview:animationImageView]; 
    [self.view sendSubviewToBack:animationImageView]; 
    [animationImageView startAnimating]; 

    [UIView beginAnimations:nil context:nil]; 
    [UIView setAnimationDuration:10.0f]; 
    animationImageView.frame = CGRectMake(leftY, leftY, animationImageView.frame.size.width, animationImageView.frame.size.height); 
    [UIView commitAnimations]; 

,因爲我需要在運動結束水平旋轉,有這樣的代碼:

animationImageView.transform = CGAffineTransformScale(animationImageView.transform, -1.0, 1.0); 

回答

0

在你的「標題」你要求檢測時,視圖在屏幕邊界之外,但在你問題中,你要求檢測動畫的結束。

對於後者,我會建議使用animateWithDuration:animations:completion:。它在動畫完成時調用完成塊。

[UIView animateWithDuration:10.0 
    animations:^{ animationImageView.frame = CGRectMake(leftY, leftY, animationImageView.frame.size.width, animationImageView.frame.size.height); } 
    completion:^(BOOL finished){ animationImageView.transform = CGAffineTransformScale(animationImageView.transform, -1.0, 1.0); 
}]; 

Apple建議使用基於塊的動畫UIView的開發者文檔中:

有兩種不同的方式來啓動動畫:

  • 在iOS 4的,後來,使用基於塊的動畫方法。 (推薦)
  • 使用開始/提交動畫方法。
+0

沒有流暢的動畫在這裏,在我的例子animationImageView移動平滑(( – monroe

+0

MB應使用在屏幕邊界檢測碰撞? – monroe

-1
[UIView animateWithDuration:10 
         animations:^{ 
          animationImageView.frame = CGRectMake(leftY, leftY, animationImageView.frame.size.width, animationImageView.frame.size.height); 

         } 
         completion:^(BOOL finished){ 

// do something here : it is called at the end of the animation 
}];