2

アルファベット順のセクションに分割されたテーブル ビューがあります。各セクションのフッターにある UIImage ビューにアニメーション バナーを表示しています。UIImage ビューがクリックされたときにどの画像が表示されるかを判断する必要があります。startAnimating を呼び出す直前にタイマーを設定します。タイマーは、アニメーションの変化と同じ速度で 5 秒ごとに起動しますが、タイマーの起動ははるかに高速です。その5秒間に2、3回発射することもあります。タイマーとアニメーションを開始するコードは次のとおりです。

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger) section {

...

    imgAdBar = [[bannerView alloc]initWithFrame:CGRectMake(footer.frame.origin.x,footer.frame.origin.y,footer.frame.size.width,footer.frame.size.height)];

    imgAdBar.image=[UIImage imageNamed:[NSString stringWithFormat:@"%@", [animationArray objectAtIndex:0]]]; 
    [imgAdBar saveBannerArray:animationArray];
    [imgAdBar setUserInteractionEnabled:YES];
    imgAdBar.animationImages = images;
    imgAdBar.animationDuration=[images count]*5;
    imgAdBar.animationRepeatCount=0;
    timerCount=0;
    [NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:@selector(timerRunning:) userInfo:nil repeats:YES];
    [imgAdBar startAnimating];
    [footer addSubview:imgAdBar];
    footer.backgroundColor = [UIColor clearColor];

}
return footer;
}

そして、ここにセレクタがあります:

-(void)timerRunning:(NSTimer*)theTimer
{
NSLog(@"timerCount=%d",timerCount);

imgAdBar.timerCount=timerCount;
if (timerCount==numAnimationImages) {
    timerCount=0;
}
NSLog(@"currentImage=%@",[animationArray objectAtIndex:timerCount]);
timerCount++;
}

そのタイマーを使用して画像配列にインデックスを付け、どれが表示されているかを確認できるようにする予定です。発火すべきときに発火しない理由を知っている人はいますか? ご協力ありがとうございます。

4

3 に答える 3

1

ヘッダー ファイルで NSTimer をプロパティとして宣言します。

@propterty (nonatomic, retain) NSTimer *someTimer;

タイマーを起動する行で

someTimer = [NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:@selector(timerRunning:) userInfo:nil repeats:YES];

-(void)viewDidUnload で解放することを忘れないでください

[someTimer release];
于 2012-05-22T11:09:51.670 に答える
1

クリックがなければの値を使用しないのでtimerCount、タイマーで更新する必要はありません。アニメーションを開始する瞬間の時間を保存するだけで十分です。各画像が 5 秒間表示されることがわかっているので、クリックした時間とアニメーションを開始した時間の差 (秒単位) を 5 で割って、表示されている画像のインデックスを計算できます。画像の総数で割った余り。

10 個の画像があり、それぞれがループで 5 秒間表示されているとします。また、アニメーションが で始まったとしましょう08:15:5108:19:23ここで、アニメーションが開始されてから、または212数秒後にクリックがあったとします。5 で除算すると、次のようになり42ます。10 で割った余りを取ると、 が得られ2ます。したがって、ユーザーがアニメーション ループの 3 番目の画像をクリックしたことがわかります (通常、インデックスはゼロから始まります)。

于 2012-05-23T00:44:26.813 に答える
1

プロパティとして NSTimer を使用する必要があります... viewForFooterInSection 多くのセクションで数回呼び出されるため、再初期化する前に無効にするか、null をチェックする必要があります。コードは次のとおりです。 無効化:

    NSTimer *timer; // declare in ViewDidLoad
  // code in viewForFooterInSection
    [timer invalidate];
        timer = [NSTimer scheduledTimerWithTimeInterval: 0.5
                         target: self
                         selector: @selector(handleTimer:)
                         userInfo: nil
                         repeats: YES];

ゼロをチェック

if (timer == nil) {

        timer = [NSTimer scheduledTimerWithTimeInterval: 0.5
                                                 target: self
                                               selector: @selector(handleTimer:)
                                               userInfo: nil
                                                repeats: YES];
    }

それが役立つことを願っています..

于 2012-05-22T13:17:14.350 に答える