iShidden always returning YES - iphone

using following code
-(IBAction)shareMenuBtnPressed:(id)sender
{
[self.mainMenuView setHidden:YES];
[self.tryOnView setHidden:YES];
[self.d3View setHidden:YES];
if ([self.shareView isHidden] == YES)
[shareView setHidden:NO];
else
[self.shareView setHidden:YES];
}
on first Click it shows the menu i-e [shareView setHidden:NO];
but if i click again it again runs the same seq. why is isHidden not changing to NO
EDIT: Ok it seems that i have placed this code
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(hideAllViews)];
//tap.cancelsTouchesInView = NO;
[self.view addGestureRecognizer:tap];
with tap.cancelsTouchesinView it bevhaves strangely and [shareView setHidden:NO] gets called always. atm i have put commments but I can't remove tap.cancelsTouchesinView. what should be done?

no need to write below code...
if ([self.shareView isHidden] == YES)
[shareView setHidden:NO];
else
[self.shareView setHidden:YES];
instead write below code...
self.shareView.hidden = !self.shareView.hidden;
i think it will help you a lot...
Let me know it is working or not my friend!!!!!
Happy Coding....

Related

UITapGestureRecognizer for UIImageView on UIScrollView never called iOS5

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

Hide a UITableView of a UIScrollview on a button click in iPhone

I have a UITableView with some data in -(void)ViewDidLoad. So it appears accordingly. But when I click a button then my tableview should be hidden and I should load a UIlabel with some text.
But for me on button click empty tableview with some rows is loading. I should avoid it. How can I do it?
Here is my code
-(void)ViewDidLoad {
tableView=[[UITableView alloc]initWithFrame:CGRectMake(0,380,320,200) style:UITableViewStylePlain];
tableView.delegate = self;
tableView.dataSource = self;
[self.view addSubview:tableView];
}
-(void)DatePickerDoneClick:(id)sender {
[tableview setHidden:YES];
displayError =[[UILabel alloc]init];
[displaytError setFrame:CGRectMake(20,400,320,100)];
displayError.textAlignment=UITextAlignmentLeft;
displayError.backgroundColor=[UIColor clearColor];
displayError.text=[NSString stringWithFormat:#"Not found"];
[self.view addSubview:displayError];
}
Could not get where I'm going wrong?
To hide Table View you can use
myTableView.hidden = YES;
or
[mtTableView setHidden:YES];
Hope this will help you out.
if tableview.hidden = YES; is not doing the job
try the next code:
[self.tableview removeFromSuperView];

How to add Swipe Gestures to UITableView cell?

I added this code in cellForRowAtIndexPath
UISwipeGestureRecognizer *gestureR = [[UISwipeGestureRecognizer alloc]
initWithTarget:self action:#selector(handleSwipeFrom:)];
[gestureR setDirection:UISwipeGestureRecognizerDirectionRight];//|UISwipeGestureRecognizerDirectionRight)];
[cell addGestureRecognizer:gestureR];
it works fine. But I want UISwipeGestureRecognizerDirectionLeft so Added like this
[gestureR setDirection:UISwipeGestureRecognizerDirectionLeft|UISwipeGestureRecognizerDirectionRight)];
When I check with direction and state I am always getting 3 = 3
- (void)handleSwipeFrom:(UISwipeGestureRecognizer *)recognizer {
NSLog(#"%d = %d",recognizer.direction,recognizer.state);
}
if I apply only one Gesture it works fine. I tried to add two gestures one by one. but it will responding for only one gesture.
How to add second gestures. I added directly to one gesture to TableView another one to cell but now use.
Try this
UISwipeGestureRecognizer* gestureR;
gestureR = [[[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipeFrom)] autorelease];
gestureR.direction = UISwipeGestureRecognizerDirectionLeft;
[view addGestureRecognizer:gestureR];
gestureR = [[[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipeFrom)] autorelease];
gestureR.direction = UISwipeGestureRecognizerDirectionRight; // default
[view addGestureRecognizer:gestureR];
If you want to handle different functionalities on left and right swipes, just change the selectors.
Instead of two times alloc, it would be better if you use
UISwipeGestureRecognizer* recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipe:)];
[recognizer setDirection:UISwipeGestureRecognizerDirectionLeft+UISwipeGestureRecognizerDirectionRight];
[cell addGestureRecognizer:recognizer];
And get the direction of swipe in the action as :
-(void)handleSwipe:(UISwipeGestureRecognizer *) sender
{
if (sender.direction == UISwipeGestureRecognizerDirectionLeft)
{
//do something
}
else //if (sender.direction == UISwipeGestureRecognizerDirectionRight)
{
//do something
}
}
I know its been ages since you asked this.
But try and read following line again in your question.
[gestureR setDirection:UISwipeGestureRecognizerDirectionRight|UISwipeGestureRecognizerDirectionRight)];
Did you realise you added UISwipeGestureRecognizerDirectionRight. Twice!!
:D

UIImageView tap to hide navigation bar

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];
}
}

UIBarButton not working when toolbar superview has UITapGestureRecognizers?

I am experiencing this strange behavior.
I create a clean project (view template), add a toolbar with a button and hook it up with an action. it works ;)
BUT, then when I add a UITapGestureRecognizer to the view of my viewcontroller the toolbar button stops working. (It is pressed but its action is not called)
When I add the UITapGestureRecognizer only the action linked to it is called. It is like uitapgesture recognizer view were hiding the toolbar but actually is not.
What is happening here? What am I missing?
- (IBAction)itemAction{
NSLog(#"%s", _cmd);
self.view.backgroundColor = [UIColor whiteColor];
}
- (void) tapAction{
NSLog(#"%s", _cmd);
self.view.backgroundColor = [UIColor greenColor];
}
- (void)viewWasTapped:(UITapGestureRecognizer *)recognizer{
if (recognizer.state == UIGestureRecognizerStateRecognized) {
[self tapAction];
}
}
- (void)viewDidLoad {
[super viewDidLoad];
UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(viewWasTapped:)];
[tapGestureRecognizer setNumberOfTapsRequired:1];
[self.view addGestureRecognizer:tapGestureRecognizer];
[tapGestureRecognizer release];
}
EDIT: the project source can be downloaded from here
Thanks in advance for any advice
Ignacio
I've encountered a similar problem, and found this answer quite helpful, especially because it allows selectively excluding the gesture recognizer based on the type of view that was touched.
Eventually I found the reason:
tapGestureRecognizer.cancelsTouchesInView = NO;