问题描述
我正在尝试为在屏幕上移动的按钮设置动画.在任何时候,用户都可以按下按钮.但是按钮不响应触摸.我尝试了一个动画块,但按钮只是立即移动到其结束坐标,而框架显示动画(按钮称为气泡):
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 设置动画?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!


大气响应式网络建站服务公司织梦模板
高端大气html5设计公司网站源码
织梦dede网页模板下载素材销售下载站平台(带会员中心带筛选)
财税代理公司注册代理记账网站织梦模板(带手机端)
成人高考自考在职研究生教育机构网站源码(带手机端)
高端HTML5响应式企业集团通用类网站织梦模板(自适应手机端)