2011-08-13 77 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;,並且這樣,它不會拿得出,並具有同樣的效果。希望有所幫助!