使用 NSTimer 进行幻灯片放映时出现问题
我正在尝试使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
调用[selftilePages];不会显示新页面。看起来您是基于 WWDC 2010 分页和缩放图像的示例代码。因为我熟悉该代码,所以我也许可以帮助一些人。
您应该创建自己的方法来更改 pagingScrollView 的框架。然后您应该从timerFired 方法调用此方法。将 [selftilePages] 替换为 [selfchangePage] 并添加以下代码:
基本上只是计算要滚动到的页框,然后调用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:
Basically just calculate the page frame you want to scroll to and then call scrollRectToVisible.
I hope this helps.