开发者

UIScrollView with images slideshow and timers

i'm trying to implement a slideshow of images with previews horizontaly and vertically, and i want to be able to do some stuff when the user stop scrolling and after 5 seconds do this stuff. I use 2 timers to handle this but i need some synchronization or maybe i'm in the wrong way! Why i use 2 timers : because i want to handle scrolling in the main view (-(void)touchesBegan and -(void)touchesEnded)and scrolling on the scrollView( UIScrollView delegate). my code is below and i hope someone can help :). Thanks.

// 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 me开发者_Python百科thods

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


I think that you could get rid of the two timers and define an intermediate function, say enableTimerFor:, that you could call from -scrollViewDidEndDecelerating: and -touchesEnded:withEvent: instead of creating there the 2 timers.

enableTimerFor: would basically create the timer and schedule it:

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

And it would be called like this:

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

Like that, you would have no problem in syncing. Notice also that I thought of enableTimerFor: as having a parameter that allows you to identify whether it was called from one function or the other, but you can decide easily to get rid of it in case you don't need to distinguish the two cases.

I am not sure that this suggestion could work well for you, since I don't know all the details of your solution, but still I think this is the most direct suggestion given the information I have.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜