在自定义视图中定义点按手势时无法识别的选择器错误[Swift]

时间:2016-11-11 18:43:30

标签: ios swift unrecognized-selector

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

  1. closeView
  2. self.closeView
  3. ToolTipView.closeView
  4. 他们都没有奏效。你知道我做错了什么吗?

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

1 个答案:

答案 0 :(得分:0)

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

我正在改变计算自定义视图的相对位置。我正在改变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!) } 的框架,这是错误的部分。相反,我操纵了{{1}}。现在一切都按我的意愿运作。

工作版我的功能:

{{1}}