I have a view which shows an image from the web. The view only has an UIImageView. I want to know how to hide the navigationBar when the user taps and show it again when the user re-taps the view again. (Just like the native iPhone photo app)
I know i can use this
[self.navigationController setNavigationBarHidden:YES animated:YES];
but i am not sure where to use this,where to put in this code.
Help would be appreciated
Initialize a new UITapGestureRecognizer:
UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(toggleNavigationBar:)];
tapGestureRecognizer.numberOfTapsRequired = 1;
tapGestureRecognizer.numberOfTouchesRequired = 1;
[self.imageView addGestureRecognizer:tapGestureRecognizer];
[tapGestureRecognizer release];
You also must make sure the UIImageView has userInteractionEnabled set to YES because by default it is set to NO on UIImageView's.
self.imageView.userInteractionEnabled = YES;
Finally, write the method that is called when the gesture recognizer recognizes. This is the method selector that is passed in the action: argument in the gesture recognizer's initializer method:
- (void)toggleNavigationBar:(UITapGestureRecognizer *)tapGestureRecognizer
{
[self.navigationController setNavigationBarHidden:![self.navigationController isNavigationBarHidden] animated:YES];
}
Put a UITapGestureRecognizer on your UIImageView and in the delegate just call the method you mentioned. Something like this:
UITapGestureRecognizer* g = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(imageTapped:)];
[img addGestureRecognizer:g];
[g release];
Then your delegate:
-(void) imageTapped:(UITapGestureRecognizer*)tg
{
if(self.navigationController.toolbarHidden)
[self.navigationController setNavigationBarHidden:YES animated:YES];
else
[self.navigationController setNavigationBarHidden:NO animated:YES];
}
If you can't figure out the other answers you can cheat a little bit. You can throw on a button set it to transparent and link a IBAction to it with the code:
UIButton *imageButton = [[UIButton alloc] initWithFrame:CGRectMake( x,y,0,0)];
imageButton.backgroundColor = [UIColor clearColor];
[imageButton addTarget:self action:#selector(navBarHide:)
forControlEvents:UIControlEventTouchUpInside];
-(IBAction)navBarHide {
if (!navBarHidden) {
[self.navigationController.navigationBar removeFromSuperView];
}
else {
[YourUIView addSubview: yourNavigationBar];
}
}
Related
I've implemented 2 ImageViews with images and tapGestures, this works fine, now I want to segue to another ViewController dependent on which image I've tapped. I don't know how I have to declare my ImageView which ViewController it should call.
so my Code is:
-(void) initImageViews{
self.navigationItem.title = #"Menu";
UIImage *serviceImage = [UIImage imageNamed:#"Service.png"];
self.serviceImageView = [[UIImageView alloc] initWithImage:serviceImage];
[self.serviceImageView setFrame:CGRectMake(20, 20, 48,48 )];
[self.view addSubview:self.serviceImageView];
[self.view setBackgroundColor:[UIColor blackColor]];
UIImage *industryImage = [UIImage imageNamed:#"Industry.png"];
self.industryImageView = [[UIImageView alloc] initWithImage:industryImage];
[self.industryImageView setFrame:CGRectMake(73,20,48,48)];
[self.view addSubview:industryImageView];
UITapGestureRecognizer *singleTap =
[[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleSingleTap:)];
[self.industryImageView setUserInteractionEnabled:TRUE];
[self.serviceImageView setUserInteractionEnabled:TRUE];
[self.serviceImageView addGestureRecognizer:singleTap];
[self.industryImageView addGestureRecognizer:singleTap];
}
- (void)handleSingleTap:(UITapGestureRecognizer *)gestureRecognizer {
NSLog(#"Tapped!");
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:#"Service"]) {
SelectOptionViewController *controller = (SelectOptionViewController*)segue.destinationViewController;
}if ([segue.identifier isEqualToString:#"Industry"]) {
SelectOptionViewController *controller = (SelectOptionViewController*)segue.destinationViewController;
}
}
}
Thanks in advance :)
[self performSegueWithIdentifier:IdentifierForTheSegueiFromStoryBoard sender:nil];
EDIT: the seque must exist in the storyboard and must have an identifier.
If you don't want/need to use segues you could use:
if (i want controller A) {
[self presentViewController:ControllerA animated:YES completion:nil];
} else if (i want controller B)
[self presentViewController:ControllerB animated:YES completion:nil];
}
Create a seague in the storyboard with the identifier.then programatically call a segue like this
[self performSegueWithIdentifier: #"myidentifier" sender: self];
I am adding a UIImageView as a subview to a UIScrollView then i set the image.
Im trying to to use UITapGestureRecognizer.
The selector of the UITapGestureRecognizer is never called in iOS 5 (in iOS 6 it DOES!).
Tried many variations. this is my code:
UIImageView *imgView = [[UIImageView alloc]initWithFrame:CGRectMake(index*(IMAGE_WIDTH+10), 10.0, IMAGE_WIDTH, IMAGE_HEIGHT)];
[imgView setImageWithURL:[NSURL URLWithString:meal.RecommendedImageURL] placeholderImage:[UIImage imageNamed:#""]];
imgView.layer.cornerRadius = 4;
[imgView.layer setMasksToBounds:YES];
[imgView setUserInteractionEnabled:YES];
[imgView setMultipleTouchEnabled:YES];
[scrollView addSubview:imgView];
if (IOS_NEWER_OR_EQUAL_TO_5)
{
UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(imageTapped:)];
[imgView addGestureRecognizer:tapGestureRecognizer];
tapGestureRecognizer.numberOfTapsRequired = 1;
tapGestureRecognizer.enabled = YES;
tapGestureRecognizer.delegate = self;
[tapGestureRecognizer setCancelsTouchesInView:NO];
}
this is my selector which is only called in iOS5:
- (void) imageTapped: (UITapGestureRecognizer *)recognizer
{
//Code to handle the gesture
UIImageView *tappedImageView = (UIImageView*)recognizer.view;
GGFullscreenImageViewController *vc = [[GGFullscreenImageViewController alloc] init];
vc.liftedImageView = tappedImageView;
vc.liftedImageView.contentMode = UIViewContentModeScaleAspectFit;
if (IOS_NEWER_OR_EQUAL_TO_5) {
[self.parentViewController presentViewController:vc animated:YES completion:nil];
}
else
{
[self.parentViewController presentModalViewController:vc animated:YES];
}
}
In addition to that, i tried to setCancelsTouchesInView to YES in my UIScrollView but it doesn't work either.
Thanks for your help!
try to add Gesture in Scrollview then check iskind of class image view
UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(imageTapped:)];
[scrollview addGestureRecognizer:tapGestureRecognizer];
tapGestureRecognizer.numberOfTapsRequired = 1;
tapGestureRecognizer.enabled = YES;
tapGestureRecognizer.delegate = self;
[tapGestureRecognizer setCancelsTouchesInView:NO];
- (BOOL)gestureRecognizer:(UITapGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
// test if our control subview is on-screen
if ([touch.view isKindOfClass:[UIImageView class]]) {
// we touched a button, slider, or other UIControl
return YES;
}return NO;
}
Implement the 3 methods of UIGestureRecognizerDelegate & return default values:
-(BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer;
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer;
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch;
Ok i solved it very EASILY!
the problem was setting the delegate of the UITapGestureRecognizer to self.
when i removed the delegate = self, it started working :)
Thanks for your assistance
I have implemented UITapGestureRecognizer on UIImageView, It is working on first tap. On First Tap, I am hiding that image and starting animation. Once the animations are completed, i am showing the image again. But After setting setHidden:FALSE, I am not getting the Tap event of that UIImageView.
Following is the code I am using :
- (void)viewDidLoad{
[super viewDidLoad];
defaultDogView= [[UIImageView alloc] initWithFrame:CGRectMake(3, 270, 110, 210)];
[defaultDogView setImage:[UIImage imageNamed:#"dog1.png"]];
defaultDogView.userInteractionEnabled = YES;
[self addGestureRecognizersToPiece:defaultDogView];
[self.view addSubview:defaultDogView];
}
- (void)addGestureRecognizersToPiece:(UIImageView *)piece
{
NSLog(#"in Gesture");
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(singleTapPiece:)];
[tapGesture setDelegate:self];
[piece addGestureRecognizer:tapGesture];
[tapGesture release];
UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(longPressPiece:)];
[piece addGestureRecognizer:longPressGesture];
[longPressGesture release];
NSLog(#"%#", [piece gestureRecognizers]);
}
- (void)singleTapPiece:(UITapGestureRecognizer *)gestureRecognizer
{
NSLog(#"Image Tapped");
/** Hide the default Image and start the animation ***/
[defaultDogView setHidden:TRUE];
/***Animating the Dog***/
[dogArray addObject:[SpriteHelpers setupAnimatedDog:self.view numFrames:69 withFilePrefix:#"dog" withDuration:(12) ofType:#"png" withValue:0]];
dogView = [dogArray objectAtIndex:0];
//[self addGestureRecognizersToPiece:dogView];
[self performSelector:#selector(callBubbleUpdater) withObject:nil afterDelay:5.5];
}
-(void)showDogFrame{
NSLog(#"%#",[defaultDogView gestureRecognizers]);
[defaultDogView setHidden:FALSE];
defaultDogView.userInteractionEnabled = YES;
}
When view is hidden or its alpha component is zero that view won't receive any UIGestureRecognizers.
I can suggest to use next approach if you need to hide some view (let's name it touchableView) but want it to respond to gestures:
Create backgroundView with the same frame as touchableView:
UIView *backgroundView = [[UIView alloc] initWithFrame:touchableView.frame];
Set background color of backgroundView to clearColor:
backgroundView.backgroundColor = [UIColor clearColor];
Reset position of touchableView:
CGRect frame = touchableView.frame;
frame.origin.x = 0;
frame.origin.y = 0;
Disable user interaction of touchableView:
touchableView.userInteractionEnabled = NO;
Add touchableView as subview to backgroundView:
[backgroundView addSubview:touchableView];
Add appropriate gesture recognizers to backgroundView.
Add backgroundView to view that you want.
Now you can hide touchableView but you will still receive gesture recognizers.
I don't test this but I think it should work.
sure
when UIImageView is hidden. it does not receive any touch events
set alpha zero for uiimageview
see the screen shot is clear to understand what I mean
you can see I add a navigationItem in my pop view
I wish I can dismiss the pop view
But it seems only tab the cell under the pop view
The pop view will dismiss,I try to add this method
[self.view removeFromSuperview];
It only remove the table view , the pop view frame is still there ,only without the content view
Any reply will be helpful : )
Thanks
Webber
/******EDIT******/
I use WEPopoverView into my project
And this is the code I create the pop view when I select the table view
if (indexPath.row==2) {
DaysOfWeek *popView = [[DaysOfWeek alloc]init];
UINavigationController *navPopView = [[UINavigationController alloc] initWithRootViewController:popView];
if (self.popoverController) {
[self.popoverController dismissPopoverAnimated:YES];
self.popoverController = nil;
}
else {
self.popoverController = [[[WEPopoverController alloc] initWithContentViewController:navPopView] autorelease];
CGRect frame = [tableView cellForRowAtIndexPath:indexPath].frame;
[self.popoverController presentPopoverFromRect:frame
inView:self.view permittedArrowDirections:UIPopoverArrowDirectionDown|UIPopoverArrowDirectionUp
animated:YES];
}
}
/******EDIT2******/
I try to add Done button when I create the pop view
here is the code , But it only appear a navigation , no Done button
DaysOfWeek *popView = [[DaysOfWeek alloc]init];
UINavigationController *navPopView = [[UINavigationController alloc] initWithRootViewController:popView];
navPopView.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(hidePopView)];
While you add the popup view, set tag to that popupView and then, add them as subview,
then use:
for (UIView *tempView in [self.view subviews]) {
if ([tempView tag]==urTag) {
[tempView removeFromSuperview];
}
}
This retrieves all the subviews and then remove only your popupview
I think that simply releasing your self.popoverController will do the dismiss properly, including all the superviews.
You can also have a look at the dealloc method in WEPopoverController to see which views are involved and need to be removed:
[self dismissPopoverAnimated:NO];
[contentViewController release];
[containerViewProperties release];
[passthroughViews release];
Anyway, the only advantage I see is the possibility of calling dismissPopoverAnimated with YES.
Hope this helps.
EDIT:
How can you connect your done button to your controller?
Make your button accessible through a read-only property of DaysOfWeek; then in your controller, when you create DaysOfWeek, do:
DaysOfWeek *popView = [[DaysOfWeek alloc]init];
[propView.doneButton addTarget:self action:#selector(fullyDismissPopover) forControlEvents:UIControlEventTouchUpInside];
In fullyDismissPopover, you call release or call the sequence of functions highlighted above (but release would be better, I think).
DaysOfWeek *popView = [[DaysOfWeek alloc]init];
UIButton *doneButton = [UIButton buttonWithType:UIButtonTypeContactAdd];
[doneButton addTarget:self action:#selector(hidePopView) forControlEvents:UIControlEventTouchUpInside];
popView.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithCustomView:doneButton] autorelease];
UINavigationController *navPopView = [[UINavigationController alloc] initWithRootViewController:popView];
This also can figure out the problem !
I have a problem that I'm helping someone can shed some light on. I'm sure it is something very simple, but I just can't see it and could really use an extra set of eyes to help find this bug.
The app I'm working on uses UIImagePickerController to present a camera preview with the stock camera UI hidden. This preview implements a cameraOverlay view with a few buttons to perform my required tasks, one of which is to dismiss the camera preview.
For some reason, the function to dismiss the camera view will not work when being called by the button in the overlay view. I can NSLog the function and see that it is in fact being called by the button, but simply doesn't allow the dismissModalViewController to work. I have tested the function by having an NSTimer call it a few seconds after it loads, and it works fine, just not when I call it from the button in the cameraOverlay.
My viewController (CameraTestViewController.m) calls the UIImagePicker as shown below with the press of a button.
-(IBAction) getPhoto:(id) sender {
OverlayView *overlay = [[OverlayView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGTH)];
picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
picker.cameraDevice = UIImagePickerControllerCameraDeviceFront;
picker.cameraOverlayView = overlay;
picker.showsCameraControls = NO;
picker.toolbarHidden = YES;
[self presentModalViewController:picker animated:NO];
//[self performSelector:#selector(doCancel) withObject:nil afterDelay:10];
}
I dismiss the camera view with this method elsewhere in the viewController
-(void)doCancel
{
NSLog (#"doCancel method has been called");
[picker dismissModalViewControllerAnimated:YES];
}
Then in my OverlayView.m class, I set up the required buttons and provide a method that calls the dismissing of the modal view controller.
- (id)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
// Clear the background of the overlay:
self.opaque = NO;
self.backgroundColor = [UIColor clearColor];
...
myButton04 = [UIButton buttonWithType:UIButtonTypeCustom];
myButton04.frame = CGRectMake(10, 447, 44, 13);
[myButton04 setTitle:#"" forState:UIControlStateNormal];
[myButton04 addTarget:self action:#selector(scanButton05TouchUpInside) forControlEvents:UIControlEventTouchUpInside];
[myButton04 setImage:[UIImage imageNamed:#"camControlsBack.png"] forState:UIControlStateNormal];
[self addSubview:myButton04];
...
}
return self;
}
- (void) scanButton05TouchUpInside {
NSLog(#"BACK BUTTON HAS BEEN PRESSED");
CameraTestViewController *baseViewController = [[CameraTestViewController alloc] init];
[baseViewController doCancel];
}
ANY help on this would be most appreciated. I'm sick of having a purple forehead from beating my head against the wall over this one. :cool:
THANKS!
Use this Below code:
[[[self presentingViewController] presentingViewController] dismissModalViewControllerAnimated:YES];
The way I dismiss my modalViewController is to call its parent:
[[picker parentViewController] dismissModalViewControllerAnimated:YES];