我有一個分段控制,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或其他選擇實現觸摸事件效果?
觸動開始,細分還沒有被選中,所以你不能得到它。 – kientux
@ kientux有沒有什麼辦法可以在選擇索引之前?我想要觸發事件時觸發 – vin
我認爲只用UISegmentedControl時,觸發事件無能爲力,因爲只有觸摸時才選擇段。如果你真的想這樣做,你可以在每個片段上放置一個透明的按鈕,當按鈕被觸及時,調用'setSelectedSegmentIndex:'。然後你有兩個觸摸事件和選定的索引。 – kientux