Move a pixel through screen with swift

2019-08-08 16:51发布

I'm interested in having a VC with objects and others (static, not necessarily dynamic) and apart from that, I want to have a pixel (one or four, depending if it can be seen for the human eye) moving around the borders of the screen (so in the bounds).

I attach a photo of what I need:

Mockup of what I need

So I need that the "pixel" move indefinetely doing a square and I also need to know where it is to do something in my code (in swift)

1条回答
小情绪 Triste *
2楼-- · 2019-08-08 17:17

You can use a solution something to

Swift 2

UIView.animateKeyframesWithDuration(4, delay: 0.0, options: UIViewKeyframeAnimationOptions.Repeat, animations: { () -> Void in

    UIView.addKeyframeWithRelativeStartTime(0, relativeDuration: 1/4, animations: { () -> Void in
        self.aView.center = CGPointMake(0, 100)
    })
    UIView.addKeyframeWithRelativeStartTime(1/4, relativeDuration: 1/4, animations: { () -> Void in
        self.aView.center = CGPointMake(100, 100)
    })
    UIView.addKeyframeWithRelativeStartTime(2/4, relativeDuration: 1/4, animations: { () -> Void in
        self.aView.center = CGPointMake(100, 0)
    })

    UIView.addKeyframeWithRelativeStartTime(3/4, relativeDuration: 1/4, animations: { () -> Void in
        self.aView.center = CGPointMake(0, 0)
    })

}, completion: nil)

Swift 3,4,5

UIView.animateKeyframes(withDuration: 4, delay: 0.0, options: UIView.KeyframeAnimationOptions.repeat, animations: { () -> Void in

    UIView.addKeyframe(withRelativeStartTime: 0, relativeDuration: 1/4, animations: { () -> Void in
        self.aView.center = CGPoint(x: 0, y: 100)
    })
    UIView.addKeyframe(withRelativeStartTime: 1/4, relativeDuration: 1/4, animations: { () -> Void in
        self.aView.center = CGPoint(x: 100, y: 100)
    })
    UIView.addKeyframe(withRelativeStartTime: 2/4, relativeDuration: 1/4, animations: { () -> Void in
        self.aView.center = CGPoint(x: 100, y: 0)
    })

    UIView.addKeyframe(withRelativeStartTime: 3/4, relativeDuration: 1/4, animations: { () -> Void in
        self.aView.center = CGPoint(x: 0, y: 0)
    })

}, completion: nil)

The CGPoint values would the 4 corners of your screen where you want your UIView object to move.

查看更多
登录 后发表回答