Prevent unwanted animation in subzones with animation restrictions

Usually you get unwanted animations in UITableViewCells when animating the height of a UITableView. New cells that are reused come and have strange unwanted animations on the subtitles that are laid out. This is usually seen when the table expands.

Animations usually look as if objects appear from around the corner, because for some reason they enliven the width and height of the supervision from 0 to the size that they should be.

This can be solved by overriding the cellSubviews method.

- (void)layoutSubviews { [UIView performWithoutAnimation:^{ [super layoutSubviews]; }]; } 

But this still does not prevent the areas it contains from making unnecessary animations. It also does not prevent unwanted animations when you add things to the cell’s contentView. You basically have to override the layoutSubviews methods for each individual subheading, if you also have subviews.

This is not always possible, as is the case with contentView. I can not override the contentView class and tell it to lay out its subtitles without animation. This is also a very cumbersome fix since I need to add this to every UIView object that I can override.

I still have not found an answer that corrects this once and for all. How to simply animate a restriction on something, but prevent weird unwanted animations in their subtitles.

+5
source share
3 answers

I ran into a similar problem in UICollectionView and was able to fix it in my deletion:

 - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { // setup your cell // ... [UIView performWithoutAnimation:^{ [cell layoutIfNeeded]; }]; return cell; } 
+11
source

The other answers are correct, but you must do this in the entire hierarchy of views. Add this extension method and call it in ** subview **, which does not need animation.

 public func commitSuperviewsLayout() { var processedSuperview = superview while processedSuperview != nil { processedSuperview?.layoutIfNeeded() processedSuperview = processedSuperview?.superview } } 
0
source

In general, what I do, if I only want to revitalize certain things, and not others, make sure that all restrictions that should not be animated are set, and after this call

 [view layoutIfNeeded] 

on the parent view of views affected by these restrictions.

Finally, I set constants on the constraints that I want to animate, and call

 [UIView animateWithDuration:0.2 animations:^{ [view layoutIfNeeded];}]; 
-1
source

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


All Articles