Search code examples
uiscrollviewaudiouiscrollviewdelegate

Playing a sound at certain points on a UIScrollView


I'm trying to figure out how to play a sound when a UIScrollView point lines up with a certain image, or actually, play a sound at a certain point. I have a scroll view that can be scrolled to the sides (only left and right, not up and down). I have spent some time researching this but found nothing. I did find this: http://cl.ly/65FM When I applied this, every time I would drag the UIScrolView it would play the sound at every .x point making it sound really bad therefore didn't work. Anyone know how I can perform this action? My goal is to play a short sound (less than a second) whenever the scroll view is moved X pixels to either side. How can this be done? Code samples will be greatly appreciated...


Solution

  • I figured it out. I managed rot get it working now like this.

    static NSInteger previousPage = 0;
    CGFloat pageWidth = 64;
    float fractionalPage = scrollView.contentOffset.x / pageWidth;
    NSInteger page = lround(fractionalPage);
    if (previousPage != page) {
        [scrollViewSound play];
        previousPage = page;
    }
    

    Now my goal is to figure out when the UIScrollView is aligned with a certain image it performs an action.