I am looking for a way to implement a tick noise that plays every time a cell passes a specific point on the screen (the center).
I have been pouring over the web but cant figure out where to start? Any direction would be great (not looking for someone to solve it for me, just some insight or advice)
Thanks!
UPDATE:
Here is the code I have implemented using your method, but it doesn't work properly. It never seems to call the "Tick" nslog which means something in the parameters is incorrect. My tableview cells are 100 pixels tall. 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;
}
}
Start by adding a property to your view controller to store the index path of the cell at the center of the table:
@property (nonatomic, strong) NSIndexPath *currentIndexPath;
Then, make sure that your view controller adopts the UIScrollViewDelegate
protocol, and that your tableView is accessible via the self.tableview property (or change the code below to the appropriate property for the tableView).
Then, implement the following method from the 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;
}
}
the UITableViewDelegate
protocol conforms to UIScrollViewDelegate
, so if you are adopting this protocol you can implement the UIScrollViewDelegate
- scrollViewWillBeginDragging
and - scrollViewDidScroll
to detect the scrolling
Implement the
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
method that is part of the UIScrollViewDelegate protocol which the UITableViewDelegate protocol conforms to.
In your implementation check the contentOffset
of the scrollView, whenever the content is offset the height of a cell (either up or down), play your sound.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With