小编Sam*_*tha的帖子

UIImageView 中的 Swift 4 UIButton 操作不起作用

我正在尝试添加删除按钮作为图像中的子视图。这是我当前的结构: -> class DesignViewController: UIViewController | -> class Sticker: UIImageView, UIGestureRecognizerDelegate | -> UI button inside the Sticker

在 Sticker 类中我有:

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    let button2 = UIButton(frame: CGRect(x: 100, y: 100, width: 100, height: 50))
    button2.backgroundColor = .red
    button2.setTitle("Delete", for: .normal)
    button2.tag = 23
    button2.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)        
    self.addSubview(button2)
}

@objc func buttonAction(sender: UIButton!) {
    print("Button tapped")        
}
Run Code Online (Sandbox Code Playgroud)

没有buttonAction被调用。当我将self.addSubview(button2)线路更改为:

self.superview?.addSubview(button2)
Run Code Online (Sandbox Code Playgroud)

我可以看到buttonAction有人打电话。不过,我想将按钮保留在贴纸视图内,以便当用户移动贴纸时,按钮会作为子视图随之移动。

谁能帮忙并让我知道如何将按钮保留在贴纸视图中?

uibutton uiimageview ios swift4

0
推荐指数
1
解决办法
1356
查看次数

标签 统计

ios ×1

swift4 ×1

uibutton ×1

uiimageview ×1