开发者

UIView animation determine center during animation

开发者 https://www.devze.com 2023-03-16 21:00 出处:网络
I\'m using UIView\'s + animateWithDuration:delay:options:animations:completion: method to move my view along a line over the course of a few seconds or so.

I'm using UIView's + animateWithDuration:delay:options:animations:completion: method to move my view along a line over the course of a few seconds or so.

I would like to determine, at an arbitrary time during this animation, where the UIView is along with path it is moving. However when I do this and try and access center or frame, I find that the location 开发者_如何学Pythonis already set to it's final destination.

The reason I'm doing this is that I've got an NSTimer firing every 0.1 seconds (or thereabouts) which will update the parent view to show a line where the UIView has previously been. I want to be able to keep this updated as the UIView moves.

Any ideas on how to do this? The only similar question I found was http://forums.macrumors.com/showthread.php?t=1056105, but there is no resolution shown.

Alternatively...are there any better ways to do this?

Thanks!


UIViews are backed by CALayers, and it is the layer that does the actual animation. CALayer exposes a property presentationLayer which, when accessed, returns a copy of the CALayer that represents, as closely as it can, the current state visible to the user. By asking for the view.layer.presentationLayer.frame you can get the frame of the layer as visible to the user right now, and this corresponds to the frame of the UIView.


I know its too late but maybe it can help somebody else.

CALayer has a presentation() function

func presentation() Returns a copy of the presentation layer object that represents the state of the layer as it currently appears onscreen.

the presentation layer has a frame property what is a CGRect and from here easy the calculate the midpoint.

Demo

UIView animation determine center during animation

The code in swift 3

class ViewController: UIViewController {
        var circle : UIView!
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        let animator = UIViewPropertyAnimator(duration: 10, curve: .easeInOut)
        circle = UIView(frame: CGRect(x: 0.0, y: 0.0, width: 40.0, height: 40.0))
        circle.layer.cornerRadius = 20.0
        circle.backgroundColor = UIColor.blue
        self.view.addSubview(circle)
        Timer.scheduledTimer(withTimeInterval: 0.2, repeats: true)
        {
            [weak self] (myTimer) -> Void in
            if let movingBlueDotFrame = self?.circle.layer.presentation()?.frame
            {
                let blueDotOriginPoint = movingBlueDotFrame.origin
                let blueDotMiddlePoint = CGPoint(x: blueDotOriginPoint.x + movingBlueDotFrame.width/2, y: blueDotOriginPoint.y + movingBlueDotFrame.height/2)
                print(blueDotMiddlePoint)
                if blueDotMiddlePoint == CGPoint(x:100,y:100){
                    print("Yeeaahh")
                    myTimer.invalidate()
                }
            }
       }
        animator.addAnimations {
            self.circle.center = CGPoint(x: 100,y: 100)
        }
        animator.startAnimation()
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号