Search code examples
iosuiimageviewuianimation

Create Continous uiimage animation in x axis


i want to create background like http://disqus.com/ continues background but i can't get it after try with my code like this

continuesBackground =[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"pattern"]];
    continuesBackground.frame=CGRectMake(0, 56, 320, 102);
    continuesBackground.layer.zPosition=100;
    [UIView animateWithDuration:5.0
                          delay:0.0
                        options:UIViewAnimationOptionCurveLinear
                     animations:^{
                         continuesBackground.transform = CGAffineTransformMakeTranslation(0, 200);  }
                     completion:^(BOOL finish) {
                         continuesBackground.transform = CGAffineTransformMakeTranslation(200, 0);

                     }];

how to make it ? do i need nstimer?


Solution

  • Try this:

    - (void)animateImageViewFromStartPosition:(CGPoint)aStartPosition toEndPosition:(CGPoint)aEndPosition; {
      CGRect oldFrame = CGRectMake(aStartPosition.x, aStartPosition.y, continuesBackground.frame.size.width, continuesBackground.frame.size.height);
      [continuesBackground setFrame:oldFrame];
    
      [UIView animateWithDuration:5.0f
                            delay:0.0f
                          options:UIViewAnimationOptionCurveEaseInOut
                       animations:^(void){
                         CGRect newFrame = continuesBackground.frame;
                         newFrame.origin = aEndPosition;
                         continuesBackground.frame = newFrame;
                       }
                       completion:^(BOOL finished){
                         if(finished){
                           [self animateImageViewFromStartPosition:aStartPosition toEndPosition:aEndPosition];
                         }
                          }];
    }
    

    Then call the method in your viewDidLoad method:

    - (void)viewDidLoad; {
      [super viewDidLoad];
      [self animateImageViewFromStartPosition:CGPointMake(0.0f, 56.0f) toEndPosition:CGPointMake(320.0f, 56.0f)];
    }
    

    You can easily modify this to a transform, or whatever you want, and it will keep the animation going indefinitely. Hope that Helps!