Question

I'm coding a shake image function, and the code is below:

CABasicAnimation* shake = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
shake.fromValue = [NSNumber numberWithFloat:-0.2];
shake.toValue = [NSNumber numberWithFloat:+0.2];
shake.duration = 0.1;
shake.autoreverses = YES; 
shake.repeatCount = 4;
[self.imageView.layer addAnimation:shake forKey:@"imageView"];
self.imageView.alpha = 1.0;
[UIView animateWithDuration:2.0 delay:2.0 options:UIViewAnimationOptionCurveEaseIn animations:nil completion:nil];

I have two questions:

1.the code above will shake the image around its center(transform.rotation.z).But how to make the image shake around the bottom center?

2.How can I control the start and stop of this shaking animation? For example, make the image shaking until get data from server finished then stop the animation.

Was it helpful?

Solution

you could do this by modifying the image. Make it twice as tall but have the bottom just be transparent.

A similar solution that only uses code: Put the ImageView into another view...call it imageHolderView where imageHolderView is twice as tall as the image, but it otherwise just transparent, and then shake that instead of just your image

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top