2013-12-16 35 views

回答

2

您是否嘗試添加UILongPressGestureRecognizer? In viewDidLoad:

UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)]; 
longPress.delegate = self; 
[segmentedControl addGestureRecognizer:longPress]; 

不要忘了添加UIGestureRecognizerDelegate到你的頭文件。

要知道,按下其中:

- (void)longPress:(UILongPressGestureRecognizer *)gestureRecognizer { 
    CGPoint p = [gestureRecognizer locationInView:segmentedControl]; 
} 

然後,你可以檢查什麼segmentedControl段與CGPoint p匹配,檢查Y座標,例如。當它從UISegmentedControl的中線左側是段0,當它是正確的,行的是段1

+1

而在viewDidUnload –

+0

感謝您的評論將其刪除。但是我想知道長按下時段控件的索引值。 longpressgesturerecognizer運作良好。我的問題是不添加手勢,但檢查索引值。 – Akoya

+0

你是指segmentedControl.selectedSegmentIndex值? – Mathijs

0

您註冊長按UISegmentedControl

let longPress = UILongPressGestureRecognizer(target: self, action: #selector(ListQuizViewController.segmentLongPress(_:))) 
    //longPress.delegate = self; 
    self.segmentedControl.addGestureRecognizer(longPress) 
    longPress.minimumPressDuration = 1 

您獲得所選按鈕的指標如下在假設段等距

func segmentLongPress(gestureRecognizer:UILongPressGestureRecognizer) 
    { 
     let p = gestureRecognizer.locationInView(self.segmentedControl) 
     let index = Int(ceil(p.x/(self.segmentedControl.frame.width/4))) - 1 
     self.segmentedControl.selectedSegmentIndex = index 

    }