ios UISwipeGestureRecognizer 滑动长度
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4828833/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
UISwipeGestureRecognizer Swipe length
提问by Tomo
Any idea if there is a way to get the length of a swipe gesture or the touches so that i can calculate the distance?
知道是否有办法获得滑动手势或触摸的长度以便我可以计算距离吗?
回答by Matthias Bauch
It's impossible to get a distance from a swipe gesture, because the SwipeGesture triggers the method where you could access the location exactly one time, when the gesture has ended.
Maybe you want to use a UIPanGestureRecognizer.
不可能从滑动手势获得距离,因为当手势结束时, SwipeGesture 会触发您可以准确访问该位置一次的方法。
也许您想使用 UIPanGestureRecognizer。
If it possible for you to use pan gesture you would save the starting point of the pan, and if the pan has ended calculate the distance.
如果您可以使用平移手势,您将保存平移的起点,如果平移已经结束,则计算距离。
- (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);
}
}
回答by ColossalChris
In Swift
在斯威夫特
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)
}
@objc func panedView(sender:UIPanGestureRecognizer){
var startLocation = CGPoint()
//UIGestureRecognizerState has been renamed to UIGestureRecognizer.State in Swift 4
if (sender.state == UIGestureRecognizer.State.began) {
startLocation = sender.location(in: self.view)
}
else if (sender.state == UIGestureRecognizer.State.ended) {
let stopLocation = sender.location(in: 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
}
}
}
Hope that helps all you Swift pioneers
希望对各位 Swift 开拓者有所帮助
回答by M?h?m?? ???w??h
func swipeAction(gesture: UIPanGestureRecognizer) {
let transition = sqrt(pow(gesture.translation(in: view).x, 2)
+ pow(gesture.translation(in: view).y, 2))
}
回答by IceWarrior353
For those of us using Xamarin:
对于我们这些使用 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);
}
}
回答by Oleg Danu
You can only do it a standard way: remember the touch point of touchBegin and compare the point from touchEnd.
你只能用标准的方式来做:记住 touchBegin 的触摸点并比较 touchEnd 的点。
回答by Carles Estevadeordal
I have an implementation similar to the answer in swift that discriminates between a drag and a swipe calculating the distance relative to the container and the speed of the swipe.
我有一个类似于 swift 中的答案的实现,它区分拖动和滑动,计算相对于容器的距离和滑动的速度。
@objc private func handleSwipe(sender: UIPanGestureRecognizer) {
if (sender.state == .began) {
self.swipeStart.location = sender.location(in: self)
self.swipeStart.time = Date()
}
else if (sender.state == .ended) {
let swipeStopLocation : CGPoint = sender.location(in: self)
let dx : CGFloat = swipeStopLocation.x - swipeStart.location.x
let dy : CGFloat = swipeStopLocation.y - swipeStart.location.y
let distance : CGFloat = sqrt(dx*dx + dy*dy );
let speed : CGFloat = distance / CGFloat(Date().timeIntervalSince(self.swipeStart.time))
let portraitWidth = min(self.frame.size.width, self.frame.size.height)
print("Distance: \(distance), speed: \(speed), dy: \(dy), dx: \(dx), portraitWidth: \(portraitWidth), c1: \(distance > portraitWidth * 0.4), c2: \(abs(dy) < abs(dx) * 0.25), c3: \(speed > portraitWidth * 3.0) ")
if distance > portraitWidth * 0.4 && abs(dy) < abs(dx) * 0.25 && speed > portraitWidth * 3.0 {
if dx > 0 {
delegate?.previousAssetPressed(self)
}else{
delegate?.nextAssetPressed(self)
}
}
}
}