我試圖用自定義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")
}
是你的函數_myHandleTap被調用嗎? –
那不叫我已經檢查了斷點。 –