【Swift】UISegmentedControl에서 같은 세그먼트를 두 번 탭하면 선택 해제하는 방법
데모

절차
1. 커스텀 클래스 만들기
먼저 UISegmentedControl의 사용자 정의 클래스를 만듭니다.
⌘+N으로 신규 파일 작성 화면으로 갑니다.
Cocoa Touch Class를 선택하고 Next를 클릭합니다.

다음에 클래스명을 적당히 붙이고, Subclass에 UISegmentedControl를 선택해 Next.
다음 화면에서 파일을 저장할 위치를 지정하여 만듭니다.

그리고 작성한 커스텀 클래스를 이하와 같이 기술합니다.
CustomSegmentedControl.swiftimport UIKit
class CustomSegmentedControl: UISegmentedControl {
var oldValue : Int!
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
self.oldValue = self.selectedSegmentIndex
super.touchesBegan(touches, with: event )
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesEnded(touches, with: event )
if self.oldValue == self.selectedSegmentIndex
{
sendActions(for: .valueChanged )
}
}
}
2. 커스텀 클래스 적응
설치한 UISegmentedControl에 방금 작성한 커스텀 클래스를 적응시킵니다.

3. 끈
UISegmentedControl을 ViewController에 연결합니다.
설치된 UISegmentedControl을 처리하기 위해 Outlet으로 연결.

탭 이벤트를 얻기 위해 Action에서도 연결.

4. 탭했을 때의 처리를 기술
값을 넣는 변수를 준비해, 탭시의 처리를 기술해 완성입니다.
ViewController.swiftimport UIKit
class ViewController: UIViewController {
@IBOutlet weak var sexSC: CustomSegmentedControl!
var sex: String = ""
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func sexSCDidTapped(_ sender: CustomSegmentedControl) {
let selectedIndex = sexSC.selectedSegmentIndex
if sex == sexSC.titleForSegment(at: selectedIndex)! {
sexSC.selectedSegmentIndex = UISegmentedControlNoSegment // 選択解除
sex = ""
} else {
sex = sexSC.titleForSegment(at: selectedIndex)!
}
print(sex)
}
}
참고 : detect event when tapped on already selected segment - Stack Overflow
Reference
이 문제에 관하여(【Swift】UISegmentedControl에서 같은 세그먼트를 두 번 탭하면 선택 해제하는 방법), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다
https://qiita.com/Simmon/items/ce52f3396e04cb5f6e97
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)
1. 커스텀 클래스 만들기
먼저 UISegmentedControl의 사용자 정의 클래스를 만듭니다.
⌘+N으로 신규 파일 작성 화면으로 갑니다.
Cocoa Touch Class를 선택하고 Next를 클릭합니다.

다음에 클래스명을 적당히 붙이고, Subclass에 UISegmentedControl를 선택해 Next.
다음 화면에서 파일을 저장할 위치를 지정하여 만듭니다.

그리고 작성한 커스텀 클래스를 이하와 같이 기술합니다.
CustomSegmentedControl.swift
import UIKit
class CustomSegmentedControl: UISegmentedControl {
var oldValue : Int!
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
self.oldValue = self.selectedSegmentIndex
super.touchesBegan(touches, with: event )
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesEnded(touches, with: event )
if self.oldValue == self.selectedSegmentIndex
{
sendActions(for: .valueChanged )
}
}
}
2. 커스텀 클래스 적응
설치한 UISegmentedControl에 방금 작성한 커스텀 클래스를 적응시킵니다.

3. 끈
UISegmentedControl을 ViewController에 연결합니다.
설치된 UISegmentedControl을 처리하기 위해 Outlet으로 연결.

탭 이벤트를 얻기 위해 Action에서도 연결.

4. 탭했을 때의 처리를 기술
값을 넣는 변수를 준비해, 탭시의 처리를 기술해 완성입니다.
ViewController.swift
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var sexSC: CustomSegmentedControl!
var sex: String = ""
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func sexSCDidTapped(_ sender: CustomSegmentedControl) {
let selectedIndex = sexSC.selectedSegmentIndex
if sex == sexSC.titleForSegment(at: selectedIndex)! {
sexSC.selectedSegmentIndex = UISegmentedControlNoSegment // 選択解除
sex = ""
} else {
sex = sexSC.titleForSegment(at: selectedIndex)!
}
print(sex)
}
}
참고 : detect event when tapped on already selected segment - Stack Overflow
Reference
이 문제에 관하여(【Swift】UISegmentedControl에서 같은 세그먼트를 두 번 탭하면 선택 해제하는 방법), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/Simmon/items/ce52f3396e04cb5f6e97텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)