7

セルが画面上の特定のポイント (中央) を通過するたびに再生されるティック ノイズを実装する方法を探しています。

私はウェブ上に注いでいますが、どこから始めればよいかわかりませんか? どんな方向性でも素晴らしいでしょう(私のためにそれを解決する人を探しているのではなく、洞察やアドバイスだけです)

ありがとう!

アップデート:

あなたの方法を使用して実装したコードを次に示しますが、正しく機能しません。パラメータの何かが間違っていることを意味する「Tick」nslogを呼び出すことは決してないようです。テーブルビュー セルの高さは 100 ピクセルです。何かアドバイス?

- (void) scrollViewDidScroll:(UIScrollView *)scrollView {



}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    self.myTimer = [NSTimer scheduledTimerWithTimeInterval:1/30.0 target:self selector:@selector(checkTableViewScrollPosition) userInfo:nil repeats:YES];
}


- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
{
    if (self.myTimer)
        [self.myTimer invalidate];
    self.myTimer = nil;
}


int currentContentOffset = 0;
int tableviewCellHeight = 100;
int thresholdValue = 50;


- (void) checkTableViewScrollPosition {


    int contentOffsetValue = _contentTableView.contentOffset.y;

     NSLog(@"%d", contentOffsetValue);


    if ((contentOffsetValue + tableviewCellHeight / 2) % tableviewCellHeight <= thresholdValue && currentContentOffset != contentOffsetValue ) {
        NSLog(@"Tick!");
        NSLog(@"%d", contentOffsetValue);


        currentContentOffset = _contentTableView.contentOffset.y;

    }


}
4

6 に答える 6

6

まず、View Controllerにプロパティを追加して、セルのインデックスパスをテーブルの中央に保存します。

@property (nonatomic, strong) NSIndexPath *currentIndexPath;

次に、View ControllerがUIScrollViewDelegateプロトコルを採用していること、およびself.tableviewプロパティを介してtableViewにアクセスできることを確認します(または、以下のコードをtableViewの適切なプロパティに変更します)。

次に、UIScrollViewDelegate:から次のメソッドを実装します。

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    // Find the indexPath of the cell at the center of the tableview
    CGPoint tableViewCenter = self.tableview.center;
    tableViewCenter = [self.tableview convertPoint:tableViewCenter fromView:self.tableview.superview];
    NSIndexPath *centerCellIndexPath = [self.tableview indexPathForRowAtPoint:tableViewCenter];

    // "Tick" if the cell at the center of the table has changed
    if ([centerCellIndexPath compare:self.currentIndexPath] != NSOrderedSame)
    {
        NSLog(@"Tick");
        self.currentIndexPath = centerCellIndexPath;
    }
}
于 2013-02-20T03:08:54.020 に答える
1

を実装する

- (void)scrollViewDidScroll:(UIScrollView *)scrollView

UITableViewDelegateプロトコルが準拠するUIScrollViewDelegateプロトコルの一部であるメソッド。実装contentOffsetでは、scrollViewのを確認し、コンテンツがセルの高さ(上または下)にオフセットされるたびに、サウンドを再生します。

于 2013-02-19T21:21:11.207 に答える
1

プロトコルはにUITableViewDelegate準拠してUIScrollViewDelegateいるため、このプロトコルを採用している場合は、UIScrollViewDelegate - scrollViewWillBeginDraggingとを実装し- scrollViewDidScrollてスクロールを検出できます。

于 2013-02-19T21:16:33.010 に答える
0

UITableViewDelegate メソッド tableView:willDisplayCell:forRowAtIndexPath: 内でサウンドを再生します。このメソッドは、新しいセルが表示されるたびに呼び出されます。

于 2014-06-17T02:39:18.567 に答える
0

tableViewデリゲートcontentOffsetのメソッドでTableViewのプロパティを確認することから始めることができますscrollViewDidScroll:

于 2013-02-19T21:13:46.337 に答える
0

tableViewDelegate メソッドが返されたら、1 秒あたり 30 フレームで実行されるようにタイマーを設定します。

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    self.myTickTimer = [NSTimer scheduledTimerWithTimeInterval:1/30.0 target:self selector:@selector(checkTableViewScrollPosition) userInfo:nil repeats:YES];
}

tableViewDidBeginScroller を作成し、デリゲート メソッドに到達したらタイマーを停止します

- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
{
    if (myTickTimer)
        [myTickTimer invalidate];
    self.myTickTimer = nil;
}

次に、tableview.contentOffset を取得して印刷し、スクロールすると変化するすべてのコンテンツ オフセットを表示します...

- (void) checkTableViewScrollPosition
{
    NSLog(@"tableview.contentOffset = %@", tableview.contentOffset);
}

これで、コンテンツ オフセットが正しい数値を放出するようになりました....テーブルビュー セルの高さに基づいて contentOffset でモジュラスを実行すると、変更されるたびにサウンドを再生できるようになります....コンテンツ オフセットが0 - tableViewCellHeight*numberOfCells + headerHeight + footerHeight... 完成したメソッドは次のとおりです。

int currentContentOffset = 0;
int tableviewCellHeight = 44;
int thresholdValue = 5;
void checkTableViewScrollPosition
{
    NSLog(@"tableview.contentOffset = %@", tableview.contentOffset);
    if ((tableview.contentOffset+tableviewCellHeight/2.0) % tableviewCellHeight <= threshHoldValue &&
        currentContentOffset != tableview.contentOffset)
        NSLog(@"Tink");
        [[NSSound soundNamed:@"Tink"] play];

        currentContentOffset = tableview.contentOffset;
}
于 2013-02-19T21:39:10.753 に答える