2011-08-13 81 views
0

HIE大家好,这里是我的代码:帧动画减少

- (void)viewDidLoad { 

    [[UIAccelerometer sharedAccelerometer] setUpdateInterval:1.0/60.0]; 
    [[UIAccelerometer sharedAccelerometer] setDelegate:self]; 
    [super viewDidLoad]; 
} 

-(void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration { 
    valueX = acceleration.x *20.0; 
    valueY = acceleration.y *-20.0; 

    int newX = (int)(imageView.center.x+valueX); 
    int newY = (int)(imageView.center.y+valueY); 

    CGPoint newCenter = CGPointMake(newX,newY); 
    imageView.center=newCenter; 

    UIImageView* imageView2 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"ball.png"]]; 
    imageView2.frame = CGRectMake(newX-12, newY-12, 24, 24); 
    [self.view addSubview:imageView2]; 
    [UIView beginAnimations:nil context:imageView2]; 
    [UIView setAnimationDuration:10.5]; 
    [UIView setAnimationCurve:UIViewAnimationCurveEaseOut]; 
    imageView2.frame = CGRectMake(newX-2, newY-2, 4, 4); 
    [imageView2 setAlpha:0.0]; 
    [UIView setAnimationDelegate:self]; 
    [UIView setAnimationDidStopSelector:@selector(removeSmoke:finished:context:)]; 
    [UIView commitAnimations]; 
    if(imageView2.alpha=0){ 
     [imageView2 removeFromSuperview]; 
    } 


    [self.view bringSubviewToFront:imageView]; 

} 

起初,我的动画是非常光滑,1.0/60.0,但几秒钟后,动画的错误和不顺利,我认为它成为1.0/5.0。我不知道为什么。我该如何解决这个问题?对不起我的英语我是法国人:/

回答

1

我不完全清楚你想要达到的目标。看起来好像每次加速度计触发时,都会添加一个新的imageView动画,并且想要根据某些规则移动它,并且缩小图像。尽管你的代码有问题。你永远不会打电话给[imageView2 release];,所以你在一段时间后可能会遇到内存问题。

您也可能在方法@selector(removeSmoke:finished:context:)中做了太多的计算,但我需要查看代码才能解决这个问题。

如果您知道您计划同时有多少个UIImageView,那么最好预先加载它们,然后将它们放在一个数组中以便于访问。这样,你可以移动一个UIImageView,并移动到下一个。

如果您正在拍摄iOS 4.0或更高版本,我还建议使用块动画。它们更易于使用,而且我发现处理器也更容易。喜欢的东西:

[UIView animateWithDuration:10.5 
        animations:^{ 
         CGRect newRect = imageView2.frame; 
         newRect.origin.x -= newX-2; 
         newRect.origin.y -= newY-2; 
         imageView2.frame = newRect; 
        } 
        completion:^(BOOL finished){ 

         [UIView animateWithDuration:0.1 
             animations:^{ 
              // This is where you complete your animation, maybe remove from view or something? 
             } 
             completion:^(BOOL finished){ 
              ; 
             }]; 
        }]; 

最后,而不是增加,并从视图中删除了,我建议你只需设置imageView2.hidden = YES;,并且这样,它不会拿得出,并具有同样的效果。希望有所帮助!