How to stop uiscrollview scrolling where user stopped touching the screen - objective-c

I have a UIScrollView, and what I need, is that when the user stops dragging it, the scrollview stops scrolling to.
Sometimes it works, but sometimes, the scroll continues when the pression is released.
I tried a lot of things with the scrollView.tracking, or with delegate methods like scrollViewDidScroll, or scrollViewDidEndDecelerating, etc. But for the moment, I didn't found anything !
I also tried UIPageControl but I need my scroll view to stop every 50 pixel.
Thank you for your time and your help !

You need to impelement this method (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate the decelerate parameter means should scroll view stop immediately or not. Try to use decelerationRate property to set the needed rate.

Related

UIbutton not responding to touch

I have this view hierarchy:
The three buttons don't response. They are enabled, just when I touch there, they don't do anything (not even change their gradient/tint) which tells me they are not even received the touches. Is there something I am doing wrong?
Thanks
User interaction is enabled on all the buttons' superviews, and the buttons themselves. The buttons are also enabled.
Are you using any imageViews on top or containing the UIButtons? If so, sometimes, those absorb the touches because they have the userInteraction boolean set to NO by default. Maybe even getting rid of your gradient imageView (for the time being) to determine if those are causing the issues.
If not, I would set a breakpoint on the viewDidLoad method of the superview and type in [self.view recursiveDescription]. It should give you at-a-glance overview of what's around those buttons and might be stealing the touches.
Good luck!

Pull to reveal hidden navBar-like menu in iOS? as seen in GetPocket App

I've been driving myself mad over this one. It might be one of those things where I need to take a step back and figure out the simplest way to implement this. Can't find anything on this either. I google-fu'd the heck out of this one.
In the Pocket App when you pull down a menu reveals itself just like the searchBar does. In this instance the faux bar when you pull it past its halfway point if you release it shows springs into place. If it is release before the halfway point the bar will snap back and hide. As shown here
In my case I've been trying to replicate this with no luck. In my case I have a UITextView inside of a UIViewController view. I think I have it all wrong.
I can get it to work with a UIScrollView hidden by initiating with it offscreen and then when I press a button the UIScrollView reveals itself. The problem is that this method covers everything so I'll have to resize and relayout a bunch of views. Is this in a UITableView possibly? I want it to be a pull action though and just want to put some TextStrings/Labels in this bar.
Thank you in advance.
Feels like you need a UIScrollView (or UITableView) and to put the menu you want to reveal at the top (in the table view header, for example) and then get the delegate callbacks for scrolling.
UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
You might also need to watch for some of the dragging delegate callbacks
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
You can initially hide the menu by setting the content offset to the height of the menu, then catch the delegate callbacks for scrolling and, if the scrolling has reached beyond half the size of the menu set the content offset with animation.
- (void)setContentOffset:(CGPoint)contentOffset animated:(BOOL)animated
If I understand your goal correctly you don't need the scroll view.
What I would do is have an simple UIView containing all the stuff you need and place it outside of the visible area. This will be your pull-down view. Then add an UIPanGestureRecognizer to the view in your UIViewController and use it to track the movement of your finger and update the frame of the pull-down view accordingly. Then in this update method you simply check if the position has passed some threshold and if so you let the pull-down view snap to its final position (using animations of course).
If you are unfamiliar with the UIPanGestureRecognizer there is a really good tutorial here:
http://www.raywenderlich.com/6567/uigesturerecognizer-tutorial-in-ios-5-pinches-pans-and-more

Restricting UIScrollView scrolling to only one page at a time

I would like to limit a UIScrollView to only being able to scroll one page at a time.
In other words, even when the user flicks really fast, I want the UIScrollView to be restricted to scrolling only one page.
Is there a good way to do this?
UIScrollView *myScrollView;
...
myScrollView.pagingEnabled = YES;
When set to YES, the scroll view stops on multiples of the scroll view’s.
One way to achieve this, if you haven't already settled on the scroll view, is to use a UIPageViewController. I'm looking at one on my app and this functionality seems to come for free.
With the UIScrollView, I'm thinking you might want to respond to the UIScrollViewDelegate method - (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset
I was thinking you could hold your current 'page' in a property, and then in the scrollViewWillEndDragging:withVelocity:targetContentOffset: method you could modify the target content offset to only be the next/previous page.
There's a 2012 WWDC video, Enhancing User Experience with Scroll Views, that uses this approach if you wanted to see it in action.

TTPhotoViewController - disable dragging

I'm trying to customize the three20 TTPhotoViewController so that the first & last images cannot be dragged.
Example of what I am trying to prevent.
https://plus.google.com/photos/109413514618904450093/albums/5730490807945885537?authkey=CMTi6OfqhLS2NQ
The image shows the currently selected image has been dragged upwards. This behaviour does not occur on the central images only the first & last.
The code responsible for this behaviour is in the TTScrollView. If it was a uiscrollview I would just set bounces to NO.
Can anyone tell me how I can achieve this result?
I think the issue is in TTScrollView:resistPageEdges but my hacking has left me nowhere
TTPhotoViewController delegates TTScrollViewDelegate.
TTScrollViewDelegate has -(void)scrollViewWillBeginDragging:(TTScrollView *).
TTScrollView has (void)cancelTouches.
Therefore, you can write this in your own subclass of TTPhotoViewController.
- (void)scrollViewWillBeginDragging:(TTScrollView *)scrollView {
[scrollView cancelTouches];
}
Every time a user try to drag, it will cancel any active touches and resets everything to an untouched state.

UIButton firing selector inconsistantly

I have a UIButton linked up in IB correctly(I believe). The button fires inconsistently, every time I reload the view to show updated info, the button works sometimes and sometimes does not.It gives no errors. I can't find a pattern to when it works and when it doesn't, the same code is run every time I open the view and it still works when it wants. Besides linking it in IB I have also tried to addTarget in ViewDidLoad and remove the IB connection but it still has the same inconsistency,
[_buttonScreen addTarget:self action:#selector(buttonScreenClicked) forControlEvents:UIControlEventTouchUpInside];
If I add NSLog(#"Clicked"); to buttonScreenClicked I see that the method doesn't always get called, what would cause it to do this, I have made sure that I set:
[_buttonScreen setAlpha:0.1];
[_buttonScreen setHidden:NO];
[_buttonScreen setUserInteractionEnabled:YES];
I have no Image, text, or color in the button, but it still works sometimes.
I'm using AFKPageFlipper on the same view but it still had the same problem before I added AFKPageFlipper, so I don't think its that.
If anyone could point me in any direction to start trouble shooting this problem I would appreciate.
Thanks
I just had the same problem and worked it out. The 5 seconds is the clue.
Somewhere you have a gesture recognizer covering the same space as your button. More specifically you have a gesture recognizer that is eating your Taps but not your LongPresses. If you just tap the button the gesture recognizer runs off with your event; Hold your finger down long enough and the gesture recognizer no longer considers it a tap and the event is passed through to your button.
Instrument your Tap gesture recognizer handlers and the problem should pop out at you.
Make sure you don't have any other UIView descendants overlaying the button (like a transparent UIScrollView) as these will intercept the touch events first.
Also make sure that the containing view (the view with the button in) is correctly sized, by default you can place a view outside the bounds of another view and the clipsToBounds is set to false so you will see it but not be able to interact with it.
Things to try:
Do you have any other actions on the button?
Do you have any other UIViews which could possibly be accepting the key presses (above or below, or un-shown)
Also, please check that you have only one UIViewController instance for this screen. Other issues may arrise because of that.
What happens if you dont set the alpha level?
Do you release the object properly in the dealloc only ?
Hope this helps