2016-06-15 31 views
0

我試圖用自定義UIView顯示一些內容彈出,到目前爲止我做得很好,我想通過觸摸彈出視圖來釋放彈出窗口,我無法將其視爲成功,我對新語言很陌生,可以請你幫我這個,所有的代碼只能從谷歌只複製,用xib自定義uiview創建的tapgesture和touch事件不起作用嗎?

代碼對於自定義UIView

import UIKit 

class PopUpView: UIView { 
    var view = UIView() 
    var _myTap: UITapGestureRecognizer? 
    // this name has to match your class file and your xib file 
    @IBOutlet weak var lblContent: UILabel! 

    override init(frame: CGRect) { 
     super.init(frame: frame) 
     setup() 
    } 
    required init?(coder aDecoder: NSCoder) { 
     super.init(coder: aDecoder) 
     setup() 
    } 
    override func awakeFromNib() 
    { 
     self.userInteractionEnabled = true 
    } 
    func _myHandleTap(sender: UITapGestureRecognizer) { 
     if sender.state == .Ended { 
      print("_myHandleTap(sender.state == .Ended)") 
      sender.view!.backgroundColor 
       = UIColor(red: CGFloat(drand48()), green: CGFloat(drand48()), blue: CGFloat(drand48()), alpha: 1.0); 
      self .hideInView() 
     } 
    } 
    func setup() { 
     // setup the view from .xib 
     view = loadViewFromNib() 
     view .userInteractionEnabled = true 
     self .userInteractionEnabled = true 
     view.backgroundColor = UIColor.clearColor() 
     _myTap = UITapGestureRecognizer(target: self 
      , action: #selector(_myHandleTap(_:))) 
     _myTap!.numberOfTapsRequired = 1 
     view.addGestureRecognizer(_myTap!) 

    } 

    func loadViewFromNib() -> UIView { 
     // grabs the appropriate bundle 
     let bundle = NSBundle(forClass: self.dynamicType) 
     let nib = UINib(nibName: "PopUpView", bundle: bundle) 
     let view = nib.instantiateWithOwner(self, options: nil)[0] as! UIView 
     return view 
    } 
    func showInView(currentView:UIView,content:String) { 
     view.frame = currentView.bounds 
     currentView .userInteractionEnabled = true 
     lblContent.text = content 
     UIApplication.sharedApplication().keyWindow!.addSubview(view) 
     view.alpha = 0 
     UIView.animateWithDuration(0.2, delay: 0.2, options: [.CurveEaseInOut], animations: { 
      self.view.alpha = 1 
      }, completion: nil) 

    } 
    func hideInView() { 
     view.alpha = 0 
     UIView.animateWithDuration(0.2, delay: 0.2, options: [.Repeat, .CurveEaseInOut], animations: { 
      self.view.alpha = 0 
      self.view .removeFromSuperview() 
      }, completion: nil) 

    } 
    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) { 
     self .hideInView() 
    } 
    func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWithGestureRecognizer otherGestureRecognizer: UIGestureRecognizer) -> Bool { 

     return true 

    } 
} 

訪問它從我的UIViewController

@IBAction func doForgotAction(sender: AnyObject) { 
//  warning: could not load any Objective-C class information. This will significantly reduce the quality of type information available. 
     let custom = PopUpView() 
     custom .showInView(self .view, content: "welcome") 

    } 
+0

是你的函數_myHandleTap被調用嗎? –

+0

那不叫我已經檢查了斷點。 –

回答

1

最後我得到了我的答案, 的主要問題是事件不會被接受,無論是其由廈門國際銀行建立或編程

我已經改變了所有的代碼下面

import UIKit 

class PopUpView: UIView { 
    var view = UIView() 
    // this name has to match your class file and your xib file 
    @IBOutlet weak var containerView: UIView! 
    @IBOutlet weak var lblContent: UILabel! 
    @IBOutlet weak var _myTap: UITapGestureRecognizer! 
    override init(frame: CGRect) { 
     super.init(frame: frame) 
     setup() 
    } 
    required init?(coder aDecoder: NSCoder) { 
     super.init(coder: aDecoder) 
     setup() 
    } 
    override func awakeFromNib() 
    { 
     super.awakeFromNib() 
    } 
    @IBAction func tapGestureAction(sender: AnyObject) { 
     self .hideInView() 
    } 
    func setup() { 
     // setup the view from .xib 
     view = loadViewFromNib() 
     view.frame = bounds 
     self .userInteractionEnabled = true 
     view.backgroundColor = UIColor.greenColor() 
     view.autoresizingMask = [UIViewAutoresizing.FlexibleWidth, UIViewAutoresizing.FlexibleHeight] 
     addSubview(view) 
    } 

    func loadViewFromNib() -> UIView { 
     // grabs the appropriate bundle 
     let bundle = NSBundle(forClass: self.dynamicType) 
     let nib = UINib(nibName: "PopUpView", bundle: bundle) 
     let view = nib.instantiateWithOwner(self, options: nil)[0] as! UIView 
     return view 
    } 
    func showInView(currentView:UIView,content:String) { 
     self.frame = currentView.bounds 
     self .layoutIfNeeded() 
     lblContent.text = content 
     UIApplication.sharedApplication().keyWindow!.addSubview(self) 
     self.alpha = 0 
     UIView.animateWithDuration(0.2, delay: 0.2, options: [.CurveEaseInOut], animations: { 
      self.alpha = 1 
      }, completion: nil) 

    } 
    func hideInView() { 
     self.alpha = 1 
     UIView.animateWithDuration(0.2, delay: 0.2, options: [.Repeat, .CurveEaseInOut], animations: { 
      self.alpha = 0 
      self .removeFromSuperview() 
      }, completion: nil) 

    } 

    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) { 
     self .hideInView() 
    } 
    func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWithGestureRecognizer otherGestureRecognizer: UIGestureRecognizer) -> Bool { 

     return true 

    } 
} 

所有的工作後好改變這一後續行

func setup() { 
     // setup the view from .xib 
     view = loadViewFromNib() 
     view.frame = bounds 
     self .userInteractionEnabled = true 
     view.backgroundColor = UIColor.greenColor() 
     view.autoresizingMask = [UIViewAutoresizing.FlexibleWidth, UIViewAutoresizing.FlexibleHeight] 
     addSubview(view) 

}

現在我可以得到tapgesture和touchevents。

0

您必須在自定義視圖課堂中創建插座操作方法,並在該課程中創建觸摸和輕擊手勢。現在爲了接收這些觸摸和點擊的效果,您必須創建一個委託並將委託方法添加到您的視圖控制器,以便接收觸摸和點擊。

+0

爲什麼它不能以編程方式創建? –