
UIControl::show를 사용한 버튼의 터치 이벤트가 잘 안 먹히는 이유는 UIControl::show가 버튼을 화면에 바로 보여주기 때문에 버튼의 기본적인 동작이 제대로 작동하지 않는 때문입니다.
UIControl::show는 버튼을 화면에 바로 보여주기 때문에 버튼의 touchesBegan, touchesMoved, touchesEnded, touchesCancelled 메서드가 호출되지 않습니다. 따라서 버튼의 터치 이벤트가 잘 안 먹히는 것입니다.
이 문제를 해결하기 위해서는 버튼을 화면에 직접 추가하는 방법을 사용하는 것이 좋습니다.
예를 들어, 다음 코드를 사용할 수 있습니다.
#hostingforum.kr
swift
let button = UIButton()
button.frame = CGRect(x: 100, y: 100, width: 200, height: 50)
button.setTitle("버튼", for: .normal)
button.addTarget(self, action: #selector(buttonTapped), for: .touchUpInside)
view.addSubview(button)
이 코드는 버튼을 화면에 직접 추가하고 버튼의 터치 이벤트를 처리하는 메서드를 설정합니다.
또한, 버튼의 touchesBegan, touchesMoved, touchesEnded, touchesCancelled 메서드를 직접 구현하여 터치 이벤트를 처리할 수도 있습니다.
예를 들어, 다음 코드를 사용할 수 있습니다.
#hostingforum.kr
swift
override func touchesBegan(_ touches: Set, with event: UIEvent?) {
print("touchesBegan")
}
override func touchesMoved(_ touches: Set, with event: UIEvent?) {
print("touchesMoved")
}
override func touchesEnded(_ touches: Set, with event: UIEvent?) {
print("touchesEnded")
}
override func touchesCancelled(_ touches: Set, with event: UIEvent?) {
print("touchesCancelled")
}
이 코드는 버튼의 터치 이벤트를 직접 처리합니다.
이러한 방법을 사용하면 UIControl::show를 사용한 버튼의 터치 이벤트가 잘 안 먹히는 문제를 해결할 수 있습니다.
2025-06-02 20:14