简体   繁体   English

如何在屏幕上移动几个按钮

[英]How to move several buttons throughout the screen

I am creating an application where I have several buttons on the screen and I want to make them move throughout the screen. 我正在创建一个应用程序,该应用程序在屏幕上有多个按钮,并且希望它们在整个屏幕上移动。 User can tap on that buttons and the buttons on which user taps gets removed from the screen.Hence I have to stop the timer which moves that button. 用户可以点击该按钮,然后从屏幕上删除用户点击的按钮。因此,我必须停止移动该按钮的计时器。

But my main concern is How to make all the bubbles move ie How to manage each of them? 但是我主要关心的是如何使所有气泡移动,即如何管理每个气泡?

I have made the use of NSTimer. 我已经使用了NSTimer。

NSTimer* timer = [NSTimer timerWithTimeInterval:1 target:self selector:@selector(moveTheButton) userInfo:nil repeats:YES]; [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];

But this can be helpful in case if I have only one button. 但是,如果我只有一个按钮,这可能会有所帮助。 If there are more buttons its quite hard to manage the timers. 如果还有更多按钮,则很难管理计时器。

Also the number of buttons are dynamic. 按钮的数量也是动态的。

Please suggest some alternate. 请提出一些建议。

I have gone through few threads but didnt got the expected answer. 我经历了几个线程,但没有得到预期的答案。

Thanks in advance for help..... 预先感谢您的帮助.....

I would suggest using CAAnimations to move your buttons. 我建议使用CAAnimations移动您的按钮。 This would allow you to animate your buttons smoothly without the use of NSTimers and would also be ideal for creating your button animations dynamically. 这将允许您在不使用NSTimers的情况下平滑地对按钮进行动画处理,并且对于动态创建按钮动画也是理想的选择。

CAAnimations are created then added to a views layer. 将创建CAAnimations,然后将其添加到视图图层。 You can animate the rotation, position and size of a view using CAAnimations. 您可以使用CAAnimations为视图的旋转,位置和大小设置动画。 It is also easy to add and remove animations from a views layer, making it ideal for dynamic content without the headache of multiple NSTimers. 从视图层添加和删除动画也很容易,使其成为动态内容的理想选择,而无需多个NSTimers的麻烦。

How should a UIButton move? UIButton应该如何移动? Do you mean something like randomly moving each button like in a Brownian motion ? 您是说要像布朗运动一样随机移动每个按钮吗? : D :D

Well, it really does not matter I guess, but let's consider that you have a generalized function that determines the behavior of each button given its current position , in this case you can use an NSArray to keep track of all your buttons positions at each time and just call this function over the single NSArray (then cycling through its elements) as the selector of the NSTimer . 好吧,我猜这真的没关系,但是让我们考虑一下,您有一个通用函数来确定每个按钮在当前位置的行为,在这种情况下,您可以使用NSArray每次跟踪所有按钮的位置只需在单个NSArray调用该函数(然后循环通过其元素)作为NSTimer的选择器。

If you do not have a single generalized function, but each button has its own, you could subclass UIButton for each kind of motion you have to take care and implement a method for each class that implements the correct motion. 如果您没有一个通用函数,但是每个按钮都有自己的按钮,则可以为需要注意的每种动作创建UIButton的子类,并为实现正确动作的每个类实现一个方法。 Then you can have a function called in NSTimer that cycles through an array of references of the buttons and calls the implemented motion method for each instance (via polymorphism) 然后,您可以在NSTimer中调用一个函数,该函数循环遍历按钮的引用数组,并为每个实例调用实现的运动方法(通过多态性)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM