6

Hi i want to get event when i touch on already selected segment.

i have implemented below solution

import UIKit

class MARSSegmentController: UISegmentedControl {

    /*
    // Only override drawRect: if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func drawRect(rect: CGRect) {
    // Drawing code
    }
    */

    override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
        let oldValue:NSInteger = self.selectedSegmentIndex
        super.touchesBegan(touches, withEvent: event)
        if oldValue == self.selectedSegmentIndex{
            sendActionsForControlEvents(UIControlEvents.ValueChanged)
        }
    }    
} 

this works fine but ValueChanged event gets executed twice if i am tapping on non selected segment.

Implementing tap gesture is not working. means when i tap on non selected segment it shows old selected segment when used tap gesture.

If any solution please suggest.

Dattatray Deokar
  • 1,923
  • 2
  • 21
  • 31
  • Forgive me if I'm wrong but I think that'll run on touch down and touch up. – Tobias May 14 '15 at 12:53
  • You can check the UIEvent first if its type of value changed then don't call this sendActionsForControlEvents(UIControlEvents.ValueChanged) – Avinash Tag May 14 '15 at 12:55

3 Answers3

12

I realize this is an older question, but I ran across the same problem where I needed to detect if the existing segment was selected as well as the selected segment changing.

The solution above was close, but here is what worked for me. Capture the existing, selected segment index on touchesBegan and then in touchesEnded, compare the old value to the current selected segment index. If they are the same, I manually trigger the .ValueChanged event. Note: this is Swift 2.0 syntax.

class SegmentedControlExistingSegmentTapped : UISegmentedControl
{
    // captures existing selected segment on touchesBegan
    var oldValue : Int!

    override func touchesBegan( touches: Set<UITouch>, withEvent event: UIEvent? )
    {
        self.oldValue = self.selectedSegmentIndex
        super.touchesBegan( touches , withEvent: event )
    }

    // This was the key to make it work as expected
    override func touchesEnded( touches: Set<UITouch>, withEvent event: UIEvent? )
    {
        super.touchesEnded( touches , withEvent: event )

        if self.oldValue == self.selectedSegmentIndex
        {
            sendActionsForControlEvents( .ValueChanged )
        }
    }
}
Dan Nichols
  • 769
  • 1
  • 7
  • 19
1

Only putself.nameSegmentedControl.isMomentary = true

With your method associate to valueChanges, the tap is detected.

0

I think your problem stands on the fact that if oldValue == self.selectedSegmentIndex will always return true because of let oldValue:NSInteger = self.selectedSegmentIndex. So all you need is to change your if condition with if oldValue == selectedValue where selectedValue is value with which you consider segment selected

Zell B.
  • 10,266
  • 3
  • 40
  • 49