1

我的 UIView 上有多个可动画/交互式 UIImageViews,它们会根据用户触摸进行动画/交互。我正在尝试添加一个功能,该功能将在触摸图标时“摆动”这些动画/交互式 UIImageView,这将帮助用户识别哪些对象在屏幕上是交互式的。我正在尝试使用以下代码来实现博客中的“摆动”运动:

-(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 吗?

4

1 回答 1

0

在调用 doWiggle 之前,您可能需要执行以下代码:

[[touchView superview] 带来SubviewToFront:touchView];

如果您在制作动画之前可以看到整个视图,那么这可能无法解决您的问题。图像视图可能正在缩放您的图像,这可能会导致动画期间出现问题。您可能需要使用图像编辑程序重新缩放图像,以适应视图而不需要视图对其进行缩放。如果您没有静态图像(即从网络加载等),您也可以以编程方式执行此操作。

于 2011-10-12T15:16:16.050 回答