我有一个功能,可以在屏幕的另一侧动画UIImageView从一组坐标到另一组坐标的移动。在动画期间,我想记录当我点击它时图像的位置。当我这样打电话时,我得到的目的地位置不是当前位置:
var rippleImage = UIImageView()
//example animation block
UIView.animate(withDuration: 6, delay: 0.0, options: [.curveLinear, .allowUserInteraction], animations: {
self.rippleImage.frame = CGRect(x: (xaxis - 500) , y: (yaxis - 500) , width: (self.rippleImage.frame.width + 1000), height: (self.rippleImage.frame.height + 1000))
self.rippleImage.alpha = 0.1
}, completion: nil )
//sample button click function to capture location
@objc func pianoKeyButtonTapped(sender: UIImageView) -> [String] {
// get and animate current ripple
let currentRippleLocation = self.rippleImage.frame
print(currentRippleLocation)
}
在每种情况下,我都会得到相同的目的地位置:
(-342.5, 67.0, 1060.0, 1060.0)
答案 0 :(得分:4)
https://stackoverflow.com/a/7625335/4503593转换为Swift 3代码:
presentationLayer - CALayer的一个属性“提供了一个 近似于当前图层的版本 正在显示“
let currentRippleLocation = rippleImage.layer.presentation().frame
答案 1 :(得分:1)
您必须根据您想要更改的特定坐标/尺寸调整我的答案,但我认为此解决方案适合您:
UIView.animate(withDuration: 0.1, delay: 0.0, options: [.curveLinear, .allowUserInteraction], animations: {
UIView.setAnimationRepeatCount(100) //however many times you want
self.rippleImage.frame.origin.x += 1 //example animation
self.rippleImage.alpha -= 0.9/100 //you can adjust the variables to change incrementally
}, completion: nil )
这样,您收到的框架应该或多或少准确。