【问题标题】:wiggle animation in objective C目标C中的摆动动画
【发布时间】: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。动画完成后会自动从图层中移除。

标签: objective-c caanimation


【解决方案1】:

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

[[touchView superview]bringSubviewToFront:touchView];

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-08-09
    • 1970-01-01
    • 1970-01-01
    • 2012-09-16
    • 1970-01-01
    • 2023-03-26
    • 1970-01-01
    相关资源
    最近更新 更多