2014-04-06 80 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 
}];