Swift 5.4 UIImage动画:如何从弹跳(UIViewAnimate)切换到拖动(UIPanGestureRecognizer)
Swift 5.4 UIImage animation: How to switch from bouncing (UIViewAnimate) to dragging (UIPanGestureRecognizer)
我有一个 UIImageView。
我可以使用 animate 让它弹跳:
self.myImage.frame = CGRect(x: myImage.center.x, y: myImage.center.y, width: 50, height: 70)
UIView.animate(withDuration: 0.4, delay: 0, options: [.repeat, .autoreverse] , animations: {
self.myImage.frame = CGRect(x: self.myImage.center.x, y: self.myImage.center.y, width: 70, height: 80);
}) { (completed) in }
或者,我可以使用 panGestureRecogniser 拖动它:
@IBAction func dragView(_ sender: UIPanGestureRecognizer) {
let translation = sender.translation(in: self.view)
if let viewToDrag = sender.view {
viewToDrag.center = CGPoint(x: viewToDrag.center.x + translation.x, y: viewToDrag.center.y + translation.y)
sender.setTranslation(.zero, in: viewToDrag)
}
如何从先弹跳切换到拖动?
在设置动画时将 .allowUserInteraction
添加到您的选项中
self.myImage.frame = CGRect(x: myImage.center.x, y: myImage.center.y, width: 50, height: 70)
UIView.animate(withDuration: 0.4, delay: 0, options: [.repeat, .autoreverse, .allowUserInteraction] , animations: {
self.myImage.frame = CGRect(x: self.myImage.center.x, y: self.myImage.center.y, width: 70, height: 80);
}) { (completed) in }
编辑:
要打破弹跳动画循环,请在平移手势的开始状态使用 self.myImage.layer.removeAllAnimations()
,如下所示:
@IBAction func dragView(_ sender: UIPanGestureRecognizer) {
let translation = sender.translation(in: self.view)
if let viewToDrag = sender.view {
if sender.state == .began {
viewToDrag.layer.removeAllAnimations()
}
viewToDrag.center = CGPoint(x: viewToDrag.center.x + translation.x, y: viewToDrag.center.y + translation.y)
sender.setTranslation(.zero, in: viewToDrag)
}
我有一个 UIImageView。
我可以使用 animate 让它弹跳:
self.myImage.frame = CGRect(x: myImage.center.x, y: myImage.center.y, width: 50, height: 70)
UIView.animate(withDuration: 0.4, delay: 0, options: [.repeat, .autoreverse] , animations: {
self.myImage.frame = CGRect(x: self.myImage.center.x, y: self.myImage.center.y, width: 70, height: 80);
}) { (completed) in }
或者,我可以使用 panGestureRecogniser 拖动它:
@IBAction func dragView(_ sender: UIPanGestureRecognizer) {
let translation = sender.translation(in: self.view)
if let viewToDrag = sender.view {
viewToDrag.center = CGPoint(x: viewToDrag.center.x + translation.x, y: viewToDrag.center.y + translation.y)
sender.setTranslation(.zero, in: viewToDrag)
}
如何从先弹跳切换到拖动?
在设置动画时将 .allowUserInteraction
添加到您的选项中
self.myImage.frame = CGRect(x: myImage.center.x, y: myImage.center.y, width: 50, height: 70)
UIView.animate(withDuration: 0.4, delay: 0, options: [.repeat, .autoreverse, .allowUserInteraction] , animations: {
self.myImage.frame = CGRect(x: self.myImage.center.x, y: self.myImage.center.y, width: 70, height: 80);
}) { (completed) in }
编辑:
要打破弹跳动画循环,请在平移手势的开始状态使用 self.myImage.layer.removeAllAnimations()
,如下所示:
@IBAction func dragView(_ sender: UIPanGestureRecognizer) {
let translation = sender.translation(in: self.view)
if let viewToDrag = sender.view {
if sender.state == .began {
viewToDrag.layer.removeAllAnimations()
}
viewToDrag.center = CGPoint(x: viewToDrag.center.x + translation.x, y: viewToDrag.center.y + translation.y)
sender.setTranslation(.zero, in: viewToDrag)
}