Use the implicit animation of UICollectionViewFlowLayout to adjust the layout animation of UICollectionView (and enlarge the specified cell effect), and collectionviewlayout

Source: Internet
Author: User

Use the implicit animation of UICollectionViewFlowLayout to adjust the layout animation of UICollectionView (and enlarge the specified cell effect), and collectionviewlayout

I saw a good result on gitHub a few days ago, that is DaiExpandCollectionViewSo hurry up and look at the source code to see how he implemented it. After reading the source code for a long time, I found that he didn't write any animation code... After being instructed by someone else, he realized that he used the implicit animation of UICollectionViewFlowLayout! The so-called implicit animation is the built-in animation of the system. In fact, it is not a big thing. The key is how I did not expect it to be used in this way! After studying the source code of people for half a day, I basically understood his Implementation ideas and found that his library is inconvenient to use. He needs to inherit his collectionView and cannot customize the cell size, I can't adapt to screens of various sizes, but I really like this library, so I copied it myself, that is, LXMExpandLayout. By the way, the problem found above is solved. The address is here: https://github.com/phelthas/lxmexpandlayout: inherit the system's UICollectionViewFlowLayout and use the existing properties and effects of UICollectionViewFlowLayout, make the appropriate modification to achieve the desired effect, specifically: 1, heavy load-(NSArray *) layoutAttributesForElementsInRect :( CGRect) rect; method, using [super layoutAt TributesForElementsInRect: newRect] gets the layoutAttributes of each element in UICollectionViewFlowLayout. Then modify its attribute 2. The attribute is not changed at will. This should give the original author a thumbs up. The rule he came up with simplifies the modification of this attribute several times. This rule is: Only one unenlarged item can be placed next to the selected item !!! The ox X of this rule is: the right side of the enlarged item can just put the remaining item in the same line with him, the item that needs to be modified is the item in the same row as the item to be zoomed in. For the remaining items, you only need to simply move up or down or do not need to move them at all !!! 3. Now that you know this rule, you have a direction. You can boldly change it. The selected item uses the transform attribute for translation and amplification, other simple modifications to the frame can be used. How can we retrieve other items in the same row as the selected item? There is a good method: self. sameRowItemArray = [super layoutAttributesForElementsInRect: CGRectMake (0, selectedAttributes. frame. origin. y, self. collectionViewWidth, self. itemHeight)]; note that it is a super method, not self ~~ 4. How can I make collectionView execute an invisible animation? This is also learned from the original author, that is, calling the system's-(void) performBatchUpdates :( void (^) (void) updates completion :( void (^) (BOOL finished) completion; method. I encapsulated it into a collectionView category to facilitate use with my LXMExpandLayout. This method will make collectionView re-call its layout to re-layout, and it is animated ~~ 5. If the performBatchUpdates method is called directly, the animation is linear. How can we achieve the spring effect? Here, I learned another trick from the original author: Use a UIView animation block to package a javasmbatchupdates so that the default animation of javasmbatchupdates can be replaced by the iview animation. Use the spring animation method + (void) animated :( NSTimeInterval) duration delay :( NSTimeInterval) delay animated :( CGFloat) dampingRatio initialSpringVelocity :( CGFloat) velocity options :( UIViewAnimationOptions) options animations :( void (^) (void) animations completion :( void (^) (BOOL finished) completion NS_AVAILABLE_IOS (7_0); excellent performance ~~ 6. A system bug of UICollectionView was found. For the-(NSArray *) layoutAttributesForElementsInRect :( CGRect) of UICollectionViewFlowLayout's UICollectionViewFlowLayout, The rect method returns an array containing repeated indexpath objects: http://stackoverflow.com/questions/12927027/uicollectionview-flowlayout-not-wrapping-cells-correctly-ios/13389461#13389461 and other details need to pay attention to, I have written a comment in the code, here is not to say how to use the specific Library, has been written on gitHub, that's enough. If you have any questions, please leave a message for discussion ~~

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.