播放声音时的UIScrollView正在滚动

播放声音时的UIScrollView正在滚动

本文介绍了播放声音时的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正在滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 17:50