i'm trying implement animation move view position tap. each tap cancel previous animation , start moving again current position.
class moveanimationviewcontroller: uiviewcontroller { lazy var block: uiview = { let block = uiview() block.frame = cgrect(x: 100, y: 100, width: 100, height: 100) block.backgroundcolor = uicolor.greencolor() return block }() override func viewdidload() { super.viewdidload() view.addsubview(block) view.addgesturerecognizer(uitapgesturerecognizer(target: self, action: "tap:")) } func tap(gesture: uitapgesturerecognizer) { let fromposition = (block.layer.presentationlayer() ?? block.layer).position let topostion = gesture.locationinview(view) block.layer.removeallanimations() let animation = cabasicanimation(keypath: "position") animation.duration = 2 animation.fromvalue = nsvalue(cgpoint: fromposition) animation.tovalue = nsvalue(cgpoint: topostion) block.layer.addanimation(animation, forkey: nil) block.layer.position = topostion } }
however, block view jumps destination directly without animation.
replace following code
block.layer.addanimation(animation, forkey: nil)
with
block.layer.addanimation(animation, forkey: "move")
will fix problem, why?
by way, following statement looks illegal, since presentationlayer
method returns anyobject?
, has no position
property. right?
let fromposition = (block.layer.presentationlayer() ?? block.layer).position
it should replaced this, guess. compiler doesn't warn me. bug?
let fromposition = (block.layer.presentationlayer() as? calayer ?? block.layer).position
if name animation, i.e. associate key it, replace other animations same key. if give no name automatic cancel , replace doesn't happen.
Comments
Post a Comment