在iphone中从左到右,从右到左移动图像

时间:2010-09-28 09:56:32

标签: iphone animation uiimageview

- (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];  

}

我试过这个。从左到右一次工作正常。但我需要反复从左到右移动图像。

提前致谢。

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

你不创建一个每5秒重复调用一次该函数的NSTimer(其中5是从左向右移动的时间)然后该函数将检查。

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

用户 NSTimer 或在您的移动功能中调用 [self performSelector:];