objective-c - UIScrollview 在 iphone sdk 中滚动时如何添加声音?

标签 objective-c ios uiscrollview touch-event audio

这是我在 iPhone SDK 中滚动 UIScrollview 时添加声音的代码。在这段代码中,如何在滚动中使用触摸事件时添加声音?
请给我你的想法和建议或 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];
}

最佳答案

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

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

更新:

当 ScrollView 即将开始滚动内容时告诉委托(delegate)。
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
此外,在 ScrollView 中拖动结束时告诉代理。
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
在您的代码中实现这些并放入您的自定义行为

关于objective-c - UIScrollview 在 iphone sdk 中滚动时如何添加声音?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10831215/

相关文章:

iphone - OS 3.0 中的快速枚举

ios - 如何更改 Google map 上的语言?

ios - Swift - AVPlayer 加载失败,出现错误 Error Domain=NSURLErrorDomain Code=-999 "cancelled"

swift - 水平 UIScrollView 幻灯片上的增量变量

objective-c - Objective-c 中的正则表达式无法按预期工作

objective-c - 在 Objective-C 中,重写合成 setter 的正确方法是什么?

ios - 是否可以使用从单个模态视图 Controller 返回到同一源 View Controller 的多个实例之一的展开转场?

ios - 我应该传输 AVPlayer 还是 AVPlayerItem?

iPhone SDK ScrollView 显示额外空间

ios - contentOffset 和 contentInset 如何在 iOS 7 中为 UIScrollView 工作?