ios - Animate a UIView on a sine wave path -
how make uiview move along sine wave path? ideally, i'd able stop animation when uiview goes off screen , release it. see uiview->animatewithduration can animate on 1 property @ time, seems 2 exclusive things happening simultaneously: it's moving right , it's moving up/down.
you can simple cakeyframeanimation
.
override func viewdidload() { super.viewdidload() let myview = uiview(frame: cgrect(x: 10, y: 100, width: 50, height: 50)) myview.backgroundcolor = uicolor.cyan view.addsubview(myview) let animation = cakeyframeanimation() animation.keypath = "position" animation.duration = 60 // 60 seconds animation.isadditive = true // make animation position values later generate relative values. // x = 0 x = 299, generate y values using sine. animation.values = (0..<300).map({ (x: int) -> nsvalue in let xpos = cgfloat(x) let ypos = sin(xpos) let point = cgpoint(x: xpos, y: ypos * 10) // 10 give amplitude. return nsvalue(cgpoint: point) }) myview.layer.add(animation, forkey: "basic") }
Comments
Post a Comment