播放声音时的UIScrollView正在滚动

编程入门 行业动态 更新时间:2024-10-10 23:19:33
本文介绍了播放声音时的UIScrollView正在滚动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个可以滚动到两侧(只左右,不要上下)滚动视图。我想打一个短的声音(不到​​一秒)每当滚动视图移动x像素到两边。

I have a scroll view that can be scrolled to the sides (only left and right, not up and down). I want to play a short sound (less than a second) whenever the scroll view is moved X pixels to either side.

如何才能做到这一点? code样品将大大AP preciated ...

How can this be done? Code samples will be greatly appreciated...

谢谢,

推荐答案

下面是code我用:

我添加了SoundEffect.h和SoundEffect.m文件到我的项目(​​你可以在网上找到)。然后,我创建了一个声音效果实例:

I added the SoundEffect.h and SoundEffect.m files to my project (you can find them online). Then, I created a sound effect instance:

SoundEffect *soundEffect;

然后,我安装我的UIViewController作为我的UIScrollView的代表加入< UIScrollViewDelegate> 到视图控制器的.h文件和设置的相关出口UIScrollView的。

Then, I setup my UIViewController as the delegate of my UIScrollView by adding <UIScrollViewDelegate> to the .h file of the view controller and setting the relevant outlet of the UIScrollView.

在 - (无效)viewDidLoad中的方法,我初始化我的声音效果:

In the -(void)viewDidLoad method, I initialized my sound effect:

NSBundle *mainBundle = [NSBundle mainBundle]; soundEffect = [[SoundEffect alloc] initWithContentsOfFile:[mainBundle pathForResource:@"Tik" ofType:@"wav"]];

然后,我实现了这两种方法:

And then, I implemented these two methods:

#pragma mark - #pragma mark Scroll View Delegate Methods - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView { lastScrollPosition = scrollView.contentOffset.x / 55; } - (void)scrollViewDidScroll:(UIScrollView *)scrollView { if ((int)(scrollView.contentOffset.x / 55) != lastScrollPosition1) { lastScrollPosition1 = scrollView.contentOffset.x / 55; [soundEffect1 play]; } }

我需要的音效,以每55个像素火到两个方向,但你可以改变这符合您需求的恒定值。它为我的伟大工程,并希望,这将有助于其他人也...

I needed the sound effect to fire every 55 pixels to either direction, but you can change this to a constant value that fits your needs. It works great for me, and hopefully, it will help others as well...

更多推荐

播放声音时的UIScrollView正在滚动

本文发布于:2023-11-05 18:15:56,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1561521.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:声音   UIScrollView

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!