Ios 以恒定速度将imageView从当前位置移动到点击位置

Ios 以恒定速度将imageView从当前位置移动到点击位置,ios,swift,xcode,uiimageview,Ios,Swift,Xcode,Uiimageview,我有一个imageView,我想以恒定的速度将prom的当前位置移动到抽头位置,而不考虑距离 尝试了下面的方法,但没有真正起作用,我不知道如何根据imageView需要穿越的距离保持动画持续时间的动态 override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { if let touch = touches.first { let position = touch.loc

我有一个imageView,我想以恒定的速度将prom的当前位置移动到抽头位置,而不考虑距离

尝试了下面的方法,但没有真正起作用,我不知道如何根据imageView需要穿越的距离保持动画持续时间的动态

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    if let touch = touches.first {
        let position = touch.location(in: self.view)
        print(position.x)
        print(position.y)


        CATransaction.begin()
        CATransaction.setCompletionBlock { () -> Void in
            self.imageView.layer.position = (self.imageView.layer.presentation()?.position)!
        }
        var animation = CABasicAnimation(keyPath: "position")
        animation.duration = 2


        var currentPosition : CGPoint = imageView.layer.presentation()!.position
        animation.fromValue = NSValue(currentPosition)
        animation.toValue = NSValue(CGPoint: position.x, (position.y))
        animation.isRemovedOnCompletion = false
        animation.fillMode = kCAFillModeForwards
        imageView.layer.add(animation, forKey: "transform")
        CATransaction.commit()




    }
}
override func touchsbegind(touch:Set,带有事件:UIEvent?){
如果让触摸=先触摸{
让位置=触摸位置(在:self.view中)
打印(位置x)
打印(位置y)
CATransaction.begin()
CATransaction.setCompletionBlock{()->中的Void
self.imageView.layer.position=(self.imageView.layer.presentation()?.position)!
}
var动画=CABasicAnimation(关键路径:“位置”)
动画。持续时间=2
var currentPosition:CGPoint=imageView.layer.presentation()!.position
animation.fromValue=NSValue(当前位置)
animation.toValue=NSValue(CGPoint:position.x,(position.y))
animation.isRemovedOnCompletion=false
animation.fillMode=kCAFillModeForwards
imageView.layer.add(动画,forKey:“变换”)
CATransaction.commit()
}
}
试试这个

计算距离

func distance(_ a: CGPoint, _ b: CGPoint) -> CGFloat {
        let xDist = a.x - b.x
        let yDist = a.y - b.y
        return CGFloat(sqrt((xDist * xDist) + (yDist * yDist)))
    }
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        if let touch = touches.first {
            let position = touch.location(in: self.view)
            print(position.x)
            print(position.y)

            let distance = Double(self.distance(self.imageView.center, position))
            let velocity = 100.0 //pixels by seconds


            let time = distance / velocity

            UIView.animate(withDuration: time, animations: {
                self.imageView.center = position
            })


        }
    }
然后使用任意速度,计算时间

func distance(_ a: CGPoint, _ b: CGPoint) -> CGFloat {
        let xDist = a.x - b.x
        let yDist = a.y - b.y
        return CGFloat(sqrt((xDist * xDist) + (yDist * yDist)))
    }
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        if let touch = touches.first {
            let position = touch.location(in: self.view)
            print(position.x)
            print(position.y)

            let distance = Double(self.distance(self.imageView.center, position))
            let velocity = 100.0 //pixels by seconds


            let time = distance / velocity

            UIView.animate(withDuration: time, animations: {
                self.imageView.center = position
            })


        }
    }
override func touchsbegind(touch:Set,带有事件:UIEvent?){
如果让触摸=先触摸{
让位置=触摸位置(在:self.view中)
打印(位置x)
打印(位置y)
让距离=两倍(self.distance(self.imageView.center,position))
让速度=100.0//s像素
让时间=距离/速度
UIView.animate(持续时间:时间,动画:{
self.imageView.center=位置
})
}
}

您可以添加imageView父视图的UITapGestureRecognitor并创建点击手势动作

@IBAction func tapGesture(_ sender: UITapGestureRecognizer) {
    print("tap working")
    if sender.state == UIGestureRecognizerState.recognized
    {
        let location = sender.location(in: sender.view)
        print(location)
        UIView.animate(withDuration: 0.5, animations: {
            self.imageView.center = location
        })

    }
}
在速度之后有一个额外的“,”