使用 NSTimer 进行幻灯片放映时出现问题

发布于 2024-09-25 06:40:08 字数 1777 浏览 4 评论 0原文

我正在尝试使用 NSTimer 创建幻灯片...但以下代码不会定期滚动图像...

- (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];

    }


}    

}

在我使用的 ViewWillAppear 方法中...

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

调用要每隔一段时间触发的事件 i已经使用过...

-(void)timerFired:(NSTimer *)timer

{

[self tilePages];

}

我尝试过调试。该事件在 3 秒后被触发,但tilePages 没有被调用。我对scrollViewDidScroll方法使用了相同的方法,其中tilePages执行得很好......可能是什么问题???请帮忙...

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...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

回眸一笑 2024-10-02 06:40:08

调用[selftilePages];不会显示新页面。看起来您是基于 WWDC 2010 分页和缩放图像的示例代码。因为我熟悉该代码,所以我也许可以帮助一些人。

您应该创建自己的方法来更改 pagingScrollView 的框架。然后您应该从timerFired 方法调用此方法。将 [selftilePages] 替换为 [selfchangePage] 并添加以下代码:

-(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];
}

基本上只是计算要滚动到的页框,然后调用scrollRectToVisible。

我希望这有帮助。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文