On iOS, the user view will not animate?

In the new Single View application, the following code:

- (void)viewDidAppear:(BOOL)animated { self.fooView = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 200, 200)]; self.fooView.backgroundColor = [UIColor yellowColor]; [self.view addSubview:self.fooView]; [UIView animateWithDuration:2 animations:^{ self.fooView.backgroundColor = [UIColor blueColor]; }]; } 

will animate the yellow background to blue. fooView fair:

 @property (strong, nonatomic) UIView *fooView; 

But if a custom view class fooView is fooView , and I changed the above code, replacing UIView with fooView , and also define an empty -drawRect for fooView , then there will be no animation - the background is immediately replaced with blue.

It didn’t matter if using [UIView animateWithDuration: ...] or [FooView animateWithDuration: ...] - this is not an animation.

If inside is -drawRect , I call [super drawRect:rect]; , then it’s the same thing: no animation.

If I change drawRect to drawRect2 , or if I delete the entire method, the animation will work.

(and you might need -drawRect , for example, what if we need to draw a circle in a custom FooView?)

Why? Also, to give a decision on how to make it work even with -drawRect , please explain what happens, which made it inanimate?

+4
source share
4 answers

Having drawRect: changes the way the UIView works. You should only use drawRect: when absolutely necessary, and you should never have an empty implementation of drawRect :.

If the -drawRect: method is present in your subclass, then UIView must do a few things before it can call this method. Namely, it should allocate a potentially expensive backup storage in which your custom drawing will be executed, and it should fill this storage with background color.

Then UIView will call your drawRect implementation: execute any custom drawing in the repository before transferring the repository to the GPU. Only after your animation block starts working.

So, at the beginning of your animation, the backup storage that your custom UIView represents is already painted in a new background color.

If your subclass of UIView does not have a drawRect method: then the view does not have backup storage. The background color is processed directly by the GPU, which can very effectively animate transitions between colors.

Empty UIView objects (without drawRect: implementation) are pretty lightweight. Your custom view should consist of several subzones depending on what kind of animation effects you want to achieve.

For example, the UITableViewCell class consists of many routines. UITableViewCell is a UIView. It contains a background image and a selected background view. It is also a content view, and inside the content view are various text labels and images. All of these views combine to create the selection and resizing animations that you see in a regular UITableView.

+2
source

Using the CATransition Class. You will need to do the animation. Using this method

+ (CAAnimation *) setAnimation {}

+1
source

Use the following line in the animation block

[self.view addSubview: self.fooView];

+1
source

Instead, we should create a new class and use CATransition and use these methods of the "+ (CAAnimation *) setAnimation" class. We must create an object for this and call when the transition occurs.

0
source

Source: https://habr.com/ru/post/1434541/


All Articles