2012-12-07 31 views
1

如何在iPhone應用程序中隨機移動圖像陣列。如何在iPhone應用程序中隨機移動圖像陣列

貝婁代碼用於移動一個圖像。我必須使用一組圖像。

- (void)moveImage:(UIImageView *)image duration:(NSTimeInterval)duration 
     curve:(int)curve x:(CGFloat)x y:(CGFloat)y 
{ 
    [UIView beginAnimations:nil context:NULL]; 

    [UIView setAnimationDuration:duration]; 

    [UIView setAnimationCurve:curve]; 

    [UIView setAnimationBeginsFromCurrentState:YES]; 

    // The transform matrix 
    CGAffineTransform transform = CGAffineTransformMakeTranslation(x, y); 
    image.transform = transform; 

    // Commit the changes 
    [UIView commitAnimations]; 
} 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    UIImageView *imageToMove = 
    [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"fish.png"]]; 

    imageToMove.frame = CGRectMake(70, 120, 100, 100); 

    [self.view addSubview:imageToMove]; 

    [self moveImage:imageToMove duration:1.0 
       curve:UIViewAnimationCurveLinear x:0.0 y:110.0]; 

} 
+1

您應該將CAAnimationGroup用於多個對象的同時動畫。檢查此鏈接以供參考https://developer.apple.com/library/ios/#documentation/GraphicsImaging/Reference/CAAnimationGroup_class/Introduction/Introduction.html – Sumanth

回答

0

你可以使用這個它可以幫助你:

NSArray *animationArray=[NSArray arrayWithObjects: 
            [UIImage imageNamed:@"img1.png"], 
             [UIImage imageNamed:@"img2.png"], 
             [UIImage imageNamed:@"img3.png"], 
            [UIImage imageNamed:@"img4.png"], 
        nil]; 
UIImageView *animationView=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0,320, 460)]; 
    animationView.backgroundColor=[UIColor purpleColor]; 
    animationView.animationImages=animationArray; 
    animationView.animationDuration=1.5; 
animationView.animationRepeatCount=0; 
    [animationView startAnimating]; 
    [self.view addSubview:animationView];  
    [animationView release]; 

這僅僅是一個概念上的代碼,請根據您的需要進行更改。