相关文章推荐
买醉的小熊猫  ·  TS中Array.reduce提示没有与此调 ...·  1 年前    · 
大气的铁板烧  ·  idea maven ...·  1 年前    · 
打盹的稀饭  ·  常数吞吐量分布式的使用示例_性能测试-阿里云 ...·  2 年前    · 
失恋的青椒  ·  FreeRTOS两种延时函数的区别是什么? ...·  2 年前    · 
胆小的李子  ·  Use MySQL 8 ...·  2 年前    · 
Code  ›  为什么没有调用touchesBegan侦听器(Swift 4)?开发者社区
按钮
https://cloud.tencent.com/developer/ask/sof/114843805
淡定的核桃
1 年前
首页
学习
活动
专区
工具
TVP 最新优惠活动
返回腾讯云官网
提问

问 为什么没有调用touchesBegan侦听器(Swift 4)?

Stack Overflow用户
提问于 2018-01-28 12:37:17
EN

我创建了简单的按钮- class Btn: UIControl ,然后添加到父视图。在我的 Btn 类中,我有 override func touchesBegan 。当我点击按钮时,为什么 touchesBegan 不打电话呢?正如我想的那样,我的 Btn 扩展了 UIControl , Btn 不得不调用 touchesBegan 。

我的代码:

导入UIKit

class ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
//        add my button
        let button = Btn()
        view.addSubview(button)
class Btn: UIControl {
    fileprivate let button: UIButton = {
        let swapButton = UIButton()
        let size = CGFloat(50)
        swapButton.frame.size = CGSize(width: size, height: size)
        swapButton.backgroundColor = UIColor.green
        return swapButton
    override init(frame: CGRect) {
        super.init(frame: frame)
        addSubview(button)
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        addSubview(button)
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        super.touchesBegan(touches, with: event)
        //        Why it not called?
        print("test")
}
1 946 0 票数 0
EN
ios
swift

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-01-28 13:04:22

根据您的代码,使用下面的操场代码,您将了解正在发生的事情。当您的控件想要处理 touchesBegan 回调时,它的内部有一个 UIButton ,它会消耗事件本身。在下面的示例中,内部按钮是 Btn 大小的四分之一(红色部分),如果单击它,“测试”文本将不会被打印出来。 Btn 空间的其余部分(绿色部分)不包含将消耗 touchesBegan 的任何其他视图,因此单击那里将打印"test“文本。

我建议你看看 应答链 。

import PlaygroundSupport
import UIKit
class ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        //        add my button
        let button = Btn(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
        view.addSubview(button)
class Btn: UIControl {
    fileprivate let button: UIButton = {
        let swapButton = UIButton()
        let size = CGFloat(50)
        swapButton.frame.size = CGSize(width: size, height: size)
        swapButton.backgroundColor = UIColor.green
        return swapButton
    override init(frame: CGRect) {
        super.init(frame: frame)
        addSubview(button)
        self.backgroundColor = .red
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        addSubview(button)
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        super.touchesBegan(touches, with: event)
 
推荐文章
买醉的小熊猫  ·  TS中Array.reduce提示没有与此调用匹配的重载解析_其它_脚本之家
1 年前
大气的铁板烧  ·  idea maven 程序包org.openqa.selenium不存在-CSDN博客
1 年前
打盹的稀饭  ·  常数吞吐量分布式的使用示例_性能测试-阿里云帮助中心
2 年前
失恋的青椒  ·  FreeRTOS两种延时函数的区别是什么? - 知乎
2 年前
胆小的李子  ·  Use MySQL 8 json_table to dynamically extract JSON keys and nested value - Stack Overflow
2 年前
今天看啥   ·   Py中国   ·   codingpro   ·   小百科   ·   link之家   ·   卧龙AI搜索
删除内容请联系邮箱 2879853325@qq.com
Code - 代码工具平台
© 2024 ~ 沪ICP备11025650号