I am trying to find which image the user tapped using a gesture recognizer.
The scrollview as well as the image views are all created dynamically.
UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 100)];
scroll.pagingEnabled = YES;
int iPolaroidDimen = 200;
NSInteger numberOfViews = 10;
for (int i = 0; i < numberOfViews; i++) {
CGFloat xOrigin = i * self.view.frame.size.width;
UIImageView *polaroid = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"iconPol.png"]];
polaroid.frame = CGRectMake(xOrigin+(self.view.center.x/2)-(iPolaroidDimen/10), (self.view.center.y/2), iPolaroidDimen, iPolaroidDimen);
[scroll setShowsHorizontalScrollIndicator:NO];
polaroid.userInteractionEnabled = TRUE;
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(imageTapped:)];
singleTap.numberOfTapsRequired = 1;
singleTap.numberOfTouchesRequired = 1;
[polaroid addGestureRecognizer:singleTap];
[polaroid setUserInteractionEnabled:YES];
[polaroid setAccessibilityIdentifier:[NSString stringWithFormat:#"%#%#",#"ImageView",[NSString stringWithFormat:#"%d",i]]];
[scroll addSubview:polaroid];
The gesture recognizer
- (void)imageTapped:(UIGestureRecognizer *)gestureRecognizer{
UIAlertView *alert = [[UIAlertView alloc]initWithTitle: #"Guess what?"
message: #"An image was tapped"
delegate: self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"OK",nil];
[alert show];
}
- (void)imageTapped:(UIGestureRecognizer *)gestureRecognizer {
UIView *tappedView = gestureRecognizer.view;
}
You can give a pointer to each of the 10 images so you can access them later. You only need to add tap gesture to the top view which has all the 10 images, and then add the following example code in your method (in which I give each image a different name such as cat, cow ,etc. :]):
- (void)imageTapped:(UIGestureRecognizer *)gestureRecognizer
{
UIAlertView *alert = [[UIAlertView alloc]initWithTitle: #"Guess what?"
message: #"An image was tapped"
delegate: self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"OK",nil];
[alert show];
CGPoint point = [gestureRecognizer locationInView:self.view];
CGRect catRect = CGRectMake(self.cat.frame.origin.x, self.cat.frame.origin.y, self.cat.frame.size.width, self.cat.frame.size.height);
CGRect cowRect = CGRectMake(self.cow.frame.origin.x, self.cow.frame.origin.y, self.cow.frame.size.width, self.cow.frame.size.height);
if (CGRectContainsPoint(catRect, point)) {
NSLog(#"I am a cat!");
} else if (CGRectContainsPoint(cowRect, point)) {
NSLog(#"I am a cow!");
}
}
Related
I am new in iOS and I am facing problem to add UIscrollview on UIImage in UIAlertView.
My code is Like this
In ViewDidLoad
newsimage.userInteractionEnabled=YES;
UILongPressGestureRecognizer *longpressgestureRecognizer = [[UILongPressGestureRecognizer alloc] init];
[longpressgestureRecognizer addTarget:self action:#selector(imgLongPressed:)];
longpressgestureRecognizer.delegate = self;
[newsimage addGestureRecognizer: longpressgestureRecognizer];
- (void) imgLongPressed:(UILongPressGestureRecognizer*)sender
{
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
CGSize size = CGSizeMake(200, 200); // set the width and height
UIImage *resizedImage = [self resizeImage:newsimage.image imageSize:size];
UIImage *wonImage = resizedImage;
imageView.contentMode=UIViewContentModeCenter;
[imageView setImage:wonImage];
alertViewpress = [[UIAlertView alloc] initWithTitle:#""
message:#""
delegate:self
cancelButtonTitle:nil
otherButtonTitles:#"OK", nil];
//check if os version is 7 or above
if (floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_6_1) {
[alertViewpress setValue:imageView forKey:#"accessoryView"];
}else{
[alertViewpress addSubview:imageView];
}
[alertViewpress show];
[self performSelector:#selector(dismiss:) withObject:alertViewpress afterDelay:1.0];
}
- (void)willPresentAlertView:(UIAlertView *)alertView {
[alertView setFrame:CGRectMake(0, 0, 200, 200)];
}
-(void)dismiss:(UIAlertView*)alert
{
[alert dismissWithClickedButtonIndex:0 animated:YES];
}
-(UIImage *)resizeImage:(UIImage *)image imageSize:(CGSize)size
{
UIGraphicsBeginImageContext(size);
[image drawInRect:CGRectMake(0,0,size.width,size.height)];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
// here is the scaled image which has been changed to the size specified
UIGraphicsEndImageContext();
return newImage;
}
How can I add Scroll view so that I can scroll image or How can I change size of UIAlertView or is it is possible to change the size of UIAlertView.Thanks in Advance!
I give you what you ask.Your view name is customView and you set frame for that.Now the code looks like below.
UILongPressGestureRecognizer *longPressOnCustomView = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(longPressOnCustomView:)];
longPressOnCustomView.delegate = self;
customView.userInteractionEnabled = YES;
[customView addGestureRecognizer:longPressOnCustomView];
#pragma mark Long Press Gesture Recognizer
- (void)longPressOnCustomView:(UILongPressGestureRecognizer*)gesture
{
if (gesture.state == UIGestureRecognizerStateEnded)
{
//Code for scrolling the image
}
}
I have an app that implements Horizontal gallery. I am able to display the image thumbnails. Now what I need to do is to enlarge the image when it is tapped. I am using EMNotificationPopup to display the enlarged image. Here's what I have so far:
This snippet is in my viewdidload:
UITapGestureRecognizer *singleTap;
UIImageView * imageview;
singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleImageTap:)];
[singleTap setNumberOfTouchesRequired:1];
[singleTap setNumberOfTapsRequired:1];
singleTap.delegate = self;
[imageview setUserInteractionEnabled:YES];
[imageview addGestureRecognizer:singleTap];
- (void) handleImageTap:(UIGestureRecognizer *)gestureRecognizer {
[self coreManagementWithPosition:EMNotificationPopupPositionCenter andType:EMNotificationPopupBig];
}
- (void) coreManagementWithPosition: (EMNotificationPopupPosition) position andType:(EMNotificationPopupType) notificationPopupType {
if (_notificationPopup.isVisible) {
[_notificationPopup dismissWithAnimation:YES];
_notificationPopup = NULL;
} else {
_notificationPopup = [[EMNotificationPopup alloc] initWithType:notificationPopupType enterDirection:EMNotificationPopupToDown exitDirection:EMNotificationPopupToLeft popupPosition:position];
_notificationPopup.delegate = self;
_notificationPopup.title = #"Sorry for this Alert message :)";
_notificationPopup.subtitle = #"Awesome message :)";
_notificationPopup.image = [UIImage imageNamed:#"alert_image"];
if (notificationPopupType == EMNotificationPopupBigButton)
_notificationPopup.actionTitle = #"OK";
[_notificationPopup show];
}
}
My issue here is handleImageTap is not being initialised/called. How can enable the user interaction for imageview.
When I click on a image I want to show another popover, but it doesn't work!
My handle Tap method looks like :
-(void)handleTapView:(UITapGestureRecognizer*)recognizer
{
CGPoint startPoint = [recognizer locationInView:recognizer.view];
NSLog(#"handle Tap VIEW!!!!!!!!");
if ([recognizer.view isKindOfClass:[UIImageView class]] ) {
NSLog(#"Tap Image!!!!!!!!");
}
else if ([self.popover isPopoverVisible]) {
[self.popover dismissPopoverAnimated:YES];
}
else {
ShapesListViewController *shapes = (ShapesListViewController*) [self.storyboard instantiateViewControllerWithIdentifier:#"ShapesListViewController"];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:shapes];
UIPopoverController *pop = [[UIPopoverController alloc] initWithContentViewController:nav];
shapes.delegate = self;
self.popover = pop;
CGRect popoverRect;
popoverRect.origin = startPoint;
popoverRect.size.width = 1;
popoverRect.size.height =1;
[pop presentPopoverFromRect:popoverRect inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}
}
In viewdidload :
ImageView *imgv = [[ImageView alloc] initWithImage:[UIImage imagNamed:#"delete.png"]];
imgv.center = CGPointMake(250,250);
[self.view addSubview:imgv];
ImageView *imgv2 = [[ImageView alloc] initWithImage:[UIImage imageNamed:#"gear.png"]];
imgv2.center = CGPointMake(400,400);
[self.view addSubview:imgv2];
//Tap Recognizer
self.singelTapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleTapView:)];
[self.singelTapGestureRecognizer setNumberOfTapsRequired:1];
[self.view addGestureRecognizer:self.singelTapGestureRecognizer];
You need to attach a separate gestureRecogniser to each view whose gestures you want to capture - in your case, both of your imageViews.
UITapGestureRecognizer *tapGR1, *tapGR2;
SEL selector = #selector(handleTapView:);
tapGR1 = [[UITapGestureRecognizer alloc] initWithTarget:self
action:selector];
tapGR2 = [[UITapGestureRecognizer alloc] initWithTarget:self
action:selector];
[imgv1 addGestureRecognizer:tapGR1];
[imgv2 addGestureRecognizer:tapGR2];
Don't attach a tapGR to their superview.
Then you also need to set userInteractionEnabled on each of the imageViews otherwise they will ignore touches (UIImageView defaults to userInteractionEnabled = NO):
imgv1.userInteractionEnabled = YES;
imgv2.userInteractionEnabled = YES;
In you handleTapView you need to reorganise slightly. Change the else if in you conditional sequence to if otherwise the third clause will never get triggered.
The recognizer.view for each of the recongnizers will correctly identify the imageView that was tapped. That will be the rect that the popover should present from in the coordinates of the imageView's superview - it's frame property.
So:
[pop presentPopoverFromRect:recognizer.view.frame
inView:self.view
permittedArrowDirections:UIPopoverArrowDirectionAny
animated:YES];
In my app I have a tapGesture, panGesture, rotationGesture and pinchGesture.
The tapGesture is the starting point for all gestures, it shows me among other things which subview is selected.
After I entered a button to handle ImagePicker, the subview is still selected and therefore it is still handling gestures.
My question: Is there any statement to stop handling gestures?
EDIT
I don't need the gestureRecognizer, therefore I put them inactive:
panRecognizer.enabled = NO;
pinchRecognizer.enabled = NO;
rotationRecognizer.enabled = NO;
So if I need them I want to get them work when I am handling a tapRecognizer,
but here the recognizer do not move from inactive to active.
[panRecognizer isEnabled];
pinchRecognizer.enabled = YES;
rotationRecognizer.enabled = YES;
EDIT
My view is a ViewController and the subviews are on the imageView.
The recognizers are assigned to self.imageView.
In the first method I disenable the recognizers and in the second method I enable them
- (IBAction)photo: (id) sender {
panRecognizer.enabled = NO;
pinchRecognizer.enabled = NO;
rotationRecognizer.enabled = NO;
UIImagePickerController* picker = [[UIImagePickerController alloc]init];
picker.delegate = self;
picker.allowsEditing = NO;
#try {
picker.sourceType=UIImagePickerControllerSourceTypeCamera;
[self presentModalViewController:picker animated:YES];
}
#catch (NSException * e) {
UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:#"Error" message:#"Camera is not available"
delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil] autorelease];
[alert show];
}
[picker release];
}
- (IBAction)oneTap: (UIGestureRecognizer*)gestureRecognizer {
NSLog(#"oneTap");
float differenz = 2000;
[panRecognizer isEnabled];
pinchRecognizer.enabled = YES;
rotationRecognizer.enabled = YES;
for (UIView* const subview in array) {
subview.opaque = YES;
CGPoint const point = [gestureRecognizer locationInView:self.imageView];
float zwischenS = sqrt(powf(point.x - subview.frame.origin.x,2)) + sqrt(powf(point.y - subview.frame.origin.y,2));
if (differenz > zwischenS ) {
differenz = sqrt(powf(point.x - subview.frame.origin.x,2)) + sqrt(powf(point.y - subview.frame.origin.y,2));
newView.layer.borderColor = [[UIColor clearColor]CGColor];
newView = subview;
subview.layer.borderColor = [[UIColor whiteColor]CGColor];
subview.layer.borderWidth = 3.0f;
[imageView bringSubviewToFront: subview];
}
}
}
What is my mistake?
Thanks in advance
property [UIGestureRecognizer enabled]
//YOU CREATE GESTURES LIKE THIS
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc]
initWithTarget:self action:#selector(handlePanGesture:)];
//YOU ADD THEM LIKE THIS
[self.view addGestureRecognizer:panGesture];
//AND YOU REMOVE THEM LIKE THIS
[self.view removeGestureRecognizer:panGesture];
I'm not sure how you are coding things, but I hope this gives you an idea
How to make the UIImagePickerController size bigger in iPAD? Example MAC PhotoBoth application. http://www.youtube.com/watch?v=Ytl3EhNCP_8.
The code already i have done.
- (void)openCamera {
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
camera = [[UIImagePickerController alloc] init];
camera.sourceType = UIImagePickerControllerSourceTypeCamera;
camera.cameraDevice = UIImagePickerControllerCameraDeviceFront;
camera.delegate = self;
[self presentViewController:camera sender:nil animated:YES modal:YES];
}
else
{
UIAlertView *objAlert = [[UIAlertView alloc]initWithTitle:#"Alert" message:#"Camera not found" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[objAlert show];
[objAlert release];
}
}
- (void)presentViewController:(UIViewController *)vc
sender:(id)sender
animated:(BOOL)animated
modal:(BOOL)modal {
CGRect cframe = self.capturedImage.bounds;
//cframe = CGRectMake(cframe.origin.x, cframe.origin.y/2.0, 351, 351);
photoPopoverController = [[UIPopoverController alloc] initWithContentViewController:vc];
[photoPopoverController presentPopoverFromRect:[capturedImage bounds] inView:capturedImage permittedArrowDirections:UIPopoverArrowDirectionUnknown animated:YES];
}
You can use transform:
//The transform factor
#define CAMERA_TRANSFORM 0.34 and above
and above, when you want to resize it, use:
camera.wantsFullScreenLayout = YES;
camera.cameraViewTransform = CGAffineTransformScale(camera.cameraViewTransform,CAMERA_TRANSFORM, CAMERA_TRANSFORM);
You should change the frame property of its view.
I.e.
UIView *view = imagePickerController.view;
CGRect frame = view.frame;
frame.size.height = new_height;
frame.size.width = new_width;
view.frame = frame;
It should work. Otherwise, please, give us more detail about how you create the controller, how you display it and which are its delegates.