Unable to get touch on my UIImageView - iOS - ios

I need to make my every single UIImageView to be single tapped.
I have a viewController, on this view controller there is a view which i push another views on this view with containment.
I am trying to create gallery ,
What might I be doing wrong?
This is my code;
-(id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Custom initialization
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(openPhotoDetail)];
[self addGestureRecognizer:singleTap];
[self setBackgroundColor:[UIColor lightGrayColor]];
}
return self;
}
-(void)openPhotoDetail
{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"SideMenu" bundle:[NSBundle mainBundle]];
PhotoDetailViewController *photoDetailViewController = [storyboard instantiateViewControllerWithIdentifier:#"photoDetailViewController"];
AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
[appDelegate.window.rootViewController.menuContainerViewController.centerViewController pushViewController:photoDetailViewController animated:YES];
}

Initially a UIImageView has user interaction disabled. Enalbe the user interaction of your UIImageView and it should work.
imageView.userInteractionEnabled = YES;

In Viewdidload set
YourImageView.userInteractionEnabled = YES;
UITapGestureRecognizer * singleTap =
[[UITapGestureRecognizer alloc] initWithTarget:self
action:#selector(openPhotoDetail:)];
// Instead of self ,give your imageView to add gesture event
[YourImageView addGestureRecognizer: singleTap];

Click on the UIImageView, on storyboard,
Go to Attributes Inspector (The button that looks like a tuxedo, top right ,Second button).
Check the attribute "User Interaction Enabled"
Or if you want to do it by code,
in
-(void)viewDidLoad {
myImageView1.userInteractionEnabled = YES;
myImageView2.userInteractionEnabled = YES;
myImageView3.userInteractionEnabled = YES;
}

UIImageView *MyImageview = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"XYZ.png"]];
UITapGestureRecognizer *TapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(openPhotoDetail)];
[MyImageview addGestureRecognizer:TapGesture];
[MyImageview setUserInteractionEnabled:YES];
Can u use it like that

Related

Click UIView Pass data and set as Navigationitem title from UIView Data in Objective c

I have Many views in firstVC and the view have 3 object if i select any one views in the view data set as navigationItem title to the secondVC.
firstVC:
//UIView set Action
UITapGestureRecognizer *singleFingerTap1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleSingleTap:)];
View1.tag = 1;
[View1 addGestureRecognizer:singleFingerTap1];
UITapGestureRecognizer *singleFingerTap2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleSingleTap:)];
view2.tag = 2;
[view2 addGestureRecognizer:singleFingerTap2];
UITapGestureRecognizer *singleFingerTap3 = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleSingleTap:)];
view3.tag = 3;
[view3 addGestureRecognizer:singleFingerTap3];
}
-(void)handleSingleTap:(UITapGestureRecognizer *)sender
{
UIView *viewClicked = sender.view;
NSLog(#"Clicked: %ld",(long)viewClicked.tag);
[self performSegueWithIdentifier:#"Segue" sender:self];
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:#"Segue"])
{
DetailsViewController *controller = (DetailsViewController *)segue.destinationViewController;
controller = [segue destinationViewController];
controller.DataValue = arrayValue;
}
}
its pass data correctly but cant set navigationItem title each and every view while clicked.
secondVC:
and another help there is 20 element (like 20 index[0] to [20]) i don't want write code for each index like below, i don't know how to wrote code in objectAtIndex:some (not 0 to 20)
-(void)viewWillAppear:(BOOL)animated
{
[self.navigationController setNavigationBarHidden:NO];
NSDictionary *dictValue = [DataValue objectAtIndex:0];
self.navigationItem.title = [dictValue valueForKey:#"code"];
// NSDictionary *dictValue1 = [DataValue objectAtIndex:1];
// self.navigationItem.title = [dictValue1 valueForKey:#"code"];
// NSDictionary *dictValue2 = [DataValue objectAtIndex:2];
// self.navigationItem.title = [dictValue2 valueForKey:#"code"];
}

UIPopovercontroller not dismissing from UILongPressGestureRecognizer

I'm trying to dismiss a UIPopoverController, it works normally.
However if the UIPopoverController is created from a UILongPressGestureRecognizer action.
It doesn't work. I have a delegate which is calling the dismissAllPopovers. Anyone have any ideas? Thanks
UILongPressGestureRecognizer *longTap = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(handleChangeColor:)];
longTap.minimumPressDuration = 0.6;
[button addGestureRecognizer:longTap];
- (void)handleChangeColor:(UILongPressGestureRecognizer *)gesture
{
ColorPicker *colorPicker = [[ColorPicker alloc] initWithNibName:nil bundle:[NSBundle mainBundle]];
colorPicker.delegate2 = self;
colorPicker.navigationItem.title = [Language get:#"Button Color"];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:colorPicker];
self.selectColorPop = [[UIPopoverController alloc] initWithContentViewController:navController];
self.selectColorPop.popoverContentSize = CGSizeMake(300, 390);
self.selectColorPop.delegate = self;
colorPicker.myPop = self.selectColorPop;
[self.selectColorPop presentPopoverFromRect:button.frame inView:scrollView permittedArrowDirections:UIPopoverArrowDirectionRight animated:YES];
[self.selectColorPop setPassthroughViews:nil];
}
- (void) dismissAllPopOvers
{
if (self.selectColorPop != nil)
{
//[self.selectColorPop.delegate popoverControllerDidDismissPopover:self.selectColorPop];
[self.selectColorPop dismissPopoverAnimated:YES];
self.selectColorPop = nil;
}
}
Under handleChangeColor:
I should
if(UIGestureRecognizerStateBegan == gesture.state) {
// Called on start of gesture, do work here
[self doSomething];
}
if(UIGestureRecognizerStateChanged == gesture.state) {
// Do repeated work here (repeats continuously) while finger is down
}
if(UIGestureRecognizerStateEnded == gesture.state) {
// Do end work here when finger is lifted
}

Dismiss keyboard when default ios mail app opens

In my app I display the MFMailComposeViewController to display default mail app. How to dismiss the keyboard which opens up in the controller?
UIWindow* keyWindow = [[UIApplication sharedApplication] keyWindow];
UIView* firstResponder = [keyWindow performSelector:#selector(firstResponder)];
[firstResponder resignFirstResponder];
I hope This Will Help You.. :)
Unfortunately it look's like even if we try to force close the keyboard using endEditing; that function takes no effect. On my device it doesn't seem like the keyboard can be dismissed.
Below would have been the answer that should have worked:
Display the MFMailComposeViewControllerwith a completion and call endEditing: on the MFMailComposeViewController's view.
MFMailComposeViewController *mailVC = [[MFMailComposeViewController alloc] init];
[self presentViewController:mailVC animated:YES completion:^{
[mailVC.view endEditing:YES];
}];
UPDATE:
Wouldn't suggest doing this but, here is a hackish way that works:
Objective-C:
MFMailComposeViewController *mailVC = [[MFMailComposeViewController alloc] init];
[self presentViewController:mailVC animated:YES completion:^{
UITextField *a = [[UITextField alloc] init];
[mailVC.view addSubview:a];
[a becomeFirstResponder];
[mailVC.view endEditing:YES];
}];
Swift:
let mailVC = MFMessageComposeViewController()
self.presentViewController(mailVC, animated: true) { () -> Void in
let a = UITextField()
mailVC.view.addSubview(a)
a.becomeFirstResponder()
mailVC.view.endEditing(true)
}
In your viewcontroller.h add:
#property (nonatomic) UITapGestureRecognizer *tapRecognizer;
Now in the .m file, add this to your ViewDidLoad function:
- (void)viewDidLoad
{
[super viewDidLoad];
//Keyboard stuff
tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleSingleTap:)];
tapRecognizer.cancelsTouchesInView = NO;
[self.view addGestureRecognizer:tapRecognizer];
}*
Also, add this function in the .m file:
- (void)handleSingleTap:(UITapGestureRecognizer *) sender
{
[self.view endEditing:YES];
}

UIPopoverController and UITapGestureRecognizer not working in ViewDidLoad

When I add a UIPopoverController in the ViewDidLoad method, it displays an empty popover view. The same thing happens when I add a UITapGestureRecognizer; it doesn't trigger the attached method. If I put the code in my ViewDidAppear it does work, but in some cases ViewDidAppear is never called, depending on where in my storyboard the view is loaded.
Here's my code:
UIViewController* popoverContent = [[UIViewController alloc] init];
UIView *popoverView = [[UIView alloc] init];
popoverView.backgroundColor = [UIColor whiteColor];
popoverContent.view = popoverView;
pickerPopup = [[UIPopoverController alloc] initWithContentViewController:popoverContent];
pickerPopup.delegate = (id)self;
[pickerPopup setPopoverContentSize:CGSizeMake(320, 240) animated:NO];
And the UITapGestureRecognizer:
tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(openPicker:)];
[_form_from addGestureRecognizer:tap];
I solved it for now using a delay in the ViewDidLoad method like so
[self performSelector:#selector(custom_init) withObject:nil afterDelay:0.1];
Still don't get why the UITapGestureRecognizer doesn't trigger. It does work if I attach it to the view, but not if I attach it to a textfield in a static cell.

How to switch viewcontrollers programmatically by swipe guesture?

I want to put the push Navigation into swipe.
i want to call this method on swipe gesture.
- (IBAction)MakeADifferenceButtonPressed:(id)sender {
MakeADifferenceViewController *makeADifferenceView = [[MakeADifferenceViewController alloc] initWithNibName:#"MakeADifferenceViewController" bundle:nil];
[self.navigationController pushViewController:makeADifferenceView animated:YES];
[makeADifferenceView release];
}
any-one please help me. i am new in iphone.
Use this code to add UISwipeGestureRecognizerDirectionLeft in your current UIView
UISwipeGestureRecognizer *swipeleft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(MakeADifferenceButtonPressed:)];
swipeleft.direction = UISwipeGestureRecognizerDirectionLeft;
[self.view addGestureRecognizer:swipeleft];

Resources