gpt4 book ai didi

ios - 调用 touchesBegan 时动画 UIView 对象跳转

转载 作者:可可西里 更新时间:2023-11-01 02:01:55 25 4
gpt4 key购买 nike

我有一个 UILabel 对象,它正在动画化,上下移动。我正在使用 Swift 3 在 Xcode 8.3 中编码。用户可以平移此对象并拖动到一个位置以获得一些点。我正在使用 touchesXXX 手势处理平移/拖动。然而,当我点击并立即放开它时,这个对象出于某种原因垂直跳到它的位置上方,我无法弄清楚为什么现在一个星期......

当我启用一些调试时,我只能看到 touchesBegan 被调用(touchesMoved 没有按预期调用,touchesEnded 也没有出现意外大部头书)。如果我手动禁用对象上的动画,它会按预期工作并且可以毫不费力地平移对象并且显然没有对象跳跃。

这里是相关代码的摘录:

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
let touch = touches.first
let touchLocation = touch!.location(in: self.view)

if self.optOneLbl.layer.presentation()!.hitTest(touchLocation) != nil {
//pauseLayer(self.optOneLbl.layer) <<<<< comment #1
//optOneLbl.translatesAutoresizingMaskIntoConstraints = true <<<<< comment #2
optOneLbl.center = touchLocation
}
}

override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
let touch = touches.first
let touchLocation = touch!.location(in: self.view)
var sender: UILabel

if self.optOneLbl.layer.presentation()!.hitTest(touchLocation) != nil {
self.optOneLbl.center = touchLocation
sender = self.optOneLbl
}

// identify which letter was overlapped
var overlappedView : UILabel
if (sender.frame.contains(letter1.center)) {
...
}
else {
return
}
}

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesEnded(touches, with: event)
}

在阅读了对其他 SO 问题的回复后,我认为按照 touchesBegan 中的上述评论 #1 以编程方式禁用标签动画可能会有所帮助,但问题仍然存在。另外,我认为可能是自动布局导致了这种奇怪的跳跃。因此,我根据评论 #2 启用了 translatesAutoresizingMaskIntoConstraints,但它也没有帮助。

任何人都能看出我在哪里处理不当?感谢阅读!

编辑:

应@agibson007 的要求,我添加了动画代码摘录以供引用:

UIView.animate(withDuration: 12.0, delay: 0.0, options: [ .allowUserInteraction, .curveLinear, .autoreverse, .repeat ], animations: {
self.optOneLbl.center = CGPoint(x: self.optOneLbl.center.x, y: screenSize.midY*1.1)
})

最佳答案

更改标签位置后需要移除/重置动画。动画不知道您更新了值,并试图从一开始就保持在相同的 byValue 范围内。这是更新动画的工作示例。

import UIKit

class ViewController: UIViewController {

var optOneLbl = UILabel()


override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.


optOneLbl = UILabel(frame: CGRect(x: 20, y: 50, width: self.view.bounds.width - 40, height: 40))
optOneLbl.textAlignment = .center
optOneLbl.text = "I Will Be Moving Up and Down"
optOneLbl.textColor = .white
optOneLbl.backgroundColor = .blue
self.view.addSubview(optOneLbl)

//starts it in the beginnning with a y
fireAnimation(toY: self.view.bounds.midY*1.1)


}

func fireAnimation(toY:CGFloat) {
UIView.animate(withDuration: 12.0, delay: 0.0, options: [ .allowUserInteraction, .curveLinear, .autoreverse, .repeat ], animations: {
self.optOneLbl.center = CGPoint(x: self.optOneLbl.center.x, y:toY)
})
}



override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
let touch = touches.first
let touchLocation = touch!.location(in: self.view)

if self.optOneLbl.layer.presentation()!.hitTest(touchLocation) != nil {
//re
optOneLbl.layer.removeAllAnimations()
optOneLbl.center = touchLocation
}
}

override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
let touch = touches.first
let touchLocation = touch!.location(in: self.view)
var sender: UILabel

if self.optOneLbl.layer.presentation()!.hitTest(touchLocation) != nil {
self.optOneLbl.center = touchLocation
sender = self.optOneLbl
}
}

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesEnded(touches, with: event)
//restart animation after finished and change the Y if you want.
// you could change duration or whatever
fireAnimation(toY: self.view.bounds.height - optOneLbl.bounds.height)
}

}

关于ios - 调用 touchesBegan 时动画 UIView 对象跳转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45666869/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com