Animação Encaixa quando o UIImage é alterado
Eu tenho um UIImageView que atravessa a tela quando um botão é pressionado e mantido. Quando o botão é pressionado altera o UIImage do UIImageView e quando o botão é solto eu mudo para o seu UIImage original. Sempre que a imagem é alterada, ela se encaixa no local em que a imagem foi iniciada.
Este temporizador é chamado quando o botão é pressionado:
//This is the image that changes when the button is pressed.
imView.image = image2;
runTimer = [NSTimer scheduledTimerWithTimeInterval:0.04
target:self
selector:@selector(perform)
userInfo:nil
repeats:YES];
Isso é chamado quando o botão para de ser mantido:
- (IBAction)stopPerform:(id)sender{
[runTimer invalidate];
//THIS IS WHAT SNAPS THE ANIMATION BACK:
//Without this the animation does not snap back
imView.image = image1;
}
- (void)performRight{
CGPoint point0 = imView.layer.position;
CGPoint point1 = { point0.x + 4, point0.y };
CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"position.x"];
anim.fromValue = @(point0.x);
anim.toValue = @(point1.x);
anim.duration = 0.2f;
anim.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
// First we update the model layer's property.
imView.layer.position = point1;
// Now we attach the animation.
[imView.layer addAnimation:anim forKey:@"position.x"];
}
Preciso adicionar a alteração nas imagens à animação? Se sim como? Estou realmente confuso.