views:

37

answers:

1

I am trying to create a slide show using NSTimer... But the following code is not scrolling the images at regular intervals...

- (void)tilePages 

{

// Calculate which pages are visible
CGRect visibleBounds = pagingScrollView.bounds;
int firstNeededPageIndex = floorf(CGRectGetMinX(visibleBounds) / CGRectGetWidth(visibleBounds));
int lastNeededPageIndex  = floorf((CGRectGetMaxX(visibleBounds)-1) / CGRectGetWidth(visibleBounds));
firstNeededPageIndex = MAX(firstNeededPageIndex, 0);
lastNeededPageIndex  = MIN(lastNeededPageIndex, [self imageCount] - 1);

// Recycle no-longer-visible pages 
for (ImageScrollView *page in visiblePages) {
    if (page.index < firstNeededPageIndex || page.index > lastNeededPageIndex) {
        [recycledPages addObject:page];
        [page removeFromSuperview];
    }
}
[visiblePages minusSet:recycledPages];

// add missing pages
for (int index = firstNeededPageIndex; index <= lastNeededPageIndex; index++) {
    if (![self isDisplayingPageForIndex:index]) {
        ImageScrollView *page = [self dequeueRecycledPage];
        if (page == nil) {
            page = [[[ImageScrollView alloc] init] autorelease];
        }
        [self configurePage:page forIndex:index];
        [pagingScrollView addSubview:page];
        [visiblePages addObject:page];

    }


}    

}

In ViewWillAppear method i have used...

timer = [NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:@selector(timerFired:) userInfo:nil repeats:YES];

and

to call the event to be fired at intervals i have used...

-(void)timerFired:(NSTimer *)timer

{

[self tilePages];

}

I have tried to debug. The event is getting fired at 3 secs, but the tilePages is not getting called. I have used the same for scrollViewDidScroll method where in the tilePages is getting executed well... What might be the problem??? Please help...

A: 

Calling [self tilePages]; will not show a new page. It looks like you're basing this on the WWDC 2010 sample code for paging and zooming images. Because I'm familiar with that code I may be able to help some.

You should make your own method that changes the frame of the pagingScrollView. Then you should call this method from the timerFired method. Replace [self tilePages] with [self changePage] and add the following code:

-(void)changePage{
   CGRect pageFrame = pagingScrollView.frame;
   pageFrame.size.width -= (2 * 10);
pageFrame.origin.x = (pagingScrollView.frame.size.width * aVariableHoldingTheCurrentPageNumber)+20;
[pagingScrollView scrollRectToVisible:pageFrame animated:YES];
}

Basically just calculate the page frame you want to scroll to and then call scrollRectToVisible.

I hope this helps.

Jonah