I am using the following code in order to create a UIScrollView:
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 < 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];
The Question is: How do I implement an NSTimer in order to change the page every 4 seconds?
[NSTimer scheduledTimerWithTimeInterval:4.0f
target:self
selector:@selector(updateCounter:)
userInfo:nil
repeats:YES];
I am struggling to write the updateCounter method. Please help
- (void)updateCounter:(NSTimer *)theTimer {
NSInteger numberOfViews = 3;
int i = 0; i++;
if (i < 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; }
OK, so with this code, NSTimer changes from view 1 to view 2, but it stops, it does not change after the next 4 seconds to view 3. What I want to do is to change from view 1, to view 2, after 4 seconds, then to view 3, after 4 seconds, and so on. I want to have as many views as I need.
you are doing everything right. Now you need to implement
updateCounterso that the scroll of the scrollView happens automatically. maintain global width counter. Keep updating this counter in every call toupdateCounter. You could do this like so –This way you keep cycling between the pages automatically. Hoep this works, all of it is typed freehand. But the concept is correct.