Ios Tap手势和触摸事件不工作,这是由自定义uiview和xib创建的?
我试图用自定义的Ios Tap手势和触摸事件不工作,这是由自定义uiview和xib创建的?,ios,view,swift2,uitapgesturerecognizer,Ios,View,Swift2,Uitapgesturerecognizer,我试图用自定义的UIView作为弹出窗口显示一些内容,目前为止我做得很好,我想通过触摸弹出窗口来发布弹出窗口,但我不能成功,我是swift语言的新手,你能帮我吗,所有代码仅从Google复制 自定义视图的代码 import UIKit class PopUpView: UIView { var view = UIView() var _myTap: UITapGestureRecognizer? // this name has to match your class
UIView
作为弹出窗口显示一些内容,目前为止我做得很好,我想通过触摸弹出窗口来发布弹出窗口,但我不能成功,我是swift语言的新手,你能帮我吗,所有代码仅从Google复制
自定义视图的代码
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
}
}
您必须在自定义视图类中创建outlet动作方法,并在此类中创建触摸和点击手势。现在,为了接收这些触摸和点击的效果,您必须创建一个代理,并将代理方法添加到视图控制器中,以便接收触摸和点击。您必须在自定义视图类中创建出口动作方法,并在此类中创建触摸和点击手势。现在,为了接收这些触摸和点击的效果,您必须创建一个委托,并将委托方法添加到视图控制器中,以便接收触摸和点击。最后我得到了答案, 主要问题是没有接收到事件,不管是由xib创建的还是以编程方式创建的 我已将所有代码更改为下面的代码
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
}
}
}
现在我可以得到手势和触摸事件了。最后我得到了答案,
主要问题是没有接收到事件,不管是由xib创建的还是以编程方式创建的
我已将所有代码更改为下面的代码
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
}
}
}
现在我可以获取TapSirture和touchevents。是否调用了您的函数\u myHandleTap?这不是调用我已使用断点检查。是否调用了您的函数\u myHandleTap?这不是调用我已使用断点检查。
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)