2010-09-28 4 views
1
- (void)moveImage:(UIImageView *)image duration:(NSTimeInterval)duration 
      curve:(int)curve x:(CGFloat)x y:(CGFloat)y 
{ 
    // Setup the animation 
    [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:@"icon.png"]]; 
    imageToMove.frame = CGRectMake(10, 10, 20, 20); 
    [self.view addSubview:imageToMove]; 

    // Move the image 
    [self moveImage:imageToMove duration:3.0 
       curve:UIViewAnimationCurveLinear x:50.0 y:50.0]; 

}

J'ai essayé ce one.It fonctionne très bien à un moment donné de gauche à right.But je dois déplacer l'image de gauche et de droite à quitté à plusieurs reprises.image Déplacer de gauche à droite et de droite à gauche dans l'iphone

Merci d'avance.

Répondre

0

y ne créez pas un NSTimer qui appelle la fonction de manière répétée toutes les 5 s (où 5 est le délai requis pour se déplacer de gauche à droite) et cette fonction vérifie.

if(image.origin.x< 0) 
    //move image to right 
else if(image.origin.x > 320) 
    // move image to left. 

L'un ou l'NSTimer ou dans votre fonction de déplacement appeler un [self performSelector:];

1
[UIView animateWithDuration:timeDuration 
          delay:0.0f 
         options:UIViewAnimationOptionRepeat | UIViewAnimationOptionAutoreverse | UIViewAnimationCurveLinear 
        animations:^{ 
         [iVBackgroundImg setFrame:YOUR_FRAME]; 
        } 
        completion:nil]; 
    [UIView commitAnimations]; 
Questions connexes