I know about this command:
self.tableView.scrollEnabled = true
The question is: I want to lock scrolling according to the scrollView position. For this I do:
let topEdge = scrollView.contentOffset.y
let followersViewEdge = CGRectGetHeight(self.profileView.frame) - 50
if topEdge >= followersViewEdge {
self.tableView.scrollEnabled = true
}
it works, but the problem is that it does not lock or unlock the scrolling immediately. For locking or unlocking the UITableView scrolling I need to release my finger from the screen and scroll again. In this case it works.
I want to make it immediately, so it locks and unlocks the scrolling while I'm swiping on the screen. How can I do it?
UPDATE
My code works. It's not a problem. Problem is that I need to make these changes immediately, without releasing my finger from the screen.
So, do not answer me how to lock scrolling! I know how to do this.
Select tableview
choose Attribute insepector
In scroll view section, uncheck Scrolling Enabled property
Force the contentOffset to your max value while scrolling :
let topEdge = scrollView.contentOffset.y
let followersViewEdge = CGRectGetHeight(self.profileView.frame) - 50
if topEdge >= followersViewEdge {
self.tableView.contentOffset.y = followersViewEdge
}
You need to setting up the pan gesture recognizer for your table view and keep listening listener.
By you approach can lock it.
if topEdge >= followersViewEdge {
self.tableView.scrollEnabled = true
}
By the pan gesture recognizer, you can lock/unlock it.
if(self.tableView.scrollEnabled) {
self.tableView.scrollEnabled = false
} else {
self.tableView.scrollEnabled = true
}
Example
// Added the Pan Recognizer for capture the touches
UIPanGestureRecognizer *panReconizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(panReconizer:)];
panReconizer.maximumNumberOfTouches = panReconizer.minimumNumberOfTouches = 1;
[self.tableView addGestureRecognizer:panReconizer];
- (void)panReconizer:(UIPanGestureRecognizer *)pan {
NSLog(#" .............. pan detected!! ...................");
if(self.tableView.scrollEnabled) {
self.tableView.scrollEnabled = false
} else {
self.tableView.scrollEnabled = true
}
}
Related
iOS7 & iOS8
I need to disable 2 or three fingers scrolling in UIScrollview.
I tried :
[self.scrollView.panGestureRecognizer setMaximumNumberOfTouches:1];
[self.scrollView.panGestureRecognizer setMinimumNumberOfTouches:1];
But it has no effect. It is still possible to scroll with 2 fingers.
If i tried to set max and min to 2. One finger scrolling was disabled but 3 fingers scrolling possible :(
I tried this too, but without success:
for (UIGestureRecognizer* pan in self.scrollView.gestureRecognizers) {
OTTNSLog(#"touches: %ld", (unsigned long)pan.numberOfTouches);
if ([pan isKindOfClass:[UIPanGestureRecognizer class]])
{
UIPanGestureRecognizer *mpanGR = (UIPanGestureRecognizer *) pan;
mpanGR.minimumNumberOfTouches = 1;
mpanGR.maximumNumberOfTouches = 1;
}
if ([pan isKindOfClass:[UISwipeGestureRecognizer class]])
{
UISwipeGestureRecognizer *mswipeGR = (UISwipeGestureRecognizer *) pan;
mswipeGR.numberOfTouchesRequired = 1;
}
}
Does anybody know, how it solve this ?
Thanks.
PROBLEM:
When the UIPanGestureRecognizer is underlying a UIScrollView - which unfortunately does also effect UIPageViewController - the maximumNumberOfTouches is not behaving as expected, the minimumNumberOfTouches however always limits the lower end correctly.
When monitoring these parameters they report back correct values - they seem to do their job - it's just that UIScrollView itself doesn't honor them and ignores their settings!
SOLUTION:
Set the minimumNumberOfTouches to the desired value e.g. 1 and - very importantly - the maximumNumberOfTouches to 2 !!!
myScrollView.panGestureRecognizer.minimumNumberOfTouches = 1;
myScrollView.panGestureRecognizer.maximumNumberOfTouches = 2;
Conform to the UIGestureRecognizerDelegate protocol in your scrollView's #interface declaration. You don't have to set the panGestureRecognizer.delegate for a UIScrollView!!! The delegate is already set because UIScrollView requires to be the delegate of its own pan/pinchGestureRecognizer.
Then implement the UIGestureRecognizer delegate method:
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
NSLog(#"%d", gestureRecognizer.numberOfTouches);
NSLog(#"%#", gestureRecognizer.description);
if (gestureRecognizer == self.panGestureRecognizer) {
if (gestureRecognizer.numberOfTouches > 1) {
return NO;
} else {
return YES;
}
} else {
return YES;
}
}
}
AN EVEN SAFER VERSION:
If you have a custom scrollView class and wanna be on the VERY safe side you can also add one more line of code to disambiguate against other scrollViews:
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
NSLog(#"%d", gestureRecognizer.numberOfTouches);
NSLog(#"%#", gestureRecognizer.description);
if ([gestureRecognizer.view isMemberOfClass:[MY_CustomcrollView class]]) {
if (gestureRecognizer == self.panGestureRecognizer) {
if (gestureRecognizer.numberOfTouches > 1) {
return NO;
} else {
return YES;
}
} else {
return YES;
}
} else {
return YES;
}
}
ADDITIONAL INFORMATION:
The NSLogs tell you the number of touches. If you set both the min and max to the same value (like 1 in the example above) the if-loop would never be triggered... ;-)
That is why maximumNumberOfTouches has to be at least minimumNumberOfTouches + 1
panGestureRecognizer.minimumNumberOfTouches = 1;
panGestureRecognizer.maximumNumberOfTouches = minimumNumberOfTouches + 1;
GEEK SECTION:
for (UIView *view in self.view.subviews) {
if ([view isKindOfClass:[UIScrollView class]]) {
NSLog(#"myPageViewController - SCROLLVIEW GESTURE RECOGNIZERS: %#", view.gestureRecognizers.description);
((UIPanGestureRecognizer *)view.gestureRecognizers[1]).minimumNumberOfTouches = 1;
((UIPanGestureRecognizer *)view.gestureRecognizers[1]).maximumNumberOfTouches = 2;
}
}
This is the way to access the underlying scrollView that is responsible for the paging of a UIPageViewController. Put this code in the e.g. viewDidLoad: of the UIPageViewController (self).
If you don't have access to the scrollView at all - like for a UIPageViewController in a dynamic UITableViewCell where creation and cell reuse happens at runtime and no outlets can be set on its contentViews - put a category on UIScrollView and override the delegate method there. But be careful! This effects every scrollView in your application - so do proper introspection (class-checking) like in my 'EVEN SAFER' example above... ;-)
SIDENOTE:
Unfortunately the same trick doesn't work with the pinchGestureRecognizer on UIScrollView because it doesn't expose a min/maxNumberOfTouches property. When monitored it always reports 2 touches (which you obviously need to pinch) - so its internal min/maxNumberOfTouches seem to have been set both to 2 - even if UIScrollView isn't honoring its own settings and keeps happily pinching with any numbers of fingers (more than 2). So there is no way to restrict pinching to a limited amount of fingers...
I can confirm this is still an issue in iOS 8, but only when the UIPanGestureRecognizer is underlying a UIScrollView. Creating a UIView with a fresh UIPanGestureRecognizer and setting its maximumNumberOfTouches property works as expected.
Interesting note: if you query the UIScrollView's UIPanGestureRecognizer while you're scrolling, it reports the number of touches as less than or equal to the maximum. In other words, if you set the maximum to 2, and scroll with 3 fingers, it reports the gesture as a 2-finger scroll. Subsequently letting up fingers one at a time while continuing to scroll usually (but not consistently) reduces the reported number of touches as well — so if you go from 3 -> 2 -> 1 fingers, it will register 2 -> 1 -> 0 touches, and stop scrolling while you still have 1 finger on the device.
Submitted rdar://20890684 and copied to http://openradar.appspot.com/radar?id=6191825677189120. Please feel free to dupe.
There is no specific method available for this just do some tricks but results are only 75%.
Add swipe gestures(4 directions each) and double tap gesture to your UIScrollview..Then use below code..
[[yourScrollView panGestureRecognizer] requireGestureRecognizerToFail:swipeDown];
[[yourScrollView panGestureRecognizer] requireGestureRecognizerToFail:swipeLeft];
[[yourScrollView panGestureRecognizer] requireGestureRecognizerToFail:swipeRight];
[[yourScrollView panGestureRecognizer] requireGestureRecognizerToFail:swipeUp];
[[yourScrollView panGestureRecognizer] requireGestureRecognizerToFail:doubleTap];
This will do the job for you:
yourscrollView.multipleTouchEnabled=NO;
I am trying to figure out a way to temporarily (i.e. during a single drag gesture) disable scrolling on a UITableView and then re-enable it to have it pick up where it left off.
My reason is I have a gesture recognizer that is monitoring the drag, and if the user drags their finger above the top of the table, I want to resize the table upwards with their finger, to a point, and then stop resizing and continue scrolling again.
Naturally, I don't want the table to scroll while it is resizing, because that's effectively achieving the scroll itself (by moving the entire table view instead of the inner scrollable content), however I can't figure out how to do this in such a way that it allows the gesture to take effect again after a certain point (or if the user drags back down over the table).
Is there a way to temporarily disable/block a gesture without causing it to fail or cancel outright?
Perhaps I could write a subclass of UITableView that can intercept the gestures and ignore them as needed. What method should I override to do this?
Update:
I ended up approaching this in a different way, which is to simply adjust the contentOffset of the table view at each change of the gesture. I was afraid this might look "jittery" but it actually works quite smoothly. However I'll leave the question open as I'm still curious if this can be done.
At the request of #BrunoGalinari, here is the main part of my implementation of handling pan gestures on a UITableView without breaking the table view's intrinsic scrolling.
tableViewExpanded is a local property that switches between two layout states (expanded or not) and adjusts the bottomViewHeightConstraint constant appropriately. Setting it to itself just readjusts the constraint to one of the two valid values since it is also affected during the pan.
- (void)handlePan:(UIPanGestureRecognizer*)sender {
static CGFloat initialBottomViewY;
static CGFloat initialTableViewContentOffsetY;
static CGFloat initialTouchPointY;
CGPoint touchPoint = [sender locationInView:self.view];
CGFloat splitOffset = touchPoint.y - initialBottomViewY;
BOOL inEffect = ( sender == self.tableViewPan && touchPoint.y < initialBottomViewY ) || ( sender == self.mapViewPan && touchPoint.y > initialBottomViewY );
switch ( sender.state ) {
case UIGestureRecognizerStateBegan: {
initialBottomViewY = self.bottomView.y;
initialTableViewContentOffsetY = self.tableView.contentOffset.y;
initialTouchPointY = touchPoint.y;
break;
}
case UIGestureRecognizerStateEnded: {
self.dragVelocity = [sender velocityInView:self.view].y;
if ( inEffect ) {
if ( ABS( splitOffset ) > 60.f ) { // adjust
if ( sender == self.mapViewPan && touchPoint.y > initialBottomViewY )
self.tableViewExpanded = NO;
else if ( sender == self.tableViewPan && touchPoint.y < initialBottomViewY )
self.tableViewExpanded = YES;
else
self.tableViewExpanded = self.tableViewExpanded;
} else
self.tableViewExpanded = self.tableViewExpanded; // spring back
}
break;
}
case UIGestureRecognizerStateChanged: {
if ( inEffect ) {
self.tableView.contentOffset = CGPointMake( self.tableView.contentOffset.x, initialTableViewContentOffsetY + initialTouchPointY - initialBottomViewY );
self.bottomViewHeightConstraint.constant = self.view.height - touchPoint.y;
self.annotationToSelect = nil;
[self adjustMapAnimated:NO];
}
break;
}
default: {
break;
}
}
}
Here's what the window looks like, to get an idea of placement:
You can disable scrolling of a UITableView by setting:
table.scrollEnabled = NO;
When you are don't with you custom gesture, enable it:
table.scrollEnabled = YES;
It is a property of the parent class UIscrollView:
If the value of this property is YES , scrolling is enabled, and if it is NO , scrolling is disabled. The default is YES.
When scrolling is disabled, the scroll view does not accept touch events; it forwards them up the responder chain.
While I know nested scrollViews aren't ideal, our designers provided me with this setup, so I'm doing my best to make it work. Let's begin!
View Hierarchy
UIView
UIScrollView (Vertical Scrolling Only)
UIImageView
UICollectionView #1 (Horizontal Scrolling Only)
UIImageView (different from previous UIImageView)
UICollectionView #2 (Vertical Scrolling Only)
Important Note
All my views are defined using programmatic Auto Layout. Each successive view in the UIScrollView's subview hierarchy has a y-coordinate dependency on the view that came before it.
The Problem
For the sake of simplicity, let's modify the nomenclature a bit:
_outerScrollView will refer to UIScrollView
_innerScrollView will refer to UICollectionView #2
I'd like for my _outerScrollView to route its touch event to the _innerScrollView upon reaching the bottom of its contentSize. I'd like the reverse to happen when I scroll back up.
At present, I have the following code:
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
CGFloat bottomEdge = [scrollView contentOffset].y + CGRectGetHeight(scrollView.frame);
if (bottomEdge >= [_outerScrollView contentSize].height) {
_outerScrollView.scrollEnabled = NO;
_innerScrollView.scrollEnabled = YES;
} else {
_outerScrollView.scrollEnabled = YES;
_innerScrollView.scrollEnabled = NO;
}
}
where the initial conditions (before any scrolling occurs) is set to:
outerScrollView.scrollEnabled = YES;
innerScrollView.scrollEnabled = NO;
What happens?
Upon touching the view, the outerScrollView scrolls until its bottom edge, and then has a rubber band effect due to _outerScrollView.bounces = YES; If I touch the view again, the innerScrollView scroll until it hits its bottom edge. On the way back up, the same rubber banding effect occurs in the reverse order. What I want to happen is have a fluid motion between the two subviews.
Obviously, this is due to the scrollEnabled conditions that are set in the conditional in the code snippet. What I'm trying to figure out is how to route the speed/velocity of one scrollView to the next scrollView upon hitting an edge.
Any assistance in this issue would be greatly appreciated.
Other Notes
This did not work for me: https://github.com/ole/OLEContainerScrollView
I am considering putting everything in the UIScrollView hierarchy (except for UICollectionView #2) inside UICollectionView #2 supplementaryView. Not sure if that would work.
Figured it out!
First:
_scrollView.pagingEnabled = YES;
Second:
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
if (scrollView == _scrollView || scrollView == _offersCollectionView) {
CGFloat offersCollectionViewPosition = _offersCollectionView.contentOffset.y;
CGFloat scrollViewBottomEdge = [scrollView contentOffset].y + CGRectGetHeight(scrollView.frame);
if (scrollViewBottomEdge >= [_scrollView contentSize].height) {
_scrollView.scrollEnabled = NO;
_offersCollectionView.scrollEnabled = YES;
} else if (offersCollectionViewPosition <= 0.0f && [_offersCollectionView isScrollEnabled]) {
[_scrollView scrollRectToVisible:[_scrollView frame] animated:YES];
_scrollView.scrollEnabled = YES;
_offersCollectionView.scrollEnabled = NO;
}
}
}
Where:
_scrollView is the _outerScrollView
_offersCollectionView is the _innerScrollView (which was UICollectionView #2 in my original post).
Here's what happens now:
When I swipe up (so the view moves down), the offersCollectionView takes over the entire view, moving the other subViews out of the view.
If I swipe down (so the views up), the rest of the subviews come back into focus with the scrollView's bounce effect.
Accepted answer didn't work for me. Here's what did:
Define a subclass of UIScrollView:
class CollaborativeScrollView: UIScrollView, UIGestureRecognizerDelegate {
var lastContentOffset: CGPoint = CGPoint(x: 0, y: 0)
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
return otherGestureRecognizer.view is CollaborativeScrollView
}
}
Since it's not possible to reroute touches to another view, the only way to ensure that the outer scrollview can continue scrolling once the inner one stops is if it had been receiving the touches the whole time. However, in order to prevent the outer one from moving while the inner one is, we have to lock it without setting its isScrollEnabled to false, otherwise it'll stop receiving the touches and won't be able to pick up where the inner one left off when we want to scroll past the inner one's top or bottom.
That's done by assigning a UIScrollViewDelegate to the scrollviews, and implementing scrollViewDidScroll(_:) as shown:
class YourViewController: UIViewController, UIScrollViewDelegate {
private var mLockOuterScrollView = false
#IBOutlet var mOuterScrollView: CollaborativeScrollView!
#IBOutlet var mInnerScrollView: CollaborativeScrollView!
enum Direction {
case none, left, right, up, down
}
func viewDidLoad() {
mOuterScrollView.delegate = self
mInnerScrollView.delegate = self
mInnerScrollView.bounces = false
}
func scrollViewDidScroll(_ scrollView: UIScrollView) {
guard scrollView is CollaborativeScrollView else {return}
let csv = scrollView as! CollaborativeScrollView
//determine direction of scrolling
var directionTemp: Direction?
if csv.lastContentOffset.y > csv.contentOffset.y {
directionTemp = .up
} else if csv.lastContentOffset.y < csv.contentOffset.y {
directionTemp = .down
}
guard let direction = directionTemp else {return}
//lock outer scrollview if necessary
if csv === mInnerScrollView {
let isAlreadyAllTheWayDown = (mInnerScrollView.contentOffset.y + mInnerScrollView.frame.size.height) == mInnerScrollView.contentSize.height
let isAlreadyAllTheWayUp = mInnerScrollView.contentOffset.y == 0
if (direction == .down && isAlreadyAllTheWayDown) || (direction == .up && isAlreadyAllTheWayUp) {
mLockOuterScrollView = false
} else {
mLockOuterScrollView = true
}
} else if mLockOuterScrollView {
mOuterScrollView.contentOffset = mOuterScrollView.lastContentOffset
}
csv.lastContentOffset = csv.contentOffset
}
}
And that's it. This will stop your outer scrollview from scrolling when you begin scrolling the inner one, and get it to pick up again when the inner one is scrolled all the way to one of its ends.
I have embedded an image view into a scroll view for zooming purposes. Everything works great but if the user choses to zoom in then pan in any direction the user can slightly pull the ScrollView off the screen bounds exposing the content i have behind the scrollview before bouncing back. I want to disable this bouncing when zoomed in but don't know how. This only happens when zoomed in. At normal zoom scale you can't do it. Heres my code I have gone through the UIScrollView Class Reference and have commented out my attempts at disabling it but it doesn't work!
zoomScrollView.contentSize = CGSizeMake(enlargedPhotoImageView.frame.size.width , enlargedPhotoImageView.frame.size.height);
zoomScrollView.maximumZoomScale = 10;
zoomScrollView.minimumZoomScale = 1;
zoomScrollView.clipsToBounds = YES;
zoomScrollView.delegate = self;
zoomScrollView.zoomScale = 1;
//zoomScrollView.alwaysBounceHorizontal = NO;
//zoomScrollView.alwaysBounceVertical = NO;
//zoomScrollView.bouncesZoom = NO;
-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
if(scrollView == zoomScrollView){
return enlargedPhotoImageView;
}else{
return nil;
}
}
zoomScrollView.bounces = NO;
Will prevent overscroll. But...scrollviews that don't bounce usually feel very weird to users; bear in mind that there's probably a better way to accomplish whatever you're trying to do.
I'm trying to get notified when UIScrollView is pinch zoomed out beyond its minimum zoom limit and is about to animate back, but I'm finding it very difficult. Is there a way I can do this with delegate methods alone or do I need to override UIScrollView's touch handling?
Use scrollViewDidZoom: and check if scrollView.zoomBouncing == YES. Then use zoomScale to determine which direction the view is bouncing.
- (void)scrollViewDidZoom:(UIScrollView *)scrollView {
if (scrollView.zoomBouncing) {
if (scrollView.zoomScale == scrollView.maximumZoomScale) {
NSLog(#"Bouncing back from maximum zoom");
}
else
if (scrollView.zoomScale == scrollView.minimumZoomScale) {
NSLog(#"Bouncing back from minimum zoom");
}
}
}
You can use UIScrollView's scrollViewDidZoom delegate method to detect the moment it's about to animate back. You'll see scrollView.zoomScale drop below scrollView.minimumZoomScale while the view is being pinched. Then, as soon as the user releases their fingers, scrollViewDidZoom will be called once again with scrollView.zoomScale == scrollView.minimumZoomScale, but scrollView.zooming == NO.
Capturing this moment is fine and all, but attempting to do anything to preempt the bounce-back-to-minimumZoomScale animation seems to have really odd side effects for me. :(
In Swift 4.0:
func scrollViewDidZoom(_ scrollView: UIScrollView) {
if scrollView.zoomScale == scrollView.minimumZoomScale
{
print("zoomed out")
}
}
This will be called exactly when the user has finished zooming and the zoomScale is at its minimum possible value, i.e. when the scroll view is zoomed out completely.
I did it with UIPinchGestureRecognizer.
-(void)viewDidLoad{
UIPinchGestureRecognizer *gestureRecognizer =
[[[UIPinchGestureRecognizer alloc] initWithTarget:self
action:#selector(pinched:)]
autorelease];
gestureRecognizer.delegate=self;
[self.scrollView addGestureRecognizer:gestureRecognizer];
//your code
}
-(void)pinched:(UIPinchGestureRecognizer*)gestureRecognizer{
if(gestureRecognizer.state==UIGestureRecognizerStateEnded){
//pinch ended
NSLog(#"scale: %f",scrollView.zoomScale);
}
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer
shouldRecognizeSimultaneouslyWithGestureRecognizer:
(UIGestureRecognizer *)otherGestureRecognizer{
return YES;
}