I make a test application for iOS on swift .
I have a task in front of me:

In the application, the start button is pressed - geometric figures are falling from above. When the application is minimized - when the screen is filled with figures - a push should arrive that the area is filled with figures.

I understand that you first need to deal with the animation of objects, while you need to prescribe a rule that they will not fall into each other, but will go on top. Next, you need to write a rule according to which the event will occur when the figure reaches a certain top point + to deal with the guns.
Could you tell me where to start? Which way to dig at all?
Is it possible to do this using the UIView method animateWithDuration:animations: :?

Closed due to the fact that it is necessary to reformulate the question so that it was possible to give an objectively correct answer by the participants aleksandr barakin , Yuri Negometyanov , VenZell , D-side , tutankhamun 24 Mar '16 at 19:15 .

The question gives rise to endless debates and discussions based not on knowledge, but on opinions. To get an answer, rephrase your question so that it can be given an unambiguously correct answer, or delete the question altogether. If the question can be reformulated according to the rules set out in the certificate , edit it .

  • give more information. is it a game, or just an animation like a figure appeared from above crawled to the bottom and remained there forever? - Max Mikheyenko
  • In principle, this is just an animation. Figures with a specified period of time appear on top and fall down into a pile. As this same pile grows to the top of the screen comes push. - Kuznetsov Maxim
  • more, what is a bunch? the figure appears in the middle and flies down, where it becomes the previous figure, or if a fall can move to the side in order to get a slide? - Max Mikheyenko
  • well and at once: it will be difficult to push - when the application goes to the background, its execution stops. There are options for how to make it work, but I think it is not right when the animation is eating system resources, although the user does not see it. - Max Mikheyenko pm
  • After a certain period of time, the figures fall down from random upper positions with the same y coordinate and different x coordinates. They fall down, hit the figures that lie below, bounce and lie side by side / on top. And so, until the figures fill the screen space, after which the push comes. - Kuznetsov Maxim

2 answers 2

Similar things are done using the UIDynamic technology.

  1. UICollisionBehavior - provides collision detection
  2. UIGravityBehavior - as the name implies, it provides an imitation of anguish

Initialize UIGravityBehavior and add your views to it, which should fall from above. Run and everything falls. Next, add collision detection. That's all. Randomly select a position from above for a new view and go!

 @interface ViewController () @property (strong, nonatomic) UIDynamicAnimator *animator; @property (strong, nonatomic) UIGravityBehavior *gravity; @property (strong, nonatomic) UICollisionBehavior *collision; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 50)]; view1.backgroundColor = [UIColor orangeColor]; [self.view addSubview:view1]; UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(0, 480, 50, 50)]; view2.backgroundColor = [UIColor greenColor]; [self.view addSubview:view2]; self.animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view]; self.gravity = [[UIGravityBehavior alloc] initWithItems:@[view1, view2]]; [self.animator addBehavior:self.gravity]; self.collision = [[UICollisionBehavior alloc] initWithItems:@[view1, view2]]; self.collision.translatesReferenceBoundsIntoBoundary = YES; [self.animator addBehavior:self.collision]; } 

When run, you will see the following: enter image description here

  • I eventually did it, thanks! I just did not figure out how to stop the cycle of falling figures to achieve a certain upper coordinate by the figures. You do not know what you can pay attention to? - Kuznetsov Maxim
  • one
    UIDynamicAnimatorDelegate has two methods, dynamicAnimatorWillResume and dynamicAnimatorDidPause. They can check the coordinates - Alexander Yolkin

Well, try:

1) the fall of the figures. You can make it through animateWithDuraiton , but I would suggest looking in the direction of some more serious solution like SpriteKit or cocos2d. It makes sense to also fasten the physical engine of the box2d type - it will save you time for all the calculations that it has gone down.

2) Push. There are two options: either require the system to continue to work on the background with beginBackgroundTaskWithExpirationHandler: (here we must remember that the system can kill your application at any time if it needs resources), or register for running on the background using capabilities - > background modes. there you put, for example, that you play music on the background and calmly perform as much as you need (in this case, you will actually have to play some sound, for example, once a minute, and then the epl will not let you in the app).

The second option is to calculate all the falling figures in advance, determine how long it will take and set a local notification after this time. when the user returns to the application, show him the final state of the entire system.