Reputation: 7224
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;
}
}
Upvotes: 7
Views: 2187
Reputation: 1
Play the sound within the UITableViewDelegate method tableView:willDisplayCell:forRowAtIndexPath:. This method gets called each time a new cell is about to come into view.
Upvotes: 0
Reputation: 25740
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;
}
}
Upvotes: 6
Reputation: 696
Set up a timer to run at 30 frames per second once the tableViewDelegate method returns
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
self.myTickTimer = [NSTimer scheduledTimerWithTimeInterval:1/30.0 target:self selector:@selector(checkTableViewScrollPosition) userInfo:nil repeats:YES];
}
tableViewDidBeginScroller and then stop the timer once you reach the delegate method
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
{
if (myTickTimer)
[myTickTimer invalidate];
self.myTickTimer = nil;
}
Then get the tableview.contentOffset and print it out to show you all the content offset as it is changing as you scroll...
- (void) checkTableViewScrollPosition
{
NSLog(@"tableview.contentOffset = %@", tableview.contentOffset);
}
Now you have the content offset emmitting the right numbers.... do a modulus on the contentOffset based on the tableview cell height and you will be able to then play a sound whenever it changes.... recall the content offset will go from 0 - tableViewCellHeight*numberOfCells + headerHeight + footerHeight... here is the completed 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;
}
Upvotes: 0
Reputation: 319
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.
Upvotes: 1
Reputation: 5499
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
Upvotes: 1
Reputation: 3586
You can start by checking the contentOffset
property of your TableView in the scrollViewDidScroll:
method of your tableView delegate
Upvotes: 0