There are two scrollView in the View: mainScrollView(red) and listTable(yellow). I want to set that mainScrollView scrolls first and does the following actions:
When the topView disappears and top of the listTable reaches top of the screen, the listTable scrolls up automatically.
If the top of listTable is at the top of screen, the mainScrollView scroll down first when user scrolls down.
func detectScrollView(_ scrollView: UIScrollView){
if scrollView.tag == 1{
if scrollView.contentOffset.y >= 100{
mainScrollView.isScrollEnabled = false
mainScrollView.contentOffset.y = 100
mainScrollView.panGestureRecognizer.isEnabled = false
listTable.isScrollEnabled = true
listTable.panGestureRecognizer.isEnabled = true
}
}else{
//.....
}
}
By now, I just try the codes of scrolling up action, and it can runs part of my need. Problem is that the listTable cannot scrolls automatically when it's top reaches top of the screen. I have to make it works by releasing my finger from the screen and starting a new drag.
How can I continue the panGesture using on the listTable?
======= Update 2017-7-3 =======
I temporarily use the following codes:
var lastContentOffset:CGFloat = 0
func detectScrollView(){
if (self.lastContentOffset > mainScrollView.contentOffset.y) {
// move up
listTable.isScrollEnabled = true
}
else if (self.lastContentOffset < mainScrollView.contentOffset.y) {
// move down
if mainScrollView.contentOffset.y >= 110{
mainScrollView.setContentOffset(CGPoint(x:0, y:110), animated: true)
listTable.isScrollEnabled = true
}else{
listTable.isScrollEnabled = false
}
}
// update the new position acquired
self.lastContentOffset = mainScrollView.contentOffset.y
}
Moving up is solved, and still moving down needs retouching the screen so as to scroll the listTable.
Related
In my application I have to implement ScrollView and page control, I used ScrollView for both vertical scrolling and horizontal scrolling. when I drag the screen with scroll horizontal means it works fine but when I drag the screen with scroll Vertical means it has some glitches like(scrolling both vertically and horizontally) unable to stop that or unable to find the issue.
So I decided to place two buttons named next and previous for horizontal scrolling for next page and previous page in page control so I want to stop horizontal scroll when dragging the screen, but I don't know how to stop horizontal scrolling(not vertical scrolling).
Here I have posted the code for Scrolling in page control and Next and previous button actions.
I have declared and called the ScrollView Delegate.
UIScrollViewDelegate
override func viewDidLoad() {
super.viewDidLoad()
configurePageControl()
scrollMainHolderView.delegate = self
scrollMainHolderView.isPagingEnabled = true
}
ScrollView Method is:
//MARK:- Scrollview delegate -
func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
let pageNumber = round(scrollView.contentOffset.x / scrollView.frame.size.width)
pageControlNew.currentPage = Int(pageNumber)
self.scrollMainHolderView.contentSize=CGSize(width: self.view.frame.size.width * CGFloat(templateMutArray.count), height: CGFloat((globalYarr[Int(pageNumber)] as? CGFloat)!))
}
Set up code for Page control,
func configurePageControl() {
self.pageControlNew.numberOfPages = templateMutArray.count
self.pageControlNew.currentPage = 0
self.pageControlNew.tintColor = UIColor.red
self.pageControlNew.pageIndicatorTintColor = UIColor.black
self.pageControlNew.currentPageIndicatorTintColor = UIColor.green
}
Code for next and previous button action is,
#IBAction func pagePreviousBtnTapped(_ sender: Any) {
isHorizontalSCrolling = true
scrollMainHolderView.delegate = self
let scrollBounds = self.scrollMainHolderView.bounds
let contentOffset = CGFloat(floor(self.scrollMainHolderView.contentOffset.x - scrollBounds.size.width))
self.movScrollToFrame(contentOffset: contentOffset)
}
#IBAction func pageNextBtnTapped(_ sender: Any) {
isHorizontalSCrolling = true
scrollMainHolderView.delegate = self
let scrollBounds = self.scrollMainHolderView.bounds
let contentOffset = CGFloat(floor(self.scrollMainHolderView.contentOffset.x + scrollBounds.size.width))
self.movScrollToFrame(contentOffset: contentOffset)
}
From what i understand from the comments is that you want to stop horizontal scrolling. That is actually pretty straight forward.
You can stop horizontal scrolling or vertical scrolling in the ScrollViewDelegate Method. Here it is how,
Setting the contentOffset.x value to zero will prevent the scrollview scroll in horizontal direction.
func scrollViewDidScroll(_ scrollView: UIScrollView) {
sender.contentOffset.x = 0.0
}
I created a horizontal tableView, with cells that take up the whole view controller. Instead of scrolling with the default setting, I would like to scroll to the next cell using scrollView.scrollToItem(at: IndexPath method.
Meaning, each time the user scrolls right, it will automatically scroll to the next cell, and each time the user swipes left, it will automatically scroll to the previous cell. Whenever you scroll a direction, it should automatically scroll to the next or previous cell.
I tried intercepting it using scrollViewDidScroll delegate method, but I am running into a ton of problems, it is autoscrolling back and forth and glitching a ton. What am I doing wrong?
var previousOffset: CGFloat = 0.0
var allowHorizontalScroll: Bool = true
func scrollViewDidScroll(_ scrollView: UIScrollView) {
if (allowHorizontalScroll){
let offset = scrollView.contentOffset.x
let diff = previousOffset - offset
previousOffset = offset
var currentBoardIndex = scrollView.indexPathForItem(at: CGPoint(x:offset, y:10))?.item
if currentBoardIndex != nil {
if diff > 0 {
//print("scroll left")
if (currentBoardIndex != 0){
currentBoardIndex = currentBoardIndex! - 1
allowHorizontalScroll = false
scrollView.scrollToItem(at: IndexPath(row: (currentBoardIndex!), section: 0), at: .left, animated: true)
}
}else{
//print("scroll right")
if (currentBoardIndex != ((boardVCDataSource?.fetchedResultsController.fetchedObjects?.count)! - 1)){
currentBoardIndex = currentBoardIndex! + 1
allowHorizontalScroll = false
scrollView.scrollToItem(at: IndexPath(row: (currentBoardIndex!), section: 0), at: .left, animated: true)
}
}
}
}
}
Whenever you scroll a direction, it should automatically scroll to the next or previous cell.
Why not throw out all that code, and instead set your scroll view's isPagingEnabled to true? A paging scroll view does what you describe, automatically.
Is there a practice to scroll a collection view with cells, and, depending on its content offset, scroll the other collection view?
I have a main collection view that has two cells and scrolls horizontally. Then I have a second collection view with cells that scrolls vertically. What happens is when I scroll up, then scroll horizontally to the second vertical cv, it's not synced with relation to the first.
I overrode the scrollViewDidScroll inside the vertically scrolling collection view (not the main one, because it scrolls horizontally), to no avail (** means the parts that don't work to achieve my effect).
var didScrollPastProfileView: Bool = false
func scrollViewDidScroll(scrollView: UIScrollView) {
let collectionViewDictionary = ["scrollView": self.collectionView]
NSNotificationCenter.defaultCenter().postNotificationName("cvDidScroll", object: nil, userInfo: collectionViewDictionary)
if scrollView.contentOffset.y > 250 {
**self.didScrollPastProfileView = true
if self.didScrollPastProfileView {
let previousContentOffset = scrollView.contentOffset.y
scrollView.contentOffset.y = previousContentOffset
}**
} else if scrollView.contentOffset.y < 250 {
**let previousContentOffset = scrollView.contentOffset.y
scrollView.contentOffset.y = previousContentOffset
self.didScrollPastProfileView = false**
}
}
Here's a visual:
Notice how there's space above when I scroll to the second cell? I want that vertical scroll view to follow the first. Any suggestions?
EDIT: I tried to access the second cell of the horizontal collection view while I was scrolling in the first cell, and I tried to change contentOffSet.y when I scrolled down in the first one, but I got an EXC_BAD_ACCESS error.
if scrollView.contentOffset.y <= 250.0 {
let groupsPosts = childViewControllerForPosts.collectionView(childViewControllerForPosts.collectionView!, cellForItemAtIndexPath: NSIndexPath(forItem: 1, inSection: 0)) as! FeedCell
groupsPosts.collectionView.contentOffset.y = -scrollView.contentOffset.y
self.topVerticalConstraint?.constant = -scrollView.contentOffset.y
You can do something like this when scrolled to the collectionViewB with animated to false so it look like it is synchronised.
let indexPath = collectionViewA.indexPathsForVisibleItems().first
self.collectionViewB.scrollToItemAtIndexPath(indexPath, atScrollPosition: UICollectionViewScrollPosition.Top, animated: false)
Not exactly sure how you set up this two collection. If you do not have access to collectionViewA then you might have to save the current indexPath to a variable and pass it over.
Its normal that no one wants to scroll unnecessarily once the scroll has reached its end. I have implemented the following code which works perfect when the scroll is stable at the top/bottom position.
func scrollViewDidScroll(scrollView: UIScrollView)
{
if scrollView.contentOffset.y <= -1
{
scrollView.scrollEnabled = false
self.scrollView.setContentOffset(CGPointMake(0, 0), animated: true)
scrollView.scrollEnabled = true
}
if (scrollView.contentOffset.y >= (scrollView.contentSize.height - scrollView.frame.size.height))
{
scrollView.scrollEnabled = false
scrollView.scrollEnabled = true
}
}
But if user drags rapidly to top or bottom, the scrolling immediately doesn't stop scrolling when it reaches to top/bottom, but rather takes some time to halt. Can we do something that scroll will halt immediately when scrolling reaches the ends and not accelerate further?
I am attempting to use UIPanGestureRecognizer to translate and dismiss a UITableViewController. I want the gesture to trigger translation of the TableView only when it has reached the bottom of its scroll view and dismiss the TableView when it has been translated 1/3 the height of the screen. I've tried to add the GestureRecognizer when the TableView has reached the bottom of its scroll view, but the application ends up adding the gesture recognizer and disabling the freedom to scroll back up in the TableView.
I can supply code upon request, but I should be able to follow general solutions you may have.
Thanks in advance.
Few extra things to note:
I've added the gesture recognizer to the table view
I want to create a similar effect to a particular view in Facebook's iPhone application. In their app, whenever you select a photo from a photo album, it presents a TableView that allows you to translate and dismiss it whenever you reach the any of the edges in its scrollview.
Here's the code I currently have:
override func scrollViewDidScroll(scrollView: UIScrollView) {
// MARK: - Scrollview dismiss from bottom
let scrollViewHeight = scrollView.frame.height
let scrollContentSizeHeight = scrollView.contentSize.height
let scrollOffset = scrollView.contentOffset.y
// Detects if scroll view is at bottom of table view
if scrollOffset + scrollViewHeight >= scrollContentSizeHeight {
println("Reached bottom of table view")
self.panGesture = UIPanGestureRecognizer(target: self, action: "slideViewFromBottom:")
self.imageTableView.addGestureRecognizer(panGesture)
}
}
func slideViewFromBottom(recognizer: UIPanGestureRecognizer) {
let translation = recognizer.translationInView(imageTableView).y
let velocity = recognizer.velocityInView(self.imageTableView)
var centerOfImageTableView = self.imageTableView.center.y
switch recognizer.state {
case .Began:
// Touches begin
println("Touches began")
case .Changed:
// Limits view translation to only pan up
if velocity.y < 0.0 {
centerOfImageTableView = self.screenbounds.height/2 + translation
}
case .Ended:
// Determines length of translation to be animated
let moveToTop = screenbounds.height + translation
// Animates view depending on view location at end of gesture
if translation <= -UIScreen.mainScreen().bounds.height/2 {
UIView.animateWithDuration(0.2) {
self.imageTableView.transform = CGAffineTransformMakeTranslation(0.0, -moveToTop)
return
}
delay(0.3) {
self.presentingViewController?.dismissViewControllerAnimated(false, completion: nil)
}
}
else {
UIView.animateWithDuration(0.3) {
self.imageTableView.transform = CGAffineTransformMakeTranslation(0.0, -translation)
return
}
recognizer.enabled = false
}
default:
println("Default executed")
}
}