Sliding UIView in a UICollectionViewCell is blocking the ability to scroll - objective-c

I am using a UIPanGestureRecognizer on a UIView that is in a UICollectionViewCell. I am using the translationInView method to get the translation along the X axis and slide the UIView left and right. It works fine, but now I cannot scroll up and down in the CollectionView if my finger is on a cell. Is there a way to make the PanGestureRecognizer pass the vertical scrolling to the UICollectionView?
I am trying to replicate the sliding seen in Reeder.app for iOS.

Once the UIPanGestureRecognizer activates in your UIView touches are no longer forwarded through the responder chain, because of that your UICollectionView is not receiving the touches anymore.
You can try setting your UIView as the delegate of your UIPanGestureRecognizer and place the logic of whether the UIPanGestureRecognizer should activate. You have to implement the delegate's method in your UIView:
-(BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer;
For example with the code below you would tell it not to activate if the velocity on the y axis of the view is greater than the velocity of the x axis (therefore sending the touches to the UICollectionView under it)
-(BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{
UIPanGestureRecognizer *recognizer = (UIPanGestureRecognizer *)gestureRecognizer;
CGPoint velocity =[recognizer velocityInView:self];
if(abs(velocity.y)>=(abs(velocity.x))){
return NO;
}else return YES;
}
Hope this is helpful.

Related

ScrollView is eating Swipe Gesture Recognizer

i have a details view controller with a scrollview on it. And i have load UILabel, UIImageView on top of UIScrollView. The scrollview is set to scroll vertically only. and the view need to be able to recognize swipe left and right to navigate to next/previous page by adding
self.leftGestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(swipeRecognizer:)];
[self.leftGestureRecognizer setDirection:UISwipeGestureRecognizerDirectionLeft];
[self.view addGestureRecognizer:self.leftGestureRecognizer];
So when i swipe within UILabel, it is working. If i swipe start from UIScrollView, it is not working. I guess it is the UIScrollView conflict the swipe gesture.
In short, swipe gesture is only working on the subview but not UIScrollView .Does anyone have any idea on this?
UPDATE:
If i swipe start from scrollview first then end at UILabel, it won't recognize the swipe gesture. If i swipe within UILabel(start and end in the UILAbel) it is able to recognize.
Make sure that if the UIImage allows zooming, the swipe recognizer won't work, since it is trying to zoom. If this is the case, you will need to enable the zoom only in certain case. hope it helps.
Initialize the scroll view with scrolling disabled. Then you need to disable scrolling in the scroll view when the image is not zoomed in, and renabled it when it is zoomed in. This provides the expected behavior.
- (void)scrollViewDidZoom:(UIScrollView *)scrollView {
if (scrollView.zoomScale != 1.0) {
scrollView.scrollEnabled = YES;
} else {
scrollView.scrollEnabled = NO;
}
}
To enable zoom provide an image on a delegate.
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
return myImage;
}
Also add the gesture to instance of scrollview like this -
leftGestureRecognizer.delaysTouchesBegan = YES;
[myScrollView addGestureRecognizer:leftGestureRecognizer];
OR You can try the following -
[scrollView.panGestureRecognizer requireGestureRecognizerToFail: leftGestureRecognizer]

NSScrollView doesn't call drawRect for documentView

I have NSScrollView with drawable content.
It works fine with scrolling until I press Cmd+Tab or hide window in any other way.
When I open window with scrollView second time it doesn't redraw content on scrolling.
so drawRect function for documentView doesn't work anymore.
setting content view (I need to redraw it on scrolling because self.documentView is much wider than window's width)
[self.documentScroll setDocumentView:self.documentView];
in NSClipView
-(void) drawRect:(NSRect)dirtyRect {
[self setCopiesOnScroll: NO];
[super drawRect:dirtyRect];
NSArray* ar = self.subviews;
NSView* docView = ar.firstObject;
BOOL b = docView.needsDisplay;
}
docView.needsDisplay is always NO after window was hidden and shown second time. On application's launch it's always YES and drawRect method calls for documentView on every scrolling event

Touch on UISlider prevents scrolling of UIScrollView

I have a (vertical) UISlider inside a UIScrollview. I'd like to be able to change the value of the slider, and, without lifting my finger, scroll the scrollview left or right.
Desired behavior:
Touch down inside vertical UISlider, followed by a finger drag left or right causes the scrollview to scroll
Actual behavior:
Touch down inside vertical UISlider, followed by a finger drag left or right causes no movement in UIScrollview. A touch down outside the UISlider followed by a drag will scroll the scrollview as expected
UIView has a property called exclusiveTouch which seems as if it might be related to my problem. I tried setting it to NO, with no luck.
So, how can is set up my UISliders so that the scrollview beneath them will respond to touches which originate inside the UISliders?
Have you tried subclassing UIScrollView and implementing - (BOOL)touchesShouldCancelInContentView:(UIView *)view? According to the Apple documentation:
// called before scrolling begins if touches have already been delivered to a subview of the scroll view. if it returns NO the touches will continue to be delivered to the subview and scrolling will not occur
// not called if canCancelContentTouches is NO. default returns YES if view isn't a UIControl
If you simply return NO if the view is your UISlider, this may do what you want, assuming your UIScrollView only scrolls horizontally. If this doesn't work, you likely will have to do custom touch handling (ie. overriding touchesBegan:withEvent:, touchesChanged:withEvent:, etc.) for both your UIScrollView and your UISlider.
What you are seeing is the intended behavior.
Each touch event only gets handled by one control. What exclusiveTouch does is actually to prevent other touch events from being delivered to other views.
To do what are trying to do you would have to do some of the touch handling yourself. Passing the event to both your views. You could do either do it by implementing all the touchesBegan:, touchesMoved: etc. methods and pass the events to both views. You can read more about that approach in the UIResponder documentation. Another approach is to do the event handling in a UIGestureRecognizer on the scroll view that hit tests the slider and updates the value of the slider using the y-delta. You can read more about gesture recognizers and event handling in the section about Gesture Recognizers in the Event Handling Guide for iOS.
Side note:
Go to the Settings app and toggle a switch half way (for example the Airplane mode toggle) and then drag down. Nothing will happen. The rest of the OS behaves the same way. Are you sure that this is the interaction that you really want to do? Apps that behave differently often feel weird and unfamiliar.
Your question confused me a bit. You are saying a vertical slider - but dragging left and right?
If you wish to scroll the scrollview when dragging the UISlider, the proper way to do so is
[mySlider addTarget:self action:#selector(sliderMoved:) forControlEvents:UIControlEventValueChanged];
and
- (void) sliderMoved:(UISlider*) slider {
myScrollView.contentOffset.x = slider.value * (myScrollView.contentSize.width - myScrollView.bounds.size.width);
}
Hope this is what you want.
You need to set delaysContentTouches as NO and prevent for UISlider objects to scroll, Check below code.
mySlider.delaysContentTouches = NO;
- (BOOL)touchesShouldBegin:(NSSet *)touches withEvent:(UIEvent *)event inContentView:(UIView *)view {
if ([view isKindOfClass:[UISlider class]])
{
UITouch *touchEvent = [[event allTouches] anyObject];
CGPoint locationEvent = [touchEvent locationInView:view];
CGRect thumbRect;
UISlider *mySlide = (UISlider*) view;
CGRect trackRect = [mySlide trackRectForBounds:mySlide.bounds];
thumbRect = [mySlide thumbRectForBounds:mySlide.bounds trackRect:trackRect value:mySlide.value];
if (CGRectContainsPoint(thumbRect, locationEvent))
return YES;
}
return NO;
}
I think you can get some reference from this example in this example it is shown that how to cancel any touch or any gesture recognizers and apply them to other views.
May this lead you to the solution of your problem and if it will just let me know about it
Happy Codding :)

Enable user interaction on a UIButton underneath a UIVew?

In my program I have configured a UIView to be placed over a number of UIBUttons. This is so you can pan across the view and have the buttons move over as well as selecting the buttons themselves. However, it is not possible to select the buttons when they are underneath this UIView. Does anybody know how to configure a UIButton's userInteractionEnabled while it is under these conditions?
This technique has worked for me - caveat: I just typed this in it's not compiled or nuthin it's given as a starting point.
Create the buttons and place them at the top of the view hierarchy (i.e. where they interact normally and respond to taps, on top of the UIView)
When creating the buttons - keep a reference to them in an array.
NSMutableArray* arrayOfPeskyButtons;
Keep a record of the button frames
NSMutableArray* arrayOfPeskyFrames = [NSMutableArray mutableArray];
for ((UIButton*)button in arrayOfPeskyButtons)
{
[arrayOfPeskyFrames addObject:[NSValue valueWithCGRect:button.frame];
}
When your app responds to an action on the UIView - panning or if you change it to a UIScrollView (probably best btw), then move the frame of the buttons correspondingly so that they appear fixed in place to the user.
I have used this technique to create 'overlays' of buttons which sit on top of scrollviews and it works surprisingly well. Simply in the scrollView delegate method.
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
take note of the scrollviews offset and adjust each of the frames of the buttons accordingly depending on the offset of the scrollView
CGPoint offset = scrollView.contentOffset;
Iterate through the array of buttons and move them.
int index = 0;
for ((UIButton*)button in arrayOfPeskyButtons)
{
CGRect originalFrame = [[arrayOfPeskyFrames objectAtIndex:index]CGRectValue];
CGRect currentFrame = button.frame;
//adjust frame
//e.g.
currentFrame.origin.x = originalFrame.origin.x + offset.x;
button.frame = currentFrame.;
index++;
}
Instead of changing the value of the UIButton's userInteractionEnabled, you should set the userInteractionEnabled value of the UIView on top of the UIButton to no.
When the UIView's userInteractionEnabled is set to YES, the UIView was 'eating up' the events. The button would then never receive any events.

Scrollview of tweetdeck

I am busy with an application and I want to implement a scrollview like the tweetdeck application.
You can zoom in en out UITableviews within an UIScrollview.
I was wondering how they did that. What I can imagine is that there are two UIScrollviews.
This because of the zoom in and out of one item in the UIScrollview.
What I don't understand is how the animation precise work and how they zoom out the UITableView.
I was wondering if someone can help me to get in the right direction
Actually a UITableView is a UIScrollView, so if you add some code like following, you will be able to zoom your table view.
self.tableView.maximumZoomScale = 2.0;
self.tableView.minimumZoomScale = 1.0;
And a UIScrollView delegate method
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
return self.tableView;
}