我正在使用Xcode 10.0 beta 4和Swift 4,并且我尝试为左右滑动向UIView添加UIGestureRecognizers(特别是UISwipeGestureRecognizer),该UIView已通过子视图添加为主视图控制器的子视图。笔尖,但不幸的是,当我实际在模拟器中执行操作时,似乎无法识别该手势。这是我的viewDidLoad()中的代码片段:
//Add gesture recognizer for when the calendar is swiped right.
let grSwipeRight = UISwipeGestureRecognizer(target: self, action: #selector(swipeRight(sender:)))
grSwipeRight.direction = UISwipeGestureRecognizer.Direction.right
calendar.calendarDays.isUserInteractionEnabled = true
calendar.calendarDays.addGestureRecognizer(grSwipeRight)
选择器中的swipeRight操作是对一个仅包含print()调用的函数,日历是对我的UIView的引用,这是我的.xib中使用的类
class Calendar: UIView
{
@IBOutlet var calendarDays: UICollectionView!
let nibName = "Calendar"
var contentView: UIView?
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
guard let view = self.loadViewFromNib() else { return }
view.frame = self.bounds
self.addSubview(view)
self.bringSubviewToFront(view)
contentView = view
contentView?.isUserInteractionEnabled = true
}
private func loadViewFromNib() -> UIView? {
let bundle = Bundle(for: type(of: self))
let nib = UINib(nibName: nibName, bundle: bundle)
return nib.instantiate(withOwner: self, options: nil).first as? UIView
}
我使用Interface Builder将.xib设置为UIView,并将视图的类设置为Calendar类,并且在将任何Gesture Recognizer添加到任一日历时,.xib中的标签等都可以正确显示。视图)或我日历中的对象,手势无法识别。这是做这样事情的正确方法还是我应该采取一种完全不同的方法?
编辑: 我已经提供了IB .xib和.storyboard的外观(如果有帮助的话)。 Storyboard,xib
答案 0 :(得分:1)
为了能够在我的.xib中使用手势识别器,我在自定义UIView Calendar类中使用了awakeFromNib()的重写:
/account/login (POST)
/account/login (GET)
/account/register(POST)
/account/logout (GET)