开发者

How to move several buttons throughout the screen

开发者 https://www.devze.com 2023-02-10 00:27 出处:网络
I am creating an application where I have several buttons on the screen and I want to make them move throughout the sc开发者_StackOverflow中文版reen. User can tap on that buttons and the buttons on wh

I am creating an application where I have several buttons on the screen and I want to make them move throughout the sc开发者_StackOverflow中文版reen. 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 i.e. How to manage each of them?

I have made the use of 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. 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.

CAAnimations are created then added to a views layer. You can animate the rotation, position and size of a view using 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.


How should a UIButton move? Do you mean something like randomly moving each button like in a Brownian motion? : 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.

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

0

精彩评论

暂无评论...
验证码 换一张
取 消