0

I'm trying to achieve an infinitely scrolling background by having two UIViews scroll and replace each other like a conveyor belt. This is my code so far, I can't seem to get it to work

- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. bg1 = [[UIView alloc] initWithFrame:self.view.frame]; [bg1 setBackgroundColor:[UIColor redColor]]; bg2 = [[UIView alloc] initWithFrame:self.view.frame]; [bg2 setBackgroundColor:[UIColor blueColor]]; [self.view addSubview:bg1]; [self.view addSubview:bg2]; [self.view bringSubviewToFront:bg1]; [self animate]; } - (void)animate { [UIView animateWithDuration:3000 animations:^{ bg1.frame = CGRectOffset(bg1.frame, 0, bg1.frame.size.height); }completion:^(BOOL done) { if (done) { bg1.frame = CGRectOffset(bg1.frame, 0, -bg1.frame.size.height); [self.view sendSubviewToBack:bg1]; [UIView animateWithDuration:3000 animations:^{ bg2.frame = CGRectOffset(bg2.frame, 0, bg2.frame.size.height); }completion:^(BOOL done) { if (done) { bg2.frame = CGRectOffset(bg2.frame, 0, -bg2.frame.size.height); [self.view sendSubviewToBack:bg2]; [self animate]; } }]; } }]; } 

1 Answer 1

2

my bad! i thought withDuration was in ms. It's in seconds!

- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. bg1 = [[UIView alloc] initWithFrame:self.view.frame]; [bg1 setBackgroundColor:[UIColor redColor]]; bg2 = [[UIView alloc] initWithFrame:self.view.frame]; [bg2 setBackgroundColor:[UIColor blueColor]]; [self.view addSubview:bg1]; [self.view addSubview:bg2]; [self.view bringSubviewToFront:bg1]; [self animate]; } - (void)animate { [UIView animateWithDuration:3 animations:^{ bg1.frame = CGRectOffset(bg1.frame, 0, bg1.frame.size.height); }completion:^(BOOL done) { if (done) { bg1.frame = CGRectOffset(bg1.frame, 0, -bg1.frame.size.height); [self.view sendSubviewToBack:bg1]; [UIView animateWithDuration:3 animations:^{ bg2.frame = CGRectOffset(bg2.frame, 0, bg2.frame.size.height); }completion:^(BOOL done) { if (done) { bg2.frame = CGRectOffset(bg2.frame, 0, -bg2.frame.size.height); [self.view sendSubviewToBack:bg2]; [self animate]; } }]; } }]; } 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.