【发布时间】:2011-10-12 14:59:33
【问题描述】:
我的 UIView 上有多个可动画/可交互的 UIImageView,它们会根据用户触摸进行动画/交互。我正在尝试添加一个功能,该功能将在触摸图标时“摆动”这些动画/交互式 UIImageView,这将帮助用户识别哪些对象在屏幕上是交互式的。我正在尝试使用以下代码来实现来自this 博客的“摆动”运动:
-(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];
}
我在例程中这样称呼它:[self doWiggle:imageAnimation]; 其中 imageAnimation 是一个 UIImageView。我看到在摆动时,一半的图像正在消失。我在某处读到需要正确设置 z-index 才能使其正常工作。我需要为 z-index 设置什么?而且我每页有 3 到 4 个 UIImageViews 我需要调用 doWiggle: 例程,我需要修复所有这些 UIImageViews 的 z-indexes 吗?
【问题讨论】:
-
无需为删除安排计时器,只需将您的重复计数设置为 2.0 / 0.1。动画完成后会自动从图层中移除。