Objective C 中的摆动动画

发布于 2024-12-09 14:34:06 字数 1334 浏览 0 评论 0原文

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

-(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 个 UIImageView,需要调用 doWiggle: 例程,我是否需要修复所有这些 UIImageView 的 z 索引?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

好听的两个字的网名 2024-12-16 14:34:06

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

[[touchView superview] BringSubviewToFront:touchView];

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

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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文