如何在 .Ended 状态下访问 Pan Gesture 的初始点
How to access the initial point of a Pan Gesture in it's .Ended state
我正在尝试在我的应用程序中实现一个平移手势,我需要访问处于 .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
处于 .Ended
状态 nil
有人可以分享代码以在 .Ended
状态下访问 locationOfBeganTap
...
您需要将 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)")
}
}
}
你这样做的方式行不通。相反,在最后计算它:
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))")
}
}
我正在尝试在我的应用程序中实现一个平移手势,我需要访问处于 .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
处于 .Ended
状态 nil
有人可以分享代码以在 .Ended
状态下访问 locationOfBeganTap
...
您需要将 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)")
}
}
}
你这样做的方式行不通。相反,在最后计算它:
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))")
}
}