Frage

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];
                }
            }];
        }
    }];
}
War es hilfreich?

Lösung

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];
                }
            }];
        }
    }];
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top