Stop CollectionView from scrolling during interaction - ios

I'm trying to stop a UICollectionView from scrolling if the velocity is too low.
Using the delegate method:
func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
I've tried many different approaches, but nothing seems to stop the scrollview. I've tried combinations of:
scrollView.layer.removeAllAnimations()
scrollView.isScrollEnabled = false
scrollView.layoutIfNeeded()

You can just set the current contentOffset as a target offset.
targetContentOffset.pointee = scrollView.contentOffset

Related

Stop scrolling UITableView perfectly on cell

I have a list of cells of varying height, and I want the scroll to top perfectly at the top of a cell.
For this I can use
func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
And I can set the targetContentOffset to the top of a cells frame.
However if cells yet to be laid out have a height different from their estimated height, then this destination cells origin will change, and I don't get a chance to account for this.
Is there anyway to land perfectly on a cell after a scroll when the origin can change?

Is it possible to disable the swipe gesture and only make the pan gesture work in a scrollview?

I use a UIScrollView to show some images. I find both swipe gesture and pan gesture work. Is it possible to only make pan gesture work at a time?
I tried this code:
for gestureRecognizer in scrollView.gestureRecognizers ?? [] {
if gestureRecognizer is UISwipeGestureRecognizer) {
gestureRecognizer.isEnabled = false
}
}
It doesn't work. I print scrollView.gestureRecognizers!, get the gesture list:
UIScrollViewDelayedTouchesBeganGestureRecognizer
UIScrollViewPanGestureRecognizer
_UIDragAutoScrollGestureRecognizer
UIScrollViewPagingSwipeGestureRecognizer
I tried UIScrollViewPagingSwipeGestureRecognizer:
if gestureRecognizer is UIScrollViewPagingSwipeGestureRecognizer) {
gestureRecognizer.isEnabled = false
}
It says Use of undeclared type 'UIScrollViewPagingSwipeGestureRecognizer'.
If I understand you correctly... maybe you could use the following UIScrollViewDelegate method:
func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
targetContentOffset.pointee = scrollView.contentOffset
}

How to set constraints from page control indicator to the view

Can anybody help me please?:)
How can I set constraints in a storyboard for Page Control Indicator so when I am listing my collectionView pages the indicator isn't moving.
I've tried setting constraints for page control indicator, but there is just option to set it with cell frame. So when I list a collection view indicator appears again and again on each new page.
Screenshot:
You have to set the currentpage in the pageControl. The best is at the function scrollViewWillEndDragging:
override func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
let pageNumber = Int(targetContentOffset.pointee.x / view.frame.width)
pageControl.currentPage = pageNumber
}
then it will work :)

How to measure velocity of Table View's scrolling?

I'd like to call or not call a method depending on how fast user has scrolled.
It has to be instantly measured as user's finger goes because the method needs to be either called or ignored the second it starts moving, not when it has stopped.
This works for me.
CGPoint scrollVelocity = [[self.tableView panGestureRecognizer] velocityInView:self.tableView];
NSLog(#"scroll velocity : %f",scrollVelocity.y);
This method exposes the velocity value. It is part of the scroll view delegate.
optional func scrollViewWillEndDragging(_ scrollView: UIScrollView,
withVelocity velocity: CGPoint,
targetContentOffset: UnsafeMutablePointer<CGPoint>){
}

Slow animation of table view deceleration

Trying to set targetContentOffset in scrollview delegate method to snap tableView to desired position(top,middle,bottom),but after snap action the tableView decelerate to the position but its not finishing animation very fast it gets slow down after few seconds.i tried to change the speed by putting some lower number (0.97,0.96…..) but still its is same,I have also tried putting the same content in UIView animation block but there is no change in speed? How Can I speed up deceleration Rate more than UIScrollViewDecelerationRateFast?
tableView.decelerationRate = UIScrollViewDecelerationRateFast
var scrollRect:CGRect = scrollView.frame;
func scrollViewWillEndDragging(scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
scrollRect.origin.y = self.getNextPosition(position, previousPosition: self.previousPosition, fastScroll: isFastScroll! , andScrollDirection: self.scrollDirection!,isFrameChanged:frameChanged)
targetContentOffset.memory.y = scrollRect.origin.y
}

Resources