I've been using a UITapGestureRecognizer for quite some time now to take notice when a tap occurs and to display another view. I'm initializing it like this:
_tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(showModalDetailView:)];
In iOS 4.3, that did work perfectly. In iOS 5, -showModalDetailView: does not get called anymore.
I was wondering if anyone else has this issue, too?
Update
I'm configuring the recognizer like this:
_tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(showModalDetailView:)];
_tapGesture.numberOfTapsRequired = 1;
_tapGesture.numberOfTouchesRequired = 1;
_tapGesture.delegate = self;
[self addGestureRecognizer:_tapGesture];
Update 2
Ok, now it gets weird. The code starts working again once I disable the delegate setting by commenting it. However, the delegate methods are still being called. But not always.
The cell has a left aligned title and a right aligned detail text. When you hit the detail text it works as expected: The delegate method does not get called and the popover appears. When you hit the cell somewhere else, the delegate method (-gestureRecognizer:shouldReceiveTouch: in this case) gets called and the popover appears.
I don't quite get what's going on here.
Now that I implement all three delegate methods (though they're optional) it works. Just returning the default values. Thanks, Dunkelstern and thomas.
Try setting numberOfTouchesRequired and numberOfTapsRequired to 1 explicitly.
And of course you have to call addGestureRecognizer on your View instance.
Related
I'm looking to add a Pull to refresh feature to a UIWebView that I have in an app. I've used Pull to refresh in the past on table views, but I'm struggling to find any good tutorials/libraries to help me add it to UIWebViews specifically and this project.
I did get some of the way using PullToRefreshView by chpwn (https://github.com/chpwn/PullToRefreshView) but I can't get it detecting the drag.
Does anyone know a better plug in/library?
For me this post and code worked like a charm
http://sonnyparlin.com/2011/12/pulltorefresh-ios-5-and-arc-tutorial/
I'm new to iOS development, so this may not be the best way… but it worked for me.
I took the controller that was wrapping my WebView and I changed it to a UITableViewController (it was just a UIViewController previously) so that I could take advantage of the refreshControl property of that controller (ios6 and up). I'm not making use of the TableView inside of the UITableViewController (though, it does seem to require one to exist, which I include, but I just don't populate it). I added 2 methods to the controller:
- (void)addPullToRefresh
{
UIRefreshControl *refreshControl = [[UIRefreshControl alloc] init];
refreshControl.attributedTitle = [[NSAttributedString alloc] initWithString:#"Pull to Refresh"];
[refreshControl addTarget:self.myWebView action:#selector(reload)
forControlEvents:UIControlEventValueChanged];
self.refreshControl = refreshControl;
}
- (void)stopRefresh
{
[self.refreshControl endRefreshing];
}
I put a call to [self addPullToRefresh]; in viewDidLoad, and I added a call to [self stopRefresh]; in webViewDidFinishLoad:
I got my instructions for how to use the refreshControl property of UITableViewController here.
I have a custom UITableViewCell with four UIImageView inside it. I want to detect touch on these imageViews and notify my ViewController (which is containing the UITableView) which ImageView inside which cell has been tapped. How may I do that?
Try this:
-(void)createGestureRecognizers {
for(/*each of the UIImageViews*/) {
UITapGestureRecognizer *singleFingerTap = [[UITapGestureRecognizer alloc]
initWithTarget:self action:#selector(handleSingleTap:)];
singleFingerTap.numberOfTapsRequired = 1;
[imageView addGestureRecognizer:singleFingerTap];
imageView.tag = i;
[singleFingerTap release];
}
}
- (void)handleSingleTap:(UIGestureRecognizer *)sender {
UIImageView *imageView = (UIImageView *)sender.view;//this should be your image view
}
There are a couple variants to hit the target. In fact it doesn't matter at all how you are doing it at the cell's level. It may be buttons, image view with tap gesture recognizers or custom drawing and proceeding touch events. I will not provide you with code now as you have a lot of code already given and may find a lot more by little search, thought it may be provided by demand. The real "problem" is in transporting message to the controller. For that purpose I've found only two reasonable solutions. First is the notifications and second is the delegations. Note that the notifications method may lead to visible lag between tap and actual event occurrence as sometimes notifications are brought to objects with little delay...
Hope that it helps.
Use four buttons and set them with different tag values. Then just check the tag value to see which one was pressed.
Excuse the bad pun, I'm creating a custom tab bar in my iPhone app using UIButtons. Everything works beautifully except that when I tap and hold a button, it doesn't select it until I release it. It's really bugging me, because a standard UITabBarItem is selected on touch down and it just feels wrong.
I've set the IBAction to "Touch Down" and my code is pretty simple. Am I doing something wrong?
-(IBAction)tab1Pressed:(id)sender
{
if (self.tab1.selected == NO) {
self.tab1.selected = YES;
self.tab2.selected = NO;
}
}
Got a solution! Added a UILongPressGestureRecognizer to each UIButton, instead of using IBActions. Worked like a charm!
In viewDidLoad:
tapAndHold = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(handleTapAndHold)];
[tapAndHold setMinimumPressDuration:0.01];
[self.myTabBarButton addGestureRecognizer:tapAndHold];
[tapAndHold release];
You should use another event for your button. The default one is "touch up inside". The action is triggered when you release the button.
Using "touch down" should do what you want.
You can try subclassing a UIView and implementing touchesBegan:withEvent: method.
Using this method to hide the status bar:
[[UIApplication sharedApplication] setStatusBarHidden:YES animated:YES];
When setting "hidden" back to NO, the tap-to-scroll-to-top (in UIWebView, UITableView, whatever) doesn't work any more, and requires a restart of the app to get the functionality back.
Is this a bug (I filed a rdar anyhow) or have I missed a step? Should I perhaps expect this behavior since the statusBar "loses touch" somehow with the respective view?
You could try setting the ScrollsToTop property to true again after re-showing it:
[currentView setScrollsToTop:YES];
If that's not working, are you definitely only showing one view? If there is more than one scrolling view a scrollViewDidScrollToTop message is ignored...
In iOS 5.0 you can access the scrollview property of the UIWebView
webView.scrollView.scrollsToTop = YES;
The following fix by Alex worked for me. Thanks!
((UIScrollView *)[[webView subviews] objectAtIndex:0]).scrollsToTop = NO;
Being in a hurry this fix worked great, however given more time I might've subclassed the UIWebView and accessed the protected UIScrollView member directly.
The worry I have with Alex' method is that it assumes that UIScrollView is at index zero of the subviews (encapsulation allows private members to change). Which suggests another solution still:
for (UIView* v in [webView subviews])
{
if ([v isKindOfClass:[UIScrollView class]])
{
(UIScrollView *)v.scrollsToTop = NO;
}
}
I was having a similar problem where the scroll-to-top functionality was lost. Turns out this will only work when you have only one active view at a time (within the same scroll view). In my case I had a table view and another view which would fade in/out. Adding a removeFromSuperview at the end of the animation did the trick.
The answer was in the UIScrollView.h file comments:
/*
this is for the scroll to top gesture. by default, a single scroll visible scroll view with this flag set will get the call. if there is more than one visible with this
flag set or the delegeat method returns NO, the view isn't scrolled
*/
#property(nonatomic) BOOL scrollsToTop; // default is YES. if set, special gesture will scroll to top of view after consulting delegate
You can use the following code to have the UIWebView ignore scrollToTop without the extra UIScrollView:
((UIScrollView *)[[webView valueForKey:#"_internal"] valueForKey:#"scroller"]).scrollsToTop = NO;
I had a similar problem after playing a Youtube video within my app. scrollsToTop was still set to YES but tapping the status bar had no effect.
I finally realised that my app window was no longer the key window. After adding the following line to a UIWindow subclass (which I already had for other reasons) everything worked as it should again:
if (![self isKeyWindow]) [self makeKeyWindow];
I just ran across a similar behavior in the app I'm currently working on. In its case, if you load a YouTube video from within a UIWebView, scroll to top stops working for the rest of the application's life cycle. I kind of assume this might happen after loading the movie player as well, but haven't confirmed. That functionality has been around a lot longer and probably has fewer bugs.
When there are multiple scrollview, you can also set scrollUpToTop to NO for the others scrollview. cf:
setScrollsToTop with multiple UIScrollView classes and/or subclasses(UITableView)
I want to add my case, I add an UIWebView on an UIScrollView, as h4xxr had answered on the top:
If there is more than one scrolling view a scrollViewDidScrollToTop message is ignored
So, I get a simply way to make it work on webView: just set the scrollView·s scrollsToTop property false.
And when tap the status bar, it won`t got intercepted by the scrollView, and the webView scrolls to the top!
UIScrollView *scrollView = [[UIScrollView alloc] init];
scrollView.frame = self.view.bounds;
scrollView.scrollsToTop = false; //igore scrollView`s scrollsToTop
[self.view addSubview:scrollView];
UIWebView *webView = [[UIWebView alloc] init];
webView.frame = scrollView.bounds;
[scrollView addSubview:webView];
I'm trying to use the UIImagePickerController interface from OS 3.1, with the cameraOverlayView and takePicture, but I've clearly failed to understand how this works, and so I'm not getting the behaviour I want.
What I want to do is open the camera and take a picture automatically without having to having the user interact with the picker or edit the image. So I subclass UIImagePickerController (similar to the example in http://github.com/pmark/Helpful-iPhone-Utilities/tree/master/BTL%20Utilities/) and turn off all of the controls:
- (void)displayModalWithController:(UIViewController*)controller animated:(BOOL)animated {
self.sourceType = UIImagePickerControllerSourceTypeCamera;
self.showsCameraControls = NO;
self.navigationBarHidden = YES;
self.toolbarHidden = YES;
// Setting the overlay view up programmatically.
ipView = [[ImagePickerView alloc] init];
self.cameraOverlayView = ipView;
[controller presentModalViewController:self animated:NO];
}
In the overlayView, I've managed to force the takePicture method of UIImagePickerController to fire (I know this, because I can NSLog it, and I hear the sound of the camera taking a picture). The overlayView shows up just fine. However, the delegate method didFinishPickingMediaWithInfo: never gets called, and imagePickerControllerDidCancel doesn't get called either.
So, how do I either get the delegate methods to get called, or save the picture by overriding the takePicture method? (I have no idea how to capture the picture data here, and Google seems to have failed me). I can't help feeling that I've failed to understand how the guts of UIImagePickerController works, but the docs aren't overly helpful:
e.g.:
"You can provide a custom overlay view to display a custom picture-taking interface and you can initiate the taking of pictures from your code. Your custom overlay view can be displayed in addition to, or instead of, the default controls provided by the image picker interface."
or from showCameraControls:
"If you set this property to NO and provide your own custom controls, you can take multiple pictures before dismissing the image picker interface." - How do I dismiss the picker interface?
Note: the delegate is set properly in IB, so that's not the problem.
Thanks for any help you can provide!
I've found that you just have to wait "long enough" before calling takePicture, or it just silently fails. I don't have a good answer for how to determine the minimum value of "long enough" that will always work, but if you set a timer and wait five or ten seconds you should be okay. It would be nice if it returned some kind of an "I'm not ready to take a picture yet, sorry" error either directly from takePicture or through the delegate, but as far as I know it doesn't.
As an update to my own question: It turns out that I was trying to use takePicture too early. When I moved the action to a button on the overlay and sent takePicture from that button (once the picker was presented modally), the delegate methods fired as they should. I don't know if what I wanted is achievable - taking the image without having to press that button, automatically - but if it is, it will probably have to be done by sending takePicture sometime after I was trying to use it.
-(void)imageMethod:(id)sender{
imagePickerController = [[UIImagePickerController alloc]init];
imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
imagePopover=[[UIPopoverController alloc]initWithContentViewController:imagePickerController];
[imagePopover presentPopoverFromRect:importButton.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionLeft animated:YES];
}