Note that there are some explanatory texts on larger screens.

plurals
  1. POChange View in UIScrollView with NSTimer
    text
    copied!<p>I am using the following code in order to create a <code>UIScrollView</code>:</p> <pre><code>UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)]; scroll.pagingEnabled = YES; NSInteger numberOfViews = 3; for (int i = 0; i &lt; numberOfViews; i++) { CGFloat yOrigin = i * self.view.frame.size.width; UIView *awesomeView = [[UIView alloc] initWithFrame:CGRectMake(yOrigin, 0, self.view.frame.size.width, self.view.frame.size.height)]; awesomeView.backgroundColor = [UIColor colorWithRed:0.5/i green:0.5 blue:0.5 alpha:1]; [scroll addSubview:awesomeView]; } scroll.contentSize = CGSizeMake(self.view.frame.size.width * numberOfViews, self.view.frame.size.height); [self.view addSubview:scroll]; </code></pre> <p>The Question is: How do I implement an <code>NSTimer</code> in order to change the page every 4 seconds? </p> <pre><code> [NSTimer scheduledTimerWithTimeInterval:4.0f target:self selector:@selector(updateCounter:) userInfo:nil repeats:YES]; </code></pre> <p>I am struggling to write the updateCounter method. Please help</p> <pre><code>- (void)updateCounter:(NSTimer *)theTimer { NSInteger numberOfViews = 3; int i = 0; i++; if (i &lt; numberOfViews) { yOrigin = i * self.view.frame.size.width; [scroll scrollRectToVisible:CGRectMake(yOrigin, 0, self.view.frame.size.width, self.view.frame.size.height) animated:YES]; } return; } </code></pre> <p>OK, so with this code, <code>NSTimer</code> changes from <em>view 1</em> to <em>view 2</em>, but it stops, it does not change after the next 4 seconds to <em>view 3</em>. What I want to do is to change from <em>view 1</em>, to <em>view</em> 2, after 4 seconds, then to <em>view 3</em>, after 4 seconds, and so on. I want to have as many views as I need.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload