0
我正在使用iOS 5來實現應用程序。iOS在一次給定時間允許一次觸摸事件
在這個應用程序中,我有4個按鈕,每個按鈕觸發一個動畫來取消隱藏UIView。但是,如果我先按一個按鈕然後按另一個按鈕,則首先出現的視圖應該消失,並且將顯示新按鈕的視圖。
我到目前爲止工作。但是,如果用戶快速點擊兩個按鈕,它將顯示兩個視圖。我如何確保只處理一次觸摸事件?
該按鈕的作用是一樣的東西:
- (void)buttonPressed:(id)sender
{
MenuButton *aButton = (MenuButton *)sender;
switch (aButton.tag) {
case 0:
if (displayingView && currentlyDisplayingView != picker1)
[self toggleView:currentlyDisplayingView atIndex:currentIndex];
[self toggleView:picker1 atIndex:aButton.tag];
currentlyDisplayingView = picker1;
currentIndex = aButton.tag;
break;
case 1:
if (displayingView && currentlyDisplayingView != picker2)
[self toggleView:currentlyDisplayingView atIndex:currentIndex];
[self toggleView:picker2 atIndex:aButton.tag];
currentlyDisplayingView = picker2;
currentIndex = aButton.tag;
break;
case 2:
if (displayingView && currentlyDisplayingView != picker3)
[self toggleView:currentlyDisplayingView atIndex:currentIndex];
[self toggleView:picker3 atIndex:aButton.tag];
currentlyDisplayingView = picker3;
currentIndex = aButton.tag;
break;
default:
break;
}
NSLog(@"Pressed %@",[buttonNames objectAtIndex:aButton.tag]);
}
而且動畫代碼:
- (void)toggleView:(UIView *)picker
atIndex:(NSInteger)index
{
if (picker) {
picker.hidden = NO;
[UIView animateWithDuration:0.5
animations:^{
picker.alpha = abs(picker.alpha - 1);
CGRect rect = picker.frame;
if (rect.size.height == 0){
rect.size.height = 76;
} else if (rect.size.height == 76) {
rect.size.height = 0;
}
picker.frame = rect;
for (int i = index+1; i < [viewButtons count]; i++) {
UIButton *aButton = [viewButtons objectAtIndex:i];
CGRect frame = aButton.frame;
if (rect.size.height == 0){
frame.origin.y -= 75;
} else if (rect.size.height == 76) {
frame.origin.y += 75;
}
aButton.frame = frame;
}
}
completion:^(BOOL success){
if (picker.alpha == 0){
picker.hidden = YES;
} else if (picker.alpha == 1) {
picker.hidden = NO;
}
displayingView = !displayingView;
}];
}
}
也許發佈您的按鈕操作代碼並查看動畫。也許有人可以深入瞭解可能存在的問題。 – picciano 2012-02-23 20:22:45
我已經添加了兩個片段。 – 2012-02-23 20:53:21
[全局停止所有觸摸檢測?](http://stackoverflow.com/questions/4822000/stop-all-touch-detection-globally) – matt 2012-02-23 21:11:02