A uicollectionview has a lot of cells, sliding, who do not know which cell will stay, sliding a little faster, will be more slippery a distance, the opposite will be slippery relatively close, this is the Uiscrollview user experience good place. If you want to uicollectionview the location of a cell, you can use-(void) Scrolltoitematindexpath: (Nsindexpath *) Indexpath atscrollposition: ( Uicollectionviewscrollposition) scrollposition animated: (BOOL) animated; this method, You can also use scrollposition this parameter to control the cell specific stay in the upper and lower left and right where. So the question is: if I just slipped, I didn't know it would stop at the Indexpath cell, but whatever cell it was parked on, I wanted the cell to be just in the middle of the screen. (This scene is more common in the Coverflow effect)Previously known practices are:in theIn scrollviewdidenddecelerating or other delegate methods, the nearest integer page and its corresponding contentoffset are computed by the current Contentoffset and then moved to this position by animation. But the problem with this approach is that the animation is incoherent and there is no "just stopping there" feeling. Today, wondering if there was any other better way, suddenly found a way to never work hard:-(void) scrollviewwillenddragging: (Uiscrollview *) ScrollView withvelocity :(cgpoint) Velocity targetcontentoffset: (inout cgpoint *) Targetcontentoffset Ns_available_ios (5_0); Take a look at this parameter name and look at this document, What a delight! This is not to let ScrollView "just stop to a location" Method!!! (System 5.0 has been provided, now only to see ...) Targetcontentoffset is a pointer that modifies the value of this parameter so that ScrollView eventually stops at the target location. Note: This method is called when the Pagingenable property of the ScrollView must be no. Example:-(void) scrollviewwillenddragging: (Uiscrollview *) ScrollView withvelocity: (cgpoint) Velocity Targetcontentoffset: (inout cgpoint *) targetcontentoffset { cgpoint orifinaltargetcontentoffset = Cgpointmake (Targetcontentoffset->x, targetcontentoffset->y); *targetContentOffset = [self ITEMCENTEROFFSETWITHORIGINALTARGETCONTENTOFFSET:ORIFINALTARGETCONTENTOFFSET];//calculates where it wants to stop } So the scrollview will gradually slow down and eventually stop at ItemcenterOffsetwithoriginaltargetcontentoffset method calculated out of position, the effect of ~ originally thought this method not many people know, the results Baidu a search, found that already have the great God has written detailed article (HTTP/ tech.glowing.com/cn/practice-in-uiscrollview/), this should be recorded. Also found a direct use of nsobject to achieve similar effects of the library: Https://github.com/nicklockwood/iCarousel at first glance did not understand ... We'll study the update (2015-06-19) when we have time. Uicollectionviewlayout has provided two ways to achieve this:-(cgpoint) Targetcontentoffsetforproposedcontentoffset: (cgpoint) Proposedcontentoffset withscrollingvelocity: (CGPoint) velocity;-(Cgpoint) Targetcontentoffsetforproposedcontentoffset: (cgpoint) Proposedcontentoffset NS_AVAILABLE_IOS ( 7_0); The effect is exactly the same as the delegate method above, but this is a uicollectionviewlayout method that needs to be overloaded in its own layout subclass. The advantage is: This will not have to write in the Viewcontroller ScrollView delegate method, Viewcontroller more concise; layout-related code is transferred to the class in layout
The delegate method of Uiscrollview let Uicollectionview slide to the position you want