是否有可能成功地为移动的 UIButton 设置动画?

Is it possible to successful animate a moving UIButton?(是否有可能成功地为移动的 UIButton 设置动画?)

本文介绍了是否有可能成功地为移动的 UIButton 设置动画?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为在屏幕上移动的按钮设置动画.在任何时候,用户都可以按下按钮.但是按钮不响应触摸.我尝试了一个动画块,但按钮只是立即移动到其结束坐标,而框架显示动画(按钮称为气泡):

I'm trying to animate a button which moves around the screen. At any point, the user can push the button. But the button doesn't respond to touches. I've tried an animation block but the button simply moves immediately to its end coordinates, while the frame shows the animation (the button is called bubble):

[UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:5.0];
    CGAffineTransform newTransform = CGAffineTransformMakeScale(3, 3);
    bubble.transform = CGAffineTransformTranslate(newTransform, 0, -460);
    [UIView commitAnimations];

然后我在一些示例代码的帮助下尝试了 Core Animation(路径并不重要,它只是一个示例):

So then I tried Core Animation with the help of some sample code (the path isn't important, it's just an example):

CGMutablePathRef thePath = CGPathCreateMutable();
    CGPathMoveToPoint(thePath,NULL,15.0f,15.f);
    CGPathAddCurveToPoint(thePath,NULL,
                          15.f,250.0f,
                          295.0f,250.0f,
                          295.0f,15.0f);
    CAKeyframeAnimation *theAnimation=[CAKeyframeAnimation animationWithKeyPath:@"position"];
    theAnimation.path=thePath;
    CAAnimationGroup *theGroup = [CAAnimationGroup animation];
    theGroup.animations=[NSArray arrayWithObject:theAnimation];
    theGroup.timingFunction=[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
    theGroup.duration=15.0;
    CFRelease(thePath);
    [bubble.layer addAnimation:theGroup forKey:@"animatePosition"];

但按钮仍然不响应触摸.顺便说一句,我在屏幕上同时有几个这样的气泡"按钮,所以同时激活多个 NSTimer 并不是最佳选择.

But the buttons still don't respond to touches. Btw, I have several of these 'bubble' buttons on screen at once so having several NSTimers concurrently active wouldn't be optimal.

谁能建议另一种方法?我是否应该为 UIImageViews 设置动画并让它们响应触摸?或者我会遇到同样的问题吗?这让我困惑了几天,所以非常感谢任何帮助.

Can anyone suggest another approach? Should I perhaps animate UIImageViews and make them repsond to touches? Or will I have the same problem? This has been puzzling me for a couple of days so any help much appreciated.

谢谢:)

迈克尔

推荐答案

这对于 UIKit 和 CoreAnimation 来说听起来有点太复杂了.看起来你正在开发类似游戏的东西.为什么不使用全局动画计时器让我们说〜60 fps 并在 Quartz2D 中进行绘图?然后,对于每次触摸,您都可以快速检查 Quartz 刷新之间的任何命中.

This sounds a bit too complicated for UIKit and CoreAnimation. It seems like you're developing something like a game. Why not use a global animation timer for let's say ~60 fps and do your drawing in Quartz2D? Then for each touch you could quickly check any hits between Quartz refreshes.

这篇关于是否有可能成功地为移动的 UIButton 设置动画?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:是否有可能成功地为移动的 UIButton 设置动画?

基础教程推荐