i just want to animate the position of my UIButton with this function:
CABasicAnimation *moveUp;
moveUp = [CABasicAnimation animationWithKeyPath:@"position.y"];
moveUp.fromValue = [NSNumber numberWithFloat:self.retestBTN.frame.origin.y];
moveUp.toValue = [NSNumber numberWithFloat:self.retestBTN.frame.origin.y - 50];
moveUp.duration = 1.0;
moveUp.removedOnCompletion = NO;
moveUp.fillMode = kCAFillModeBoth;
moveUp.delegate = self;
[[retestBTN layer] addAnimation:moveUp forKey:@"y"];
and then i want to animate it back later that time with that function:
CABasicAnimation *moveDown;
moveDown = [CABasicAnimation animationWithKeyPath:@"position.y"];
moveDown.fromValue = [NSNumber numberWithFloat:self.retestBTN.frame.origin.y];
moveDown.toValue = [NSNumber numberWithFloat:self.retestBTN.frame.origin.y + 50];
moveDown.duration = 1.0;
moveDown.removedOnCompletion = NO;
moveDown.fillMode = kCAFillModeForwards;
[[retestBTN layer] addAnimation:moveDown forKey:@"y"];
But this does not work properly, my UIButton goes to wierd positions…
By using a CAAnimation you are only animating the Layer. And you don’t actually ever change the
modelLayerof the object, only thepresentationLayeras you only create the animation and never actually change thepositionproperty of the layer. This means that the button never actually moves, just where it is drawn on screen moves.If your button is a UIButton then it will still receive touches where the view is, not where the layer is. You can animate any view much more easily using UIKit like so: