Move image from left to right and right to left in iphone

一笑奈何 提交于 2020-01-05 07:10:54

问题


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

}

I have tried this one.It works fine at one time from left to right.But i need to move the image from left and right to left repeatedly.

Thanks in advance.


回答1:


y dont you create a NSTimer that repeatedly calls the function every 5 secs(where 5 is the time reqd to move from left to right) and then this function will check.

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

Either user NSTimer or in your move function call a [self performSelector:];




回答2:


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


来源:https://stackoverflow.com/questions/3811597/move-image-from-left-to-right-and-right-to-left-in-iphone

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!