2015-10-23 111 views
0

我有一個分段控制,5段..我想要一些事件被觸發時觸發發生在任何部分,我的意思是點擊並按住,沒有舉起手指。 當用戶擡起手指,它應該重置有什麼辦法我可以註冊觸摸下來UISegmentedControl

我一直在使用的touchesBegan和touchesEnded試過,但我不得到的touchesBegan當前的selectedIndex,這裏是我的代碼

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 

    long oldValue = self.selectedSegmentIndex; 
    [super touchesBegan:touches withEvent:event]; 
    if (oldValue == self.selectedSegmentIndex) 
     [self sendActionsForControlEvents:UIControlEventValueChanged]; 
} 

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    NSLog(@"touchesEnded"); 
    NSLog(@"selectedIndex%lu",self.selectedSegmentIndex); 
    [super touchesEnded:touches withEvent:event]; 
    self.selectedSegmentIndex = -1; 
} 

有什麼辦法爲segmentedControl或其他選擇實現觸摸事件效果?

+0

觸動開始,細分還沒有被選中,所以你不能得到它。 – kientux

+0

@ kientux有沒有什麼辦法可以在選擇索引之前?我想要觸發事件時觸發 – vin

+0

我認爲只用UISegmentedControl時,觸發事件無能爲力,因爲只有觸摸時才選擇段。如果你真的想這樣做,你可以在每個片段上放置一個透明的按鈕,當按鈕被觸及時,調用'setSelectedSegmentIndex:'。然後你有兩個觸摸事件和選定的索引。 – kientux

回答

0

您可以使用touchDown:IBAction選擇器。

+0

它不工作,我已經嘗試過,UISegmentedControl只適用於valueChanged選擇器 – vin

+0

檢查此線程 - http://stackoverflow.com/questions/1620972/uisegmentedcontrol-register-taps-on-selected-segment – nanjunda

0

你可以試着重寫:

- (void) setSelectedSegmentIndex:(NSInteger)toValue 
0

坦率地說,我不認爲這是可能的UISegmentedControl做。在調用touchesEnded:withEvent:之前,您不會獲得選定的段索引。

如果你想得到touchesBegan:withEvent:,我的建議是寫你自己的自定義視圖,看起來像UISegmentedControl,並給你回電touchesBegan:withEvent:

正如上面的kientux建議的那樣,在UISegmentedControl的每個段的頂部添加透明視圖或按鈕可能是解決您的問題的另一種方法,但這將非常棘手!

祝你好運!

相關問題