这是我在UIScrollview在iPhone SDK中滚动时添加声音的代码。在此代码中,如何在滚动时使用触摸事件添加声音? 请告诉我你的想法和建议或样品。
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];
}
答案 0 :(得分:1)
将UIScrollViewDelegate
添加到您的界面,然后在滚动处于scrollViewWillBeginDragging
状态的任务时播放声音,并在处于scrollViewDidEndDragging
状态时停止滚动。
您可以使用AVAudioPlayer在iOS设备上播放声音
<强>更新强>
当滚动视图即将开始滚动内容时告诉委托。
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
此外,在滚动视图中结束拖动时告诉委托。
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
在您的代码中实现这些&amp;放入你的自定义行为
答案 1 :(得分:0)
你使用UIScrollViewDelegate方法
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
//play sound here
}