How can I add a Pan Gesture Recognizer to an UITableView, without blocking my scrolling feature of my TableView?
This is my code:
#IBOutlet var ScanPanGestureRecognizer: UIPanGestureRecognizer!
#IBAction func ScanPanGestureRecognizer(sender: UIPanGestureRecognizer)
{
print("TEST")
}
override func viewDidLoad()
{
ScanTableView.addGestureRecognizer(ScanPanGestureRecognizer)
}
So the code works and I get a lot of prints with "Test" but I'm not able to move (scroll) my TableView any more. I have read some other questions / answers but I couldn't find my issue. I thought that the extension "addGestureRecognizer" only add a gesture and not overwrite the TableView pan gesture... Thanks!
I think whatever you want to do with a pan gesture recognizer on a UITableView you can do it in UITableView's delegate method scrollViewDidScroll: in that method the scrollView.contentOffset will tell you how much the tableView has scrolled
Related
I have a UICollectionView with 3 full-screen cells and want to know when a cell is focused full-screen upon swipe. The cellForItemAtIndexPath.isFocused method isn't working for me. Is this the correct use of isFocused method or should I do it another way?
What ever, I can get from your question. I think you want to know on which page it is. So, Here is how i did it:
func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
for cell in colViewSol.visibleCells {
let indexPath = colViewSol.indexPath(for: cell)
// here add the code whatever you want to do
}
}
I'm trying to reproduce the buttom sheet in Apple's iOS 10 Maps app. Most of it is working. I've been looking at this SO post and Pulley on GitHub, but none of them solves my issue.
When the sheets is fully opened, it is possible to scroll the content of the sheet as a UITableView, but when the user tries to scroll down (where the UITableView's contentOffset would be negative), the gesture is dragging in the sheet instead of the UITableView. The gesture seamlessly changes from dragging the UITableView to dragging the sheet.
It is possiple disable the scrolling of the UITableView in the gesture delegate's shouldRecognizeSimultaneouslyWith, but this is only called when a gesture begins.
I can't control the panGestureRecognizer of the UITableView, so I can't just capture the gesture and determine what view it should move.
How can I change what UIGestureRecognizer should recognize touches, in the middle of a gesture?
Try
override func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
if gestureRecognizer == myCustomPanGesture {
return self.tableView.contentOffset == 0
}
return true
}
In my tvOS app, I am trying to listen to changes in scrolling of my UICollectionView. After research, I found that the collection view natively receives a few gesture recognizers among them a UIPanGestureRecognizer with the selector handlePan:
<UIScrollViewPanGestureRecognizer: 0x101a4c1a0; state = Possible; delaysTouchesEnded = NO; view = <UICollectionView 0x1020c5d00>; target= <(action=handlePan:, target=<UICollectionView 0x1020c5d00>)>>
in the log, or in code:
myCollectionView.panGestureRecognizer
I was wondering if there's a way to add my controller as the target of the gesture recognizer, or maybe override the handlePan method.
I tried implementing the UIGestureRecognizerDelegate but it does not give me an access to the handlePan method.
Maybe I should just add a custom UIPanGestureRecognizer of my own on the collection view?
UICollectionView is a subclass of UIScrollView so you can detect scroll changes on collectionview by adding scrollview delegates.
Objective-C
// called on finger up if the user dragged. decelerate is true if it will continue moving afterwards
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
}
// called when scroll view grinds to a halt
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
}
Swift
func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
}
func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
}
I have a custom view inside a UIScrollView
Tapping/touching works perfectly if UIScrollView is not scrolling.
However, when it is scrolling, I need to tap the scrollview first to stop the scrolling, then tap again the scrollview to select my custom view.
Upon searching, I ended up with scrollViewDidEndDragging
func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
if(!decelerate){
print(scrollView.panGestureRecognizer.location(in: scrollView))
//tap scrollview again but now programmatically
}
}
I wanted to tap the scrollview again with the location(CGPoint) where the last tap location is, but couldn't find any way to do it.
But, if you have any idea on touching my custom view inside UIScrollView while scrolling, that would be really great.
Thank you!
I have two views; a UIView placed on top of a UITableView. I need to know when the UIView has been panned, so I’ve placed a UIPanGestureRecognizer on it. However, this creates a UI which seems buggy because you expect the UITableView behind it to move as your finger does.
So it seems I need to somehow pair up this pan gesture with making the table view behind it move, at least, until this covering view disappears.
How do I pair up a pan gesture to move a UIScrollView?
Note: If you’re wondering about the cover view, it’s actually a UIImageView which has a snapshot of the table view with a filter applied to it for UI reasons.
When this view is panned, it disappears. So from the user’s point of view, while they begin dragging the cover view, I want them to keep thinking they are dragging the table view as the cover disappears.
Assuming you have a view controller with a table view setup like the following:
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UIGestureRecognizerDelegate
{
#IBOutlet var tableView: UITableView!
#IBOutlet var scrollView: UIScrollView!
You can add a couple of gesture recognizers:
var gestureRecognizer1: UIPanGestureRecognizer = UIPanGestureRecognizer()
var gestureRecognizer2: UIPanGestureRecognizer = UIPanGestureRecognizer()
Setup your delegates and add the gesture recognizers to the scroll views:
override func viewDidLoad()
{
super.viewDidLoad()
scrollView.contentSize = CGSizeMake( ) // <-- Set your content size.
scrollView.delegate = self
tableView.delegate = self
gestureRecognizer1.delegate = self
gestureRecognizer2.delegate = self
tableView.addGestureRecognizer(gestureRecognizer1)
scrollView.addGestureRecognizer(gestureRecognizer2)
}
Implement a delegate method for UIGestureRecognizerDelegate to recognize simultaneous gestures:
func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWithGestureRecognizer otherGestureRecognizer: UIGestureRecognizer) -> Bool
{
return true
}
Implement a delegate method for UIScrollViewDelegate:
func scrollViewDidScroll(scrollView: UIScrollView)
{
tableView.contentOffset = scrollView.contentOffset
}
}
The movement of the tableView will be synchronized to the scroll view.
In the end I’ve decided to directly change the cell’s images with a CIFilter rather than take a snapshot. This way is the way I should have done it anyway, it’s much cleaner and uses no possibly-buggy “workarounds”.