观察动画期间UIView的帧变化 [英] Observing change in frame of a UIView during animation

查看:128
本文介绍了观察动画期间UIView的帧变化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望观察我的UIView原点的x坐标在使用 animateWithDuration:delay:options:animations:completion:进行动画处理时的变化。我希望在此动画期间以粒度级别跟踪x坐标中的更改,因为我希望将交互更改为另一个视图,即动画视图可能与之接触。我想在确切的联系点做出改变。我想了解在更高级别做这样的事情的最佳方法:

I want to observe changes to the x coordinate of my UIView's origin while it is being animated using animateWithDuration:delay:options:animations:completion:. I want to track changes in the x coordinate during this animation at a granular level because I want to make a change in interaction to another view that the view being animated may make contact with. I want to make that change at the exact point of contact. I want to understand the best way to do something like this at a higher level:

- 我应该使用 animateWithDuration:... 在联系点完成回电?换句话说,第一个动画一直运行直到它到达那个x坐标,动画的其余部分发生在完成回调中?

-- Should I use animateWithDuration:... in the completion call back at the point of contact? In other words, The first animation runs until it hits that x coordinate, and the rest of the animation takes place in the completion callback?

- 我应该使用 NSNotification 观察者并观察框架属性的变化?这有多准确/精细?我可以跟踪x的每个变化吗?我应该在一个单独的帖子中这样做吗?

-- Should I use NSNotification observers and observe changes to the frame property? How accurate / granular is this? Can I track every change to x? Should I do this in a separate thread?

欢迎任何其他建议。我正在寻找一个很好的练习。

Any other suggestions would be welcome. I'm looking for a abest practice.

推荐答案

使用 CADisplayLink 自它专门为此目的而建。在文档中,它说:

Use CADisplayLink since it is specifically built for this purpose. In the documentation, it says:


一旦显示链接与运行循环相关联,当屏幕显示时,目标上的选择器被调用内容需要更新。

Once the display link is associated with a run loop, the selector on the target is called when the screen’s contents need to be updated.

对我来说,我有一个填满的酒吧,当它通过某个标记时,我不得不改变视图的颜色超过那个标记。

For me I had a bar that fills up, and as it passed a certain mark, I had to change the colors of the view above that mark.

这就是我所做的:

let displayLink = CADisplayLink(target: self, selector: #selector(animationDidUpdate))
displayLink.frameInterval = 3
displayLink.addToRunLoop(NSRunLoop.mainRunLoop(), forMode: NSDefaultRunLoopMode)

UIView.animateWithDuration(1.2, delay: 0.0, options: [.CurveEaseInOut], animations: { 
    self.viewGaugeGraph.frame.size.width = self.graphWidth
    self.imageViewGraphCoin.center.x = self.graphWidth
    }, completion: { (_) in
        displayLink.invalidate()
})

func animationDidUpdate(displayLink: CADisplayLink) {
    let presentationLayer = self.viewGaugeGraph.layer.presentationLayer() as! CALayer

    let newWidth = presentationLayer.bounds.width

    switch newWidth {
    case 0 ..< width * 0.3: 
        break
    case width * 0.3 ..< width * 0.6: 
        // Color first mark
        break 
    case width * 0.6 ..< width * 0.9:
        // Color second mark
        break
    case width * 0.9 ... width:
        // Color third mark
        break
    default:
        fatalError("Invalid value observed. \(newWidth) cannot be bigger than \(width).")
    }
}

在示例中,我将 frameInterval 属性设置为 3 因为我没有必要严格更新。默认值为 1 ,这意味着它将为每一帧触发,但这会对性能造成影响。

In the example, I set the frameInterval property to 3 since I didn't have to rigorously update. Default is 1 and it means it will fire for every frame, but it will take a toll on performance.

这篇关于观察动画期间UIView的帧变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆