Play tick sound when UITableView scrolls (sort of like UIPickerView)

I am looking for a way to realize tick noise, which is reproduced every time a cell transmits a certain point on the screen (in the center).

I poured over the Internet, but could not figure out where to start? Any direction would be great (not looking for someone to solve it for me, just insight or advice)

Thank!

UPDATE:

Here is the code that I implemented using your method, but it does not work correctly. It seems that he did not name "Tick" nslog, which means that something in the parameters is incorrect. My tableview cells are 100 pixels high. Any advice?

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

    }


}
+5
source share
6 answers

:

@property (nonatomic, strong) NSIndexPath *currentIndexPath;

, 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;
    }
}
+6

UITableViewDelegate UIScrollViewDelegate, , , UIScrollViewDelegate - scrollViewWillBeginDragging - scrollViewDidScroll

+1

- (void)scrollViewDidScroll:(UIScrollView *)scrollView

UIScrollViewDelegate, UITableViewDelegate. contentOffset scrollView, , ( ), .

+1

contentOffset TableView scrollViewDidScroll: tableView

0

30 , tableViewDelegate

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

Now you have a content offset that assigns the correct numbers .... execute a module on contentOffset based on the height of the tableview cell and you can play the sound whenever it changes .... Remember the content offset will go from 0 - tableViewCellHeight * numberOfCells + headerHeight + footerHeight ... here is the finished method:

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;
}
0
source

Play sound in the UITableViewDelegate tableView: willDisplayCell: forRowAtIndexPath :. This method is called every time a new cell appears.

0
source

All Articles