0

水平方向と垂直方向のプレビューを使用して画像のスライドショーを実装しようとしています。ユーザーがスクロールを停止して 5 秒後にこの操作を実行したときに、いくつかの操作を実行できるようにしたいと考えています。これを処理するために2つのタイマーを使用していますが、同期が必要か、間違った方法をとっている可能性があります! 2 つのタイマーを使用する理由: メイン ビュー (-(void)touchesBeganおよび-(void)touchesEnded) でのスクロールと scrollView( 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

2 つのタイマーを取り除き、そこから 2 つのタイマーを作成する代わりに呼び出す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:また、ある関数から呼び出されたのか、別の関数から呼び出されたのかを識別できるパラメーターがあると考えましたが、2 つのケースを区別する必要がない場合は、簡単に取り除くことができます。

私はあなたの解決策の詳細をすべて知っているわけではないので、この提案があなたにとってうまくいくかどうかはわかりませんが、それでも私が持っている情報を考えると、これは最も直接的な提案だと思います.

于 2011-05-24T20:54:50.840 に答える