我正在尝试在我的应用中实现一个Pan Gesture,我需要访问.Ended状态中的初始点。
这是代码。
func handleTestViewTap (panRecognizer: UIPanGestureRecognizer){
var locationOfBeganTap: CGPoint?
if panRecognizer.state == UIGestureRecognizerState.Began {
locationOfBeganTap = panRecognizer.locationInView(view)
print("locationOfBeganTap in BEGAN State -> .\(locationOfBeganTap)")
} else if panRecognizer.state == UIGestureRecognizerState.Ended {
print("locationOfBeganTap -> .\(locationOfBeganTap)")
print("locationOfBeganTap in ENDED State -> .\(locationOfBeganTap)")
}
}
现在,这是输出:
locationOfBeganTap in BEGAN State -> .Optional((195.5, 120.0))
locationOfBeganTap in ENDED State -> .nil
我无法理解locationOfBeganTap
状态nil
为.Ended
的原因
有人可以分享代码来访问locationOfBeganTap
州内的.Ended
...
答案 0 :(得分:1)
你这样做的方式不起作用。相反,最后计算一切:
func handleTestViewTap (panRecognizer: UIPanGestureRecognizer){
if panRecognizer.state == UIGestureRecognizerState.Ended {
print("locationOfBeganTap -> (.\(touch.locationInView(view).x - touch.translationInView(view).x), .\(touch.locationInView(view).y - touch.translationInView(view).y))")
print("location in ENDED State -> .\(touch.locationInView(view))")
}
}
答案 1 :(得分:0)
您需要将locationOfBeganTap
移动为实例变量而不是局部变量。
class WhatEverClassThisIs {
var locationOfBeganTap: CGPoint?
// and the rest of your stuff
func handleTestViewTap (panRecognizer: UIPanGestureRecognizer){
if panRecognizer.state == UIGestureRecognizerState.Began {
locationOfBeganTap = panRecognizer.locationInView(view)
print("locationOfBeganTap in BEGAN State -> .\(locationOfBeganTap)")
} else if panRecognizer.state == UIGestureRecognizerState.Ended {
print("locationOfBeganTap -> .\(locationOfBeganTap)")
print("locationOfBeganTap in ENDED State -> .\(locationOfBeganTap)")
}
}
}