gpt4 book ai didi

ios - 在自定义 View 中定义点击手势时出现无法识别的选择器错误 [Swift]

转载 作者:行者123 更新时间:2023-11-30 12:54:44 25 4
gpt4 key购买 nike

我正在从 xib 创建自定义 View 。我想在触摸内部时关闭 View ,但无法识别选择器。我用它作为;

  1. 关闭 View
  2. self.closeView
  3. ToolTipView.closeView

它们都不起作用。你知道我做错了什么吗?

<小时/>
class ToolTipView: UIView {

@IBOutlet private var contentView:UIView?

override init(frame: CGRect) { // for using CustomView in code
super.init(frame: frame)
self.commonInit()
}

required init?(coder aDecoder: NSCoder) { // for using CustomView in IB
super.init(coder: aDecoder)
self.commonInit()
}

private func commonInit() {
NSBundle.mainBundle().loadNibNamed("ToolTipView", owner: self, options: nil)
guard let content = contentView else { return }
content.frame = self.bounds
content.autoresizingMask = [.FlexibleHeight, .FlexibleWidth]
self.addSubview(content)
}

func showTip(viewToAlign: UIView){

//some unrelated code

UIApplication.sharedApplication().keyWindow!.addSubview(contentView!)

contentView!.userInteractionEnabled = true
let tapGesture = UITapGestureRecognizer.init(target: contentView, action: #selector(self.closeView))
contentView!.addGestureRecognizer(tapGesture)

}

func closeView() {
self.removeFromSuperview()
}
}

最佳答案

事实证明,它与我所说的不相关代码的部分代码有关。

我正在更改自定义 View 的相对位置的计算。我正在更改 contentView 的框架,这是错误的部分。相反,我操纵了self。现在一切都按我想要的方式进行。

我的功能的工作版本:

func showTip(viewToAlign: UIView, viewToAdd: UIView){

self.userInteractionEnabled = true

let relativeFrame = viewToAlign.convertRect(viewToAlign.bounds, toView: nil)
let relativeCenter = viewToAlign.convertPoint(viewToAlign.bounds.origin, toView: nil)

self.frame = CGRectMake(relativeFrame.minX - (self.frame.size.width + 5), relativeCenter.y - self.frame.size.height/2 , self.frame.size.width, self.frame.size.height)

self.layer.masksToBounds = false
self.layer.shadowOffset = CGSizeMake(0, 0)
self.layer.shadowRadius = 5
self.layer.shadowOpacity = 0.5

viewToAdd.addSubview(self)

tapGesture = UITapGestureRecognizer.init(target: self, action: #selector(closeView))
viewToAdd.addGestureRecognizer(tapGesture!)
}

关于ios - 在自定义 View 中定义点击手势时出现无法识别的选择器错误 [Swift],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40554253/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com