I am relatively new to swift programmming, I found that when the gesture recognizers are global variables they dont work like below.
var monkeyViewPanGesture = UIPanGestureRecognizer(target: self, action: #selector(handlePan))
var bananaViewPanGesture = UIPanGestureRecognizer(target: self, action: #selector(handlePan))
override func viewDidLoad() {
super.viewDidLoad()
MonkeyView.addGestureRecognizer(monkeyViewPanGesture)
BananaView.addGestureRecognizer(bananaViewPanGesture)
view.backgroundColor = .white
view.addSubview(MonkeyView)
view.addSubview(BananaView)
setContriants()
}
But when I move them into the viewDidLoad
function they work
override func viewDidLoad() {
super.viewDidLoad()
let monkeyViewPanGesture = UIPanGestureRecognizer(target: self, action: #selector(handlePan))
let bananaViewPanGesture = UIPanGestureRecognizer(target: self, action: #selector(handlePan))
MonkeyView.addGestureRecognizer(monkeyViewPanGesture)
BananaView.addGestureRecognizer(bananaViewPanGesture)
view.backgroundColor = .white
view.addSubview(MonkeyView)
view.addSubview(BananaView)
setContriants()
}
Can anyone explain why is happens