Click on the already selected segment control swift

柔情痞子 提交于 2019-12-24 15:53:58

问题


I have an UISegmentedControl with 4 segments. When it is selected, it should pop up view. And when the pop up dismissed, and trying to click on same segment index it should again show the pop up. By using following does not have any action on click of same segment index after pop up dissmissed.

segHeader.addTarget(self, action: Selector("valuechange:"), forControlEvents: .AllEvents)

or

segHeader.addTarget(self, action: Selector("valuechange:"), forControlEvents: .ValueChanged)

回答1:


After lot of research am able to solve it.

Am writing answer here may it will be helpfull to others in future.

when the segment controll is pressed write below code.

@IBAction func segHeaderPressed(sender: UISegmentedControl) {

if segHeader.selectedSegmentIndex == 0{

let sortedViews = sender.subviews.sort( { $0.frame.origin.x < $1.frame.origin.x } )

        for (index, view) in sortedViews.enumerate() {
            if index == sender.selectedSegmentIndex {
                view.backgroundColor = iOSBlueColor // UIColor.blueColor()
            } else {
                view.backgroundColor = UIColor.clearColor()
            }
        }

selectedInd = -1

//your code ... }else if(segHeader.selectedSegmentIndex == 1){

        let sortedViews = sender.subviews.sort( { $0.frame.origin.x < $1.frame.origin.x } )

        for (index, view) in sortedViews.enumerate() {
            if index == sender.selectedSegmentIndex {
                view.backgroundColor = iOSBlueColor //UIColor.blueColor()
            } else {
                view.backgroundColor = UIColor.clearColor()
            }
        }

// your code ... }else if(segHeader.selectedSegmentIndex == 2){

let sortedViews = sender.subviews.sort( { $0.frame.origin.x < $1.frame.origin.x } )

        for (index, view) in sortedViews.enumerate() {
            if index == sender.selectedSegmentIndex {
                view.backgroundColor = iOSBlueColor //UIColor.blueColor()
            } else {
                view.backgroundColor = UIColor.clearColor()
            }
        }

}else if(segHeader.selectedSegmentIndex == 3){

let sortedViews = sender.subviews.sort( { $0.frame.origin.x < $1.frame.origin.x } )

        for (index, view) in sortedViews.enumerate() {
            if index == sender.selectedSegmentIndex {
                view.backgroundColor = iOSBlueColor //UIColor.blueColor()
            } else {
                view.backgroundColor = UIColor.clearColor()
            }
        }

} }

basically segmented controll functionality is once it is selected if you tap again same it will not respond. so am just changing the background colors of selected segment. hope it helps someone.




回答2:


Subclass UISegmentedControl and override touchesEnded

class DemoSegmentedControl: UISegmentedControl {
    override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
            self.sendActionsForControlEvents(UIControlEvents.ValueChanged);
            super.touchesEnded(touches, withEvent: event);
    }
}

This has a bug, when touching different segments it will trigger twice your action for UIControlEvents.ValueChanged but when touching same segment multiple time it will behave as expected. You can try to "filter" events using timestamp, if triggering action twice is too expensive for you.




回答3:


If I understand correctly you want to pop/show a view when a segmented control is selected right?

SWIFT 3

In that case, you are overcomplicating things, is much easier to use:

viewName.isHidden = false/true

This way you can hide or show the view/views base on the action applied to a segmented control index. For example:

@IBAction func segmentedAction(_ sender: AnyObject) {

    if segmentedControl.selectedSegmentIndex == 0 {

        viewName.isHidden = false

    }
    else {

        viewName.isHidden = true
    }
}



回答4:


I improve tesla's answers little bit.

If you send .touchUpInside event and bind it instead of .valueChanged, it triggered just one time in each situation.

Furthermore, you should call sendActions function after touchesEnded. If not, segmentedControl.selectedSegmentIndex will return old value.

class ClickableSegmentedControl: UISegmentedControl {
  override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
    super.touchesEnded(touches, with: event);
    self.sendActions(for: UIControl.Event.touchUpInside);
  }
}


来源:https://stackoverflow.com/questions/34513359/click-on-the-already-selected-segment-control-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!