任何想法,如果有一种方法来获得滑动手势或触摸的长度,以便我可以计算距离?UISwipeGestureRecognizer滑动长度
30
A
回答
53
由于SwipeGesture会在手势结束时触发一次您可以准确访问位置的方法,因此无法与滑动手势保持一定的距离。
也许你想使用UIPanGestureRecognizer。
如果可以使用平移手势,则可以保存平底锅的起始点,如果平底锅已经结束计算距离。
- (void)panGesture:(UIPanGestureRecognizer *)sender {
if (sender.state == UIGestureRecognizerStateBegan) {
startLocation = [sender locationInView:self.view];
}
else if (sender.state == UIGestureRecognizerStateEnded) {
CGPoint stopLocation = [sender locationInView:self.view];
CGFloat dx = stopLocation.x - startLocation.x;
CGFloat dy = stopLocation.y - startLocation.y;
CGFloat distance = sqrt(dx*dx + dy*dy);
NSLog(@"Distance: %f", distance);
}
}
2
你只能这样做一个标准的方式:记住touchBegin的触点并比较touchEnd的点。
2
对于我们这些使用Xamarin:
void panGesture(UIPanGestureRecognizer gestureRecognizer) {
if (gestureRecognizer.State == UIGestureRecognizerState.Began) {
startLocation = gestureRecognizer.TranslationInView (view)
} else if (gestureRecognizer.State == UIGestureRecognizerState.Ended) {
PointF stopLocation = gestureRecognizer.TranslationInView (view);
float dX = stopLocation.X - startLocation.X;
float dY = stopLocation.Y - startLocation.Y;
float distance = Math.Sqrt(dX * dX + dY * dY);
System.Console.WriteLine("Distance: {0}", distance);
}
}
13
在斯威夫特
override func viewDidLoad() {
super.viewDidLoad()
// add your pan recognizer to your desired view
let panRecognizer = UIPanGestureRecognizer(target: self, action: Selector("panedView:"))
self.view.addGestureRecognizer(panRecognizer)
}
func panedView(sender:UIPanGestureRecognizer){
if (sender.state == UIGestureRecognizerState.Began) {
startLocation = sender.locationInView(self.view);
}
else if (sender.state == UIGestureRecognizerState.Ended) {
let stopLocation = sender.locationInView(self.view);
let dx = stopLocation.x - startLocation.x;
let dy = stopLocation.y - startLocation.y;
let distance = sqrt(dx*dx + dy*dy);
NSLog("Distance: %f", distance);
if distance > 400 {
//do what you want to do
}
}
}
希望帮助你斯威夫特先驱
0
func swipeAction(gesture: UIPanGestureRecognizer) {
let transition = sqrt(pow(gesture.translation(in: view).x, 2)
+ pow(gesture.translation(in: view).y, 2))
}
相关问题
- 1. 塞格动画黑屏/毛刺&延长滑动手势长度
- 2. UISwipeGestureRecognizer动画
- 3. Swift UISwipeGestureRecognizer如何检测对角线滑动?
- 4. 找到最小长度,使滑动窗口全长滑动包含给定集合的所有区间
- 5. 垂直于UIWebView的UISwipeGestureRecognizer滚动:如何避免对角线滑动?
- 6. 如果滑块长度<2
- 7. iPhone:设置所需的滑动手势长度
- 8. Python - 滑动窗口可变长度 - 使用Deque的好处
- 9. 输入长度=值长度&根据值长度自动扩展
- 10. jQuery平滑滑动DIV高度
- 11. 自定义全宽度滑动滑块
- 12. 滑动效果灵敏度
- 13. JQuery Swiper滑动高度
- 14. Silverlight宽度平滑动画
- 15. UITabBarController与UISwipeGestureRecognizer
- 16. UISwipeGestureRecognizer的位置
- 17. UIPanGestureRecognizer重叠UISwipeGestureRecognizer
- 18. 动态滑动宽度的CSS问题
- 19. 自动字符串长度
- 20. printf%f带动态长度
- 21. 动态增长宽度AutoCompleteTextView
- 22. 动态对象的长度
- 23. printf动态长度/大小
- 24. ui5 TreeTable动态长度
- 25. 具有动态长度
- 26. 具有动态长度
- 27. UISwipeGestureRecognizer调用两次
- 28. 的touchesBegan取消UISwipeGestureRecognizer
- 29. UISwipeGestureRecognizer与UIScrollView冲突
- 30. 如何使用UISwipeGestureRecognizer
我想你只能得到方向fr om UISwipeGestureRecognizer。也许你可以获得触摸开始和结束的位置,并从中计算长度。 – picknick 2011-01-28 14:17:30