I have a customed overlay view for my UIImagePickerController.
I want to add an UITapGestureRecognizer in this overlay view in order to draw a circle and focus the camera.
But once I added the gesture, my method is never called and I don't understand why.
Here is my code:
// Overlay view
UIView *overlay = [[UIView alloc] initWithFrame:self.view.frame];
// TapGesture
UITapGestureRecognizer *stabilisateurGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(Stabiliser:)];
stabilisateurGesture.numberOfTapsRequired = 1;
[overlay addGestureRecognizer:stabilisateurGesture];
// UIImagePickerController
pickerCamera = [[UIImagePickerController alloc] init];
pickerCamera.sourceType = UIImagePickerControllerSourceTypeCamera;
pickerCamera.delegate = self;
pickerCamera.cameraFlashMode = UIImagePickerControllerCameraFlashModeOff;
pickerCamera.modalPresentationStyle = UIModalPresentationFullScreen;
pickerCamera.showsCameraControls = NO;
pickerCamera.cameraOverlayView = overlay;
And here the gesture method:
- (void)Stabiliser:(UITapGestureRecognizer *)gesture {
CGPoint point = [gesture locationInView:gesture.view];
// Create circle
UIView *cercleRouge = [[UIView alloc] initWithFrame:CGRectMake(point.x-(86/2), point.y-(86/2), 86, 86)];
cercleRouge.layer.borderColor = [[UIColor redColor] CGColor];
cercleRouge.layer.borderWidth = 4;
cercleRouge.layer.cornerRadius = cercleRouge.frame.size.height/2;
[gesture.view addSubview:cercleRouge];
}
Thank you in advance.
Related
Hello Friends Please help me,
I have added UITapGesture on UIWindow but i can't able access parent control in screen
UIRotationGestureRecognizer *rot =[[UIRotationGestureRecognizer alloc] initWithTarget:self action:#selector(handleRotation:)];
rot.cancelsTouchesInView = NO;
rot.delegate = self;
UIPinchGestureRecognizer *pinch =[[UIPinchGestureRecognizer alloc] initWithTarget:self
action:#selector(handlePinch:)];
pinch.cancelsTouchesInView = NO;
pinch.delegate = self;
/* UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self
action:#selector(handlePan:)];
pan.cancelsTouchesInView = NO;
pan.delegate = self;*/
//circle gesture
gr = [[MDCircleGestureRecognizer alloc] init];
[gr addTarget:self action:#selector(handleGesture:)];
//[self.view addGestureRecognizer:gr];
//gr.delegate = (id<MDCircleGestureFailureDelegate>)self.view;
gr.delegate = self;
gr.cancelsTouchesInView = NO;
////Z gesture
cg = [[CharGesture alloc] initWithTarget:self action:#selector(zLetterMade:)];
cg.delegate=self;
cg.cancelsTouchesInView = NO;
//Add on window - RR
[[[UIApplication sharedApplication]delegate]window].gestureRecognizers = #[gr,cg,pinch,rot];
please confirm that you have given UIGestureRecognizerDelegate and enable interaction [[[UIApplication sharedApplication]delegate]window].userInteractionEnabled=YES;
I have this piece of code, which is in init.
This should be some swipe cell, which is created by UIScrollView and there is some another view (my own action view) below cell.
My problem: if I want swipe my cell, UIScrollView doesnt call delegate methods :-/
Any suggestions? thx
self.cellState = kCellStateClosed;
self.cellScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, kSWIPECELL_WIDTH, kSWIPECELL_HEIGHT)];
self.cellScrollView.delegate = self;
self.cellScrollView.showsHorizontalScrollIndicator = NO;
self.cellScrollView.scrollsToTop = NO;
self.cellScrollView.scrollEnabled = YES;
self.tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(selectCell)];
self.longPressGestureRecognizer = [[PAPLongPressGestureRecognizer alloc] initWithTarget:self action:#selector(scrollViewPressed:)];
self.longPressGestureRecognizer.minimumPressDuration = 0.1;
[self.cellScrollView addGestureRecognizer:self.tapGestureRecognizer];
[self.cellScrollView addGestureRecognizer:self.longPressGestureRecognizer];
// Create the content view that will live in our scroll view
self.scrollViewContentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kSWIPECELL_WIDTH, kSWIPECELL_HEIGHT)];
self.scrollViewContentView.backgroundColor = [UIColor whiteColor];
[self.cellScrollView addSubview:self.scrollViewContentView];
// Add the cell scroll view to the cell
UIView *contentViewParent = [self.subviews firstObject];
NSArray *cellSubviews = [contentViewParent subviews];
[self insertSubview:self.cellScrollView atIndex:0];
for (UIView *subview in cellSubviews)
{
[self.scrollViewContentView addSubview:subview];
}
self.containingTableView.directionalLockEnabled = YES;
//alloc action view
self.actionView = [[PAPActionCellView alloc] initWithFrame:CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height) andPages:[self createActionPages] parentCell:self];
[self.cellScrollView insertSubview:self.actionView belowSubview:self.scrollViewContentView];
What you are missing is to set the contentSize of your UIScrollView, when you set it to some bigger value than its bounds, it will start to scroll, i.e:
self.cellScrollView.contentSize = CGSizeMake(2* kSWIPECELL_WIDTH, kSWIPECELL_HEIGHT);
I have 2 UIPickerViews as InputViews for textfields. I want to detect tap events on both of them. But only the taps on the first are detected.
Here is my code in ViewDidLoad:
// for Behandler selection
_behandlerArray = [self fetchBehandler];
_behandlerPickerView = [[UIPickerView alloc] init];
_behandlerTextField.inputView = _behandlerPickerView;
_behandlerPickerView.dataSource = self;
_behandlerPickerView.delegate = self;
[_behandlerPickerView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(behandlerPickerTapped:)]];
// for Medikamente selection
_medikamenteArray = [self fetchMedikamente];
_medikamentePickerView = [[UIPickerView alloc] init];
_medikamenteTextField.inputView = _medikamentePickerView;
_medikamentePickerView.dataSource = self;
_medikamentePickerView.delegate = self;
[_medikamentePickerView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(medikamentePickerTapped:)]];
And here are the methods:
-(void)behandlerPickerTapped:(UIGestureRecognizer *)gestureRecognizer{
NSLog(#"_behandlerPickerView tapped");
}
-(void)medikamentePickerTapped:(UIGestureRecognizer *)gestureRecognizer{
NSLog(#"_medikamentePickerView tapped");
}
But only the taps on _behandlerPickerView are detected/logged.
Can I only added one UITapGestureRecognizer?
And if so, how do I know in the method, which view was tapped?
I am trying to add a tap gesture from a subclassed UIImageView and then control the tap from the View Controller. I am not getting any compiling errors but "addSubview" is not displaying any image. How can make the UIImageView to be displayed?
If I try to control the tap and pan gestures from the subclassed UIImageVIew I have no problems but I would like to control these functions from the View Controller
Relevant code looks like this.
UIImageView subclass
- (id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self)
{
//self.userInteractionEnabled = YES;
previewController = [[PreviewController alloc]init];
[previewController self];
[self addSubview:character];
// Tap Initialization code
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:(PreviewController *)self.previewController action:#selector(addCharacter:)];
[self addGestureRecognizer:tap];
self.userInteractionEnabled = YES;
}
return self;
}
View Controller
- (void)addCharacter:(UITapGestureRecognizer *)t
{
NSLog(#"add character");
imageNSArray = [NSMutableArray array];
uiImg = [UIImage imageNamed:#"homer.png"];
CGPoint loc = [t locationInView:self.view];
character = [[UIImageView alloc] initWithImage:uiImg];
character.center = loc;
[imageNSArray addObject:character];
//Locate the imageNSArray on frameImageView area only.
[self.view addSubview:character];
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(panCharacter:)];
[self.character addGestureRecognizer:pan];
character.userInteractionEnabled = YES;
}
put a view behind your subclassed imageView. Apply the gestureRecognition to the new View.
You can control your tap gesture from new view. Let me know if i am not clear, or if more info needed.
I think what you want is.. .you want to show a image when user taps on screen.
Do following steps:
1. drag & drop a tap gesture recognizer on default view of your view controller.
2. connect (ctrl +) gestureRecognizer with ViewController.m
Take a look at this code. (Just modified your code)
- (IBAction)onTap:(UITapGestureRecognizer *)sender {
NSLog(#"add character");
UIImage * uiImg = [UIImage imageNamed:#"sel.png"];
CGPoint loc = [sender locationInView:self.view];
UIImageView * character = [[UIImageView alloc] initWithImage:uiImg];
character.center = loc;
[self.view addSubview:character];
}
Let me know if this is not what you want…
Edit
better go for this code… No need to do above steps.
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(onTap:)];
[self.view addGestureRecognizer:tap];
self.view.userInteractionEnabled = YES;
}
- (void)onTap:(UITapGestureRecognizer *)sender
{
NSLog(#"add character");
UIImage * uiImg = [UIImage imageNamed:#"sel.png"];
CGPoint loc = [sender locationInView:self.view];
UIImageView * character = [[UIImageView alloc] initWithImage:uiImg];
character.center = loc;
[self.view addSubview:character];
}
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];