且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

当UIScrollview在iPhone SDK中滚动时如何添加声音?

更新时间:2023-09-23 07:53:09

UIScrollViewDelegate添加到您的界面,然后在滚动处于scrollViewWillBeginDragging状态的任务中播放声音,并在scrollViewDidEndDragging状态停止滚动. /p>

您可以使用 AVAudioPlayer 在iOS上播放声音设备

更新:

在滚动视图即将开始滚动内容时通知委托.

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView

此外,在滚动视图中拖动结束时告诉委托人.

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate

在您的代码中添加这些内容&设置您的自定义行为

This is my code for adding sound while UIScrollview is scrolling in iPhone SDK. In this code, how to add the sound while using touch-events in scrolling? Please give me your idea and suggestion or sample.

const CGFloat kScrollObjHeight  = 151.0;
const CGFloat kScrollObjWidth   =320.0;    
const NSUInteger kNumImages  = 5;

- (void)layoutScrollImages
{
  UIImageView *view = nil;
  NSArray *subviews = [scrollView1 subviews];
  CGFloat curXLoc = 0;
  for (view in subviews)
    {
      if ([view isKindOfClass:[UIImageView class]] && view.tag > 0)
       {
        CGRect frame = view.frame;
        frame.origin = CGPointMake(curXLoc, 0);
        view.frame = frame;
        curXLoc += (kScrollObjWidth);
       }
    }
   [scrollView1 setContentSize:CGSizeMake((kNumImages * kScrollObjWidth), [scrollView1 bounds].size.height)];
 }

- (void)viewDidLoad
 {
   self.view.backgroundColor = [UIColor clearColor];
   [scrollView1 setBackgroundColor:[UIColor whiteColor]];
   [scrollView1 setCanCancelContentTouches:NO];
   scrollView1.indicatorStyle = UIScrollViewIndicatorStyleWhite;
   scrollView1.clipsToBounds = YES; 
   scrollView1.scrollEnabled = YES;
   scrollView1.pagingEnabled = YES;
   NSUInteger i;
   for (i = 1; i <= kNumImages; i++)
   {
     NSString *imageName = [NSString stringWithFormat:@"snap%d.jpg", i];
     UIImage *image = [UIImage imageNamed:imageName];
     UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
     CGRect rect = imageView.frame;
     rect.size.height = kScrollObjHeight;
     rect.size.width = kScrollObjWidth;
     imageView.frame = rect;
     imageView.tag = i;
     [scrollView1 addSubview:imageView];
     [imageView release];
     [self layoutScrollImages];
   }
   [super viewDidLoad];
}

add UIScrollViewDelegate to your interface and then play sound while the scrolling is in task in scrollViewWillBeginDragging state and stop scrolling while in scrollViewDidEndDragging state.

You can use AVAudioPlayer to play sound on iOS devices

UPDATE:

Tells the delegate when the scroll view is about to start scrolling the content.

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView

Also, Tells the delegate when dragging ended in the scroll view.

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate

Implement these in your code & put in your custom behavior