I am doing paging effect in UICollectionView. My solution is shown below.
setContentOffset method will be called in scrollViewWillEndDragging and calculate the next or previous page contentOffset x value and set it with animation.
However, there is one issue which is that once the setContentOffset function has been called, if I touch the screen, then the scrollView will be stopped. Even if you release your finger, it won't continue, which means it stops at a wrong position.
Actually, I've tried to override the targetOffset in UICollectionViewFlowLayout but this issue still exists. Also, I tried to call touchesEnded but this is not even triggered at all. Furthermore, I tried isPagingEnabled and it won't cause this issue but my collectionView items are more complicated, which leads to a wrong targetContentOffset.
My current solution is set scrollView.isUserInteractionEnabled = false after setContentOffset and set it back to true when scrollViewDidEndScrollingAnimation called. This is okay but I am still wondering if there is any good way to do this?
I tried both Google Calendar and Outlook, they will reset you back to the position it should be.
I searched online and I cannot find any questions regarding this issue.
Could you help me? Thanks!
I tried a lot to figure it out and finally, I got something correct to share with you guys.
The solution is to set targetContentOffset in scrollviewWillEndDragging, then I can get what I want.
The truth behind this is that if you call setContentOffset, then scrollviewWillEndDragging won't be called in the second time endDragging (when you touch the screen after the first endDragging). However, if you simply set targetContentOffset = requiredContentOffset, then the second time endDragging will be called and at this time, the paging method will be called again to navigate UIScrollView to a correct position.
What I've learned from this is never call setContentOffset when you do the pagination effect. Some tutorials online for the pagination are totally wrong.
Related
Edit: I am editing my initial question (see below for history) as I am getting new information.
I figured out that when the swipe motion starts from inside the button bounds, we never receive TouchesEnded or TouchesCancelled, only TouchesMoved. However, if I can react on WillEnddragging, it would be great. Is it possible to cancel a gesture on WillEndDragging and also pass this cancel down the children chain?
History:
I am using Xamarin Forms and I have the following issue
I have custom controls part of native scrolling views, like ScrollView or CollectionView, that remain in "clicked" state after the finger enters them but then initiates a scroll gesture.
I had a similar issue on UWP in the past and managed to solve it with the UIElement.PointerCaptureLost event.
Sorry if I am wasting your time on trivial stuff, but I am really stuck and I greatly appreciate your help.
I have tried different approaches suggested, including setting DelaysContentTouches to NO, and playing around with CanCancelContentTouches and overriding TouchesShouldCancelInContentView to always return NO, in a ScrollView custom renderer.
I have had a read of
Allow UIScrollView and its subviews to both respond to a touch
and
UIScrollView sending touches to subviews
Maybe the accepted answer here helps, but I am not sure how to get the tag of my custom view.
What I am expecting is my custom controls to receive the cancelled touch event (or something similar) as happens in both Android and Windows
This was easier than it looked. Solved by adding a UIGestureRecognizerDelegate to my UIGestureRecognizer class and in the delegate I overwrote ShouldRecognizeSimultaneously to return true.
Can I know in advance where the Scrollview will stop before scrolling stop? I have consulted relevant online materials, but it seems that there is no relevant topic. I am making a magic app to know where it will scroll before Scrollview stops, so that I can modify the value there in advance
My attempt: mark when it starts to slow down and stop, but the sliding distance between them is uncertain, so I haven't finished my idea yet
There is a scroll view delegate method.
https://developer.apple.com/documentation/uikit/uiscrollviewdelegate/1619385-scrollviewwillenddragging
This will tell you the intended target offset when the deceleration finishes.
It is intended to be used to change the target offset. For instance if you want to make sure it aligns with the content you have tween it finishes.
But you don’t have to change it. You can just return the target offset but use that to change the content like you mentioned.
I want to get information about how UITableView would be scrolled without actually schange scrollView.contentOffset. I cant use scrollEnabled property becouse its stop fireing scrollViewDidScroll event on table and I need it. Is there any way to do it?
The question is a little bit hard to understand but in any case scrolling on a UITableView will modify contentOffset. That's how the superclass, UIScrollView, works.
As for scrollViewDidScroll, it is only called automatically when the user scrolls the view, but you can override setContentOffset to trigger it manually when needed. Just to avoid a loop set a BOOL dragging = YES flag on scrollViewWillBeginDragging: and clear it on scrollViewDidEndDragging:willDecelerate:.
I'm using CPPickerView in my app to accomplish a horizontal UIPickerView, and it works great, but with large data sources (dozens of items) it scrolls very slowly which makes navigation before (especially considering a normal UIPickerView can go very fast through them).
I don't mean performance-wise, by the way, I mean the view decelerates very quickly, making traversal difficult.
It's just a subclass of UIScrollView with pagingEnabled set to YES. What can I do?
I looked in the source, and it seems CPPickerView is using a scroll view. Scroll views have a decelerationRate property. Play with that and see which value makes for the best result.
Don't fill CPPickerView with all data.
For example fill with first 20 items and if it reaches to the end add another 20.
Creator of CPPickerView here - I've recently updated CPPickerView to add an allowSlowDeceleration property, which should do what you're looking for. Check out the latest code on Github, or Cocoapods version 1.2.0.
For the purposes of documentation, here's how the solution works. Like you mentioned CPPickerView just a scrollview with pagingEnabled set to YES, so the solution I found was to disable paging when the user scrolls with enough velocity.
UIScrollViewDelegate has an optional method scrollViewWillEndDragging:withVelocity:targetContentOffset:, which is called when the user's finger is lifted after swiping/scrolling on the scrollview, and it's still called even when paging is enabled. Based on that value you can tell if the user was trying to scroll quickly through items, or just move one or two items.
I played around with the CPPickerViews in the Demo project, and found that a velocity of about 2.9f seems to be about the normal "fast swipe" threshold. So if the velocity is greater than this threshold (which I defined as kCPPickerDecelerationThreshold in CPPickerView.m) and allowSlowDeceleration is set to YES, CPPickerView now sets pagingEnabled to NO before the deceleration starts. This allows the picker to "coast" and decelerate like a normal scrollview.
It then catches the end of the deceleration, OR the user touching to stop the scroll, by the call to the scrollViewDidEndDecelerating: delegate method. The current item is determined (based on the offset of the scrollview), and then if the scrollview's pagingEnabled property is set to NO a call to the private method scrollToIndex:animated: is made with animation set to YES. This scrolls the CPPickerView to the current item, which necessary as it's unlikely the coasting scroll ended right on a page boundary.
Finally, when the animated scroll completes, the scrollViewDidEndScrollingAnimation: delegate method is called, at which point pagingEnabled is set back to YES.
If you find that you're having trouble getting it to recognize a "fast" swipe, try playing with the kCPPickerDecelerationThreshold value. In hindsight that maybe should be a customizable property, so perhaps I'll roll that into the next update.
As mentioned above you can use the decelerationRate property, setting it to UIScrollViewDecelerationRateNormal might help.
scrollView.decelerationRate = UIScrollViewDecelerationRateNormal;
If that still doesn't solve your problem you could also buffer your inputs into the scroll. See this:
Advanced scrollview techniques
I have a paging scrollview much like Apple's Page Control example project which I have adapted into a horizontal picker. I would really like the ability to scroll through many pages per flick gesture instead of one-at-a-time, much like how UIPickerViews work. Looking for some guidance on how to approach this. Thanks!
First here best Source Code
It could be that whatever is setting those numbers in there, is not greatly impressed by you setting the contentOffset under its hands. So it just goes on setting what it thinks should be the contentOffset for the next instant - without verifying if the contentOffset has changed in the meantime.
I would subclass UIScrollView and put the magic in the setContentOffset method. In my experience all content-offset changing passes through that method, even the content-offset changing induced by the internal scrolling. Just do [super setContentOffset:..] at some point to pass the message on to the real UIScrollView.
Maybe if you put your shifting action in there it will work better. You could at least detect the 3000-off setting of contentOffset, and fix it before passing the message on. If you would also override the contentOffset method, you could try and see if you can make a virtual infinite content size, and reduce that to real proportions "under the hood".
This is also helpful for you..!!!