在UITableView滚动时播放滴答声(有点像UIPickerView)

Kyl*_*man 6 audio uitableview ios

我正在寻找一种方法来实现每次单元格通过屏幕上的特定点(中心)时播放的刻度噪声.

我一直在网上倾泻但是无法弄清楚从哪里开始?任何方向都会很棒(不是找人为我解决,只是一些见解或建议)

谢谢!

更新:

这是我使用您的方法实现的代码,但它无法正常工作.似乎永远不会调用"Tick"nslog,这意味着参数中的某些内容不正确.我的tableview单元格高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;

    }


}
Run Code Online (Sandbox Code Playgroud)

lna*_*ger 6

首先向视图控制器添加一个属性,以将单元格的索引路径存储在表格的中心:

@property (nonatomic, strong) NSIndexPath *currentIndexPath;
Run Code Online (Sandbox Code Playgroud)

然后,确保您的视图控制器采用UIScrollViewDelegate协议,并且您的tableView可以通过self.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;
    }
}
Run Code Online (Sandbox Code Playgroud)