0

我正在尝试实现带有水平和垂直预览的图像幻灯片,并且我希望能够在用户停止滚动并在 5 秒后做这些事情时做一些事情。我使用 2 个计时器来处理这个问题,但我需要一些同步,或者我可能走错了路!为什么我使用 2 个计时器:因为我想处理主视图(-(void)touchesBegan-(void)touchesEnded)中的滚动以及滚动视图()上的滚动UIScrollView delegate。我的代码在下面,我希望有人可以提供帮助:)。谢谢。

// This is the slideShow view controller
// It display all the images with preview (right, left, up and down)
@interface ImageSlideShowViewController : UIViewController<UIScrollViewDelegate>
{

    UIScrollView *scrollViewForSlideShow;    
    // Timers
    NSTimer *autoscrollTimer;
    NSTimer *mainViewTimer;
}

@end

@implementation ImageSlideShowViewController

#pragma mark Touch handling

-(void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event{
    ....
    // Invalidate all timers
   [autoscrollTimer invalidate];
   autoscrollTimer = nil;
   [mainViewTimer invalidate];
   mainViewTimer = nil;
}

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {

    ....

    assert(mainViewTimer == nil);

    mainViewTimer = [NSTimer scheduledTimerWithTimeInterval:5.0
                                                       target:self
                                                     selector:@selector(allViewTimerFireMethod:)
                                                     userInfo:nil
                                                      repeats:NO];   


    // prev image horizontal
        ...

    // next image horizontal
        ....    

    // prev image vertical
            ...

    // next image vertical
            ....
}   


#pragma mark -
#pragma mark UIScrollView delegate

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    // Invalidate all timers
    [autoscrollTimer invalidate];
    autoscrollTimer = nil;
    [mainViewTimer invalidate];
    mainViewTimer = nil;
}

- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView 
{
    //Doing some management stuff here
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    if (!scrollView.dragging)
    {
        //Doing some management stuff here

        assert(autoscrollTimer == nil);


        autoscrollTimer = [NSTimer scheduledTimerWithTimeInterval:5.0
                                                           target:self
                                                         selector:@selector(timerFireMethod:)
                                                         userInfo:nil
                                                          repeats:NO];

    }
}

#pragma mark -
#pragma mark Timers methods

- (void)timerFireMethod:(NSTimer*)theTimer
{
    autoscrollTimer = nil;
    // Call a method that dispaly the image
}

- (void)allViewTimerFireMethod:(NSTimer *)theTimer
{
    mainViewTimer = nil;
    // Call a method that dispaly the image (same as the method called above)
}

@end
4

1 回答 1

0

我认为您可以摆脱两个计时器并定义一个中间函数,例如enableTimerFor:,您可以从中调用-scrollViewDidEndDecelerating:-touchesEnded:withEvent:不是在那里创建两个计时器。

enableTimerFor:基本上会创建计时器并安排它:

-(void)enableTimerFor:(NSUInteger)callerId {
    viewTimer = [NSTimer scheduledTimerWithTimeInterval:5.0
                                                   target:self
                                                 selector:@selector(timerFired:)
                                                 userInfo:nil
                                                  repeats:NO];   
}

-(void)timerFired {
   // do whatever you need to
}

它会被这样调用:

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    ....
    [self enableTimerFor:kTouchesEnded];
    ....
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    if (!scrollView.dragging) {
        //Doing some management stuff here
        [self enableTimerFor:kScrollEnded];
    }
    ....
}

像这样,你在同步时不会有问题。另请注意,我认为enableTimerFor:有一个参数可以让您识别它是从一个函数调用还是从另一个函数调用,但您可以轻松决定摆脱它,以防您不需要区分这两种情况。

我不确定这个建议是否适合你,因为我不知道你的解决方案的所有细节,但我仍然认为这是我所掌握的信息中最直接的建议。

于 2011-05-24T20:54:50.840 回答