IOS在动画期间显示标签或图像位置

时间:2015-03-25 20:31:02

标签: ios swift animation

我正在尝试使用

UIView.animateWithDuration()

更改标签或图像的位置。这很简单。但是我还需要在动画期间在屏幕上显示标签的y位置。所以我可以看到动画期间y位置是如何变化的。我该如何实现呢?

2 个答案:

答案 0 :(得分:1)

您可以从presentationLayer获取移动视图的框架。您需要设置NSTimer以获得所需的重复间隔值。

class ViewController: UIViewController {

    let block = UIView(frame: CGRect(x: 20, y: 100, width: 80, height: 80))

    override func viewDidAppear(animated: Bool) {

        super.viewDidAppear(animated)
        block.backgroundColor = UIColor.redColor()
        self.view.addSubview(block)
        let timer = NSTimer.scheduledTimerWithTimeInterval(0.2, target: self, selector: "handleTimer", userInfo: nil, repeats: true)
        UIView.animateWithDuration(5.0, animations: { () -> Void in
             self.block.frame = CGRect(x: 300, y: 400, width: 80, height: 80)
        }) { (finished) -> Void in
            timer.invalidate()
        }
    }

    func handleTimer() {
        println(block.layer.presentationLayer().frame.origin.y)
    }
}

答案 1 :(得分:0)

您可以通过更改它的center.y属性来移动标签。要获得标签的上边框y位置,请使用frame.origin.y。这可能会有所帮助:

 UIView.animateWithDuration(
    0.5,
    animations: {
        label.center.y = <your destination y value> 
        label.text = label.frame.origin.y.description
    },
    completion: nil
    }
)

希望这会有所帮助。