Is it possible to successful animate a moving 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 设置动画?
基础教程推荐
- 如何比较两个 NSDate:哪个是最近的? 2022-01-01
- Play 商店的设备兼容性问题 2022-01-01
- Xcode UIView.init(frame:) 只能在主线程中使用 2022-01-01
- navigationItem.backBarButtonItem 不工作?为什么上一个菜单仍然显示为按钮? 2022-01-01
- 如何将图像从一项活动发送到另一项活动? 2022-01-01
- Android Volley - 如何动画图像加载? 2022-01-01
- 为什么姜饼模拟器方向卡在应用程序中? 2022-01-01
- UIImage 在开始时不适合 UIScrollView 2022-01-01
- SwiftUI-ScrollViewReader的ScrollTo不滚动 2022-01-01
- iOS - UINavigationController 添加多个正确的项目? 2022-01-01
