我是新來iPhone編程。 我正在試圖做一個簡單的窗口與一個貓做兩個聲音。當你點擊貓圖標時,它應該做「miaau」,當你拖過窗口(中風)時,它應該使「mrrrr」。 它的工作原理,但總是當我嘗試使貓mrrrrr功能TouchesBegan火災和貓也做「miaaau」。Touchesbegan使用touchesmoved時總是會觸發?
如何使界面識別我只想要中風貓,而不是爲了做第一個選項而觸摸它,「miaau」?
我是新來iPhone編程。 我正在試圖做一個簡單的窗口與一個貓做兩個聲音。當你點擊貓圖標時,它應該做「miaau」,當你拖過窗口(中風)時,它應該使「mrrrr」。 它的工作原理,但總是當我嘗試使貓mrrrrr功能TouchesBegan火災和貓也做「miaaau」。Touchesbegan使用touchesmoved時總是會觸發?
如何使界面識別我只想要中風貓,而不是爲了做第一個選項而觸摸它,「miaau」?
我建議添加的NSTimer到的touchesBegan方法與小的時間間隔(比如0.1秒):
BOOL tap_event = NO; //ivar declared in header
-(void) touchesBegan:... {
tap_event = YES;
[NSTimer scheduledTimerWithTimeInterval: 0.1 target: self selector: @selector(checkTap:) userInfo: nil repeats: NO];
}
-(void) checkTap:(NSTimer*) t {
if(tap_event) //miauu here
tap_event = NO;
}
-(void) touchesMoved:... {
tap_event = NO;
//mrrrr here
}
或者作爲UIGestureRecognizers的選項檢查文檔
Max' solution是正確的。它一定會奏效。我有另一種選擇,請參閱此方法。
將您的播放器對象放在.h文件中,然後在viewDidLoad中進行分配並釋放到dealloc中。
-(void) touchesBegan:... {
//Play the miauu sound.
}
-(void) touchesMoved:... {
[self.yourPlayer stop];
//Play mrrrr.
}
編輯
.h文件中,
AVAudioPlayer *avPlayer1;
AVAudioPlayer *avPlayer2;
.m文件
-(void) touchesBegan:... {
NSString *path = [[NSBundle mainBundle] pathForResource:@"cat" ofType:@"wav"];
avPlayer1 = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
[avPlayer1 play];
}
-(void) touchesMoved:... {
[avPlayer1 stop];
NSString *path = [[NSBundle mainBundle] pathForResource:@"cat" ofType:@"wav"];
avPlayer2 = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
[avPlayer2 play];
}
-(void)dealloc
{
[avPlayer1 release];
[avPlayer2 release];
[super dealloc];
}
最大的解決方案。貓使得miauu當點擊mrrrr當衝程但我有問題: - (無效)checkKlik:(的NSTimer *)噸{ \t如果(的點擊){ \t \t如果([觸摸視圖] ==果德){ \t \t \t NSString * path = [[NSBundle mainBundle] pathForResource:@「cat」ofType:@「wav」]; AVAudioPlayer * theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL]; \t \t \t [theAudio play]; \t \t} \t \t klik = NO; \t} } 它表明我沒有聲明觸摸。 因爲我只想點擊「KOT」視圖,所以我需要使用觸摸視圖kot進行IF。 – Rafal 2011-03-08 07:58:05
因爲你讓你的功能和觸摸是touchesBegan和touchesMoved method.ok的一部分,請嘗試我的答案它可以幫助你。 – Ishu 2011-03-08 08:26:46
Ishu,當我讓你的答案如何顯示在touchesMoved米奧貓正在播放和停止它? – Rafal 2011-03-08 08:43:04
好吧,我會嘗試用BOOL的,讓你知道我的作品。非常感謝。 – Rafal 2011-03-08 07:08:54
+1它肯定會工作。 – Ishu 2011-03-08 07:10:00
如果你不想擔心計時器,你也可以使用[self performSelector:@selector(checkTap)withObject:nil afterDelay:0.1]。 – bornbnid 2011-08-30 15:00:01