【问题标题】:Is there a way to get a UIImageView location during an animation of that object?有没有办法在该对象的动画期间获取 UIImageView 位置?
【发布时间】:2017-01-07 11:19:33
【问题描述】:

我有一个函数可以动画 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)

【问题讨论】:

    标签: ios swift xcode uiimageview frame


    【解决方案1】:

    https://stackoverflow.com/a/7625335/4503593 转换为 Swift 3 代码:

    presentationLayer - CALayer 的一个属性,“提供一个 与当前层的版本非常接近 正在显示”

    let currentRippleLocation = rippleImage.layer.presentation().frame
    

    【讨论】:

    • 工作就像一个魅力谢谢。我必须做的唯一轻微修改是:rippleImage.layer.presentation()?.frame
    • 你帮了我很多
    【解决方案2】:

    您必须根据要更改的特定坐标/大小调整我的答案,但我认为此解决方案应该适合您:

    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 )
    

    这样您收到的帧应该或多或少是准确的。

    【讨论】:

    • 这是一个非常有趣的方法,我不知道可以选择以这种方式制作动画
    • @MathewS(在我研究之前我也没有):)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-01-29
    • 2021-06-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多