UIScrollView's delegate method makes it easy to slide UICollectionView to a position you want, uiscrollviewdelegate

Source: Internet
Author: User

UIScrollView's delegate method makes it easy to slide UICollectionView to a position you want, uiscrollviewdelegate

A UICollectionView has many cells. If you slide a bit, no one knows which cell will stay. If you slide a little faster, you will slide a longer distance, and vice versa, you will slide closer, this is exactly where UIScrollview has a good user experience. If you want UICollectionView to stay at a cell location, you can use-(void) scrollToItemAtIndexPath :( NSIndexPath *) indexPath atScrollPosition :( UICollectionViewScrollPosition) scrollPosition animated :( BOOL) animated; this method, you can also use the scrollPosition parameter to control the position of the cell in the upper, lower, and left sides. The problem arises: If I just slide for a moment, I don't know which indexPath it will be parked on, But no matter which cell it is located, I want this cell to be in the middle of the screen. What should I do? (This scenario is more common in coverFlow) We know that in scrollViewDidEndDecelerating or other delegate methods, we use the current contentOffset to calculate the nearest integer page and its corresponding contentOffset, then, move the animation to this position. However, there is a problem with this practice, that is, the animation is not coherent and there is no feeling of "just stopping there. Today, when I was wondering if there were any better solutions, I suddenly found a method that never worked before:-(void) scrollViewWillEndDragging :( UIScrollView *) scrollView withVelocity :( CGPoint) velocity targetContentOffset :( inout CGPoint *) targetContentOffset NS_AVAILABLE_IOS (5_0); let's take a look at this parameter name! This is not the way to make scrollView "Just stop to a certain position !!! (System 5.0 is provided, and now we can see ......) TargetContentOffset is a pointer. You can modify the value of this parameter to stop the scrollView in the target position. Note: This method is called only when the pagingEnable attribute of scrollView is NO. Example:-(void) custom :( UIScrollView *) scrollView withVelocity :( CGPoint) velocity targetContentOffset :( inout CGPoint *) targetContentOffset {CGPoint custom = CGPointMake (targetContentOffset-> x, targetContentOffset-> y); * targetContentOffset = [self defined: orifinalTargetContentOffset]; // calculate the position where you want to stop it.} Then, the scrollView will gradually slow down and eventually stop at ite. The position calculated by the mCenterOffsetWithOriginalTargetContentOffset method is good ~ I thought this method does not know how many people, the results of a Baidu search, found that the original has been a great god to write a detailed article (http://tech.glowing.com/cn/practice-in-uiscrollview/), this should be recorded it Also found a direct use of NSObject to achieve similar results Library: https://github.com/nicklockwood/iCarousel at first glance did not understand... Wait for the time to carefully study the update () the original UICollectionViewLayout has provided two methods to achieve this function:-(CGPoint) implements :( CGPoint) proposedContentOffset withScrollingVelocity :( CGPoint) velocity) except :( CGPoint) proposedContentOffset NS_AVAILABLE_IOS (7_0); the effect is exactly the same as that of the preceding delegate method, but this is the method of UICollectionViewLayout and needs to be reloaded in its own layout subclass. Advantage: in this way, you no longer need to write the scrollView delegate method in viewController. viewController is more concise; layout-related code is transferred to the layout class.

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.