-2

这是我在 iPhone SDK 中滚动 UIScrollview 时添加声音的代码。在这段代码中,如何在滚动中使用触摸事件时添加声音?请给我你的想法和建议或样品。

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];
}
4

2 回答 2

1

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

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

更新:

当滚动视图即将开始滚动内容时告诉委托。

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView

此外,在滚动视图中拖动结束时告诉代理。

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

在您的代码中实现这些并放入您的自定义行为

于 2012-05-31T09:53:59.853 回答
0

你桅杆使用 UIScrollViewDelegate 方法

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
   //play sound here 
}
于 2012-05-31T12:42:53.763 回答