简体   繁体   English

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

[英]Swift 4 UIButton action in UIImageView not working

I am trying to add a delete button as a subview in an image. 我试图将删除按钮添加为图像中的子视图。 This is my current structure: -> class DesignViewController: UIViewController | -> class Sticker: UIImageView, UIGestureRecognizerDelegate | -> UI button inside the Sticker 这是我当前的结构: -> class DesignViewController: UIViewController | -> class Sticker: UIImageView, UIGestureRecognizerDelegate | -> UI button inside the Sticker -> class DesignViewController: UIViewController | -> class Sticker: UIImageView, UIGestureRecognizerDelegate | -> UI button inside the Sticker

Inside Sticker class I have : 内部贴纸类,我有:

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")        
}

The buttonAction is not getting called. buttonAction没有被调用。 When I change self.addSubview(button2) line to : 当我将self.addSubview(button2)行更改self.addSubview(button2)

self.superview?.addSubview(button2)

I can see buttonAction getting called. 我可以看到buttonAction被调用。 However I would like to keep the button inside the Sticker view so that when user moves the sticker, the button moves as a subview with it. 但是,我想将按钮保留在“贴纸”视图中,以便当用户移动贴纸时,按钮随其一起作为子视图移动。

Can anyone please help and let me know how I can keep the button inside Sticker view? 谁能帮忙,让我知道如何将按钮保留在“贴纸”视图中?

You should create a protocol delegate for button action. 您应该为按钮操作创建协议委托。 This is code example: 这是代码示例:

protocol ButtonDelegate: class {
  func buttonTapped(button: UIButton)
}

class Sticker: UIImageView {

weak var delegate: ButtonDelegate?

override init(frame: CGRect) {
    super.init(frame: frame)

    addSubview(button2)
}

lazy var button2: UIButton = {
  let button = 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)        
  return button
}()

@objc func buttonAction(sender: UIButton) {
  guard let delegate = delegate else { return }
  delegate.buttonTapped(button: sender)
}

So, now go to your DesignViewControllerl, add your custom imageview class Sticker. 因此,现在转到DesignViewControllerl,添加自定义imageview类Sticker。 Don't forget to do that "imageView.delegate = self". 不要忘记这样做“ imageView.delegate = self”。 Then in extension add protocol delegate you've created before. 然后在扩展名中添加您之前创建的协议委托。 Code example: 代码示例:

class DesignViewController: UIViewController {

private lazy var sticker: Sticker = {
  let iv = Sticker(frame: view.bounds)
  iv.delegate = self
  return iv
}()

  override viewDidLoad() {
    super.viewDidLoad()

    view.addSubiew(sticker)
  }

}

extension DesignViewController: ButtonDelegate {

  func buttonTapped(button: UIButton) {
    // input your action here
  }

}

By default isUserInteractionEnabled property of UIImageView is set to false. 默认情况下, UIImageView isUserInteractionEnabled属性设置为false。 Set it to true and your button will start to respond. 将其设置为true,您的按钮将开始响应。 You can set it in code as well as in the storyboards. 您可以在代码以及情节提要中进行设置。

Also try setting the clipsToBounds property of your imageview to true. 也可以尝试将imageview的clipsToBounds属性设置为true。 It will clip your button if it is going outside of the image bounds. 如果按钮超出图像范围,它将裁剪您的按钮。 That might be one of the reason that your button is not getting touches. 这可能是您的按钮无法触摸的原因之一。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM