Move a pixel through screen with swift

ε祈祈猫儿з 提交于 2020-01-06 03:45:09

问题


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:

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:


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.



来源:https://stackoverflow.com/questions/31010360/move-a-pixel-through-screen-with-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!