问题
I have multiple animate-able/interactive UIImageViews on my UIView which would animate/interact based on user touches. I am trying to add a feature which would "wiggle" these animate-able/interactive UIImageViews on touch of an Icon which would help user in identifying which objects are interactive on screen. I am trying to use following code to achieve "wiggle" movement from this blog:
-(void)doWiggle:(UIView *)touchView {
// grabbing the layer of the tocuhed view.
CALayer *touchedLayer = [touchView layer];
// here is an example wiggle
CABasicAnimation *wiggle = [CABasicAnimation animationWithKeyPath:@"transform"];
wiggle.duration = 0.1;
wiggle.repeatCount = 1e100f;
wiggle.autoreverses = YES;
wiggle.toValue = [NSValue valueWithCATransform3D:CATransform3DRotate(touchedLayer.transform,0.1, 0.0 ,1.0 ,1.0)];
// doing the wiggle
[touchedLayer addAnimation:wiggle forKey:@"wiggle"];
// setting a timer to remove the layer
[NSTimer scheduledTimerWithTimeInterval: 2.0 target:self selector:@selector(endWiggle:) userInfo:touchedLayer repeats:NO];
}
-(void)endWiggle:(NSTimer*)wiggleTimer {
// stopping the wiggle now
[((CALayer*)wiggleTimer.userInfo) removeAllAnimations];
}
I call it like this in my routine :[self doWiggle:imageAnimation];
where imageAnimation is an UIImageView. I see that while wiggling, half of the image is disappearing. I read somewhere that I need to set the z-index properly in order for this to work. What do I need to set for z-index? and I have 3 to 4 UIImageViews per page where I need to call doWiggle: routine, do I need to fix z-indexes of all those UIImageViews?
回答1:
Before you call doWiggle, you may need to do this code:
[[touchView superview] bringSubviewToFront:touchView];
If you can see the entire view before doing the animation then this will probably not fix your problem. It is possible that your image is being scaled by the image view and that is causing a problem during the animation. You may need to rescale your image, with an image editing program, to fit inside the view without having the view scale it. You can also do this programmatically, if you don't have a static image (i.e. loading off the network, etc).
来源:https://stackoverflow.com/questions/7742127/wiggle-animation-in-objective-c