I have created a custom annotation and a call out for my map view. I need to navigate to another view when the user clicks on call out view or he clicks to the button that added as sub view to the callout view. But both gesture recognizer and add target is not working for me in this case. The setSelected: method was invoked and the view get hidden when tap occurs in call out view.
#interface VBPunchCardAnnotation : MKAnnotationView{
UIView *calloutView;
}
- (id)initWithAnnotation:(id )annotation reuseIdentifier:(NSString *)reuseIdentifier deal:(id)punchdeal
{
self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier];
calloutView = [[UIView alloc] init];
calloutView.hidden = YES;
infoButton = [UIButton buttonWithType:UIButtonTypeInfoLight];
[calloutView addSubview:infoButton];
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(annotationTapped:)];
singleTap.numberOfTapsRequired = 1;
singleTap.delegate = self;
[calloutView addGestureRecognizer:singleTap];
[infoButton addTarget:self action:#selector(annotationTapped:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:calloutView];
return self;
}
-(void)setSelected:(BOOL)selected animated:(BOOL)animated
{
// show/hide callout and swap pin image
calloutView.hidden = !selected;
self.image = !selected ? normalPin : selectedPin;
// dispatch an event to alert app a pin has been selected
if(selected) [[NSNotificationCenter defaultCenter] postNotificationName:#"punchCardAnnotation" object:self];
}
-(void)annotationTapped:(id)sender{
[self.delegate punchCardAnnotationClickedForDeal:self.punchDeal];
}
- (UIView*)hitTest:(CGPoint)point withEvent:(UIEvent*)event
{
UIView* hitView = [super hitTest:point withEvent:event];
if ([hitView isKindOfClass:[UIButton class]]) {
}
}
Finally I got the answer. It;s here
Followed this tutorial. Really great solution.
https://github.com/nfarina/calloutview
Happy coding!!
Related
I added custom UIView with XIB. I just tried to perform button actions and tapgestures , nothing is working eventhough user interaction enabled for all elements including ContentView.
Intializing UIView
-(void)initializeSubviews {
self.backgroundColor = [UIColor clearColor];
[[[NSBundle mainBundle]loadNibNamed:#"DatesView" owner:self options:nil]firstObject];
[self addSubview:self.contentView];
// self.contentView.frame = self.bounds;
[self.fromDateButton addTarget:self action:#selector(tapOnfromDate:) forControlEvents:UIControlEventTouchUpInside];
self.fromDateButton.backgroundColor = [UIColor redColor];
}
Tap gestures
UITapGestureRecognizer *fromDateTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapOnfromDate)];
[datesView.contentView addGestureRecognizer:fromDateTapRecognizer];
[datesView.fromMonthYearLabel addGestureRecognizer:fromDateTapRecognizer];
[datesView.fromDayLabel addGestureRecognizer:fromDateTapRecognizer];
Button Action
[datesView.fromDateButton addTarget:self action:#selector(tapOnfromDate) forControlEvents:UIControlEventTouchUpInside];
UIView interface
Couple things...
1) By commenting out this line in your view subclass:
// self.contentView.frame = self.bounds;
the view ends up with a frame of 0,0. The button and labels etc are visible because the view defaults to .clipsToBounds = NO, but the objects do not receive user interaction.
2) A UITapGestureRecognizer is a single instance. If you add it to one view, then try to add it to additional views, it will only exist on the last view to which is was added.
Try it like this (be sure to un-comment the above line):
- (void)viewDidLoad {
[super viewDidLoad];
// do all the loading stuff here...
// local declaration
UITapGestureRecognizer *tapRecognizer;
// Optional --- make *sure* user interaction is enabled
[datesView.contentView setUserInteractionEnabled:YES];
[datesView.fromMonthYearLabel setUserInteractionEnabled:YES];
[datesView.fromDayLabel setUserInteractionEnabled:YES];
// new recognizer
tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapOnfromDate:)];
// add it to contentView
[datesView.contentView addGestureRecognizer:tapRecognizer];
// new recognizer
tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapOnfromDate:)];
// add it to fromMonthYearLabel
[datesView.fromMonthYearLabel addGestureRecognizer:tapRecognizer];
// new recognizer
tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapOnfromDate:)];
// add it to fromDayLabel
[datesView.fromDayLabel addGestureRecognizer:tapRecognizer];
// add target action to fromDateButton
[datesView.fromDateButton addTarget:self action:#selector(fromDateButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
}
- (void) tapOnfromDate: (UITapGestureRecognizer *)recognizer {
NSLog(#"Tapped! %#", recognizer.view);
}
- (void) fromDateButtonTapped: (id)sender {
NSLog(#"Button Tapped! %#", sender);
}
I have a mapview in my app which needs customized calloutView for every map annotations.
Therefore, I have an XIB file for this customized calloutView.
Here is my code for the map view controller
- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view {
CustomCalloutView *calloutView = (CustomCalloutView *)[[[NSBundle mainBundle] loadNibNamed:#"CustomCalloutView" owner:self options:nil] objectAtIndex:0];
[calloutView.layer setCornerRadius:10];
CGRect calloutViewFrame = calloutView.frame;
calloutViewFrame.origin = CGPointMake(-calloutViewFrame.size.width/2 + 15, -calloutViewFrame.size.height);
calloutView.frame = calloutViewFrame;
// some other code such as load images for calloutView
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleSingleTap)];
singleTap.numberOfTapsRequired = 1;
[calloutView addGestureRecognizer:singleTap];
[view addSubview:calloutView];
}
- (void)handleSingleTap{
NSLog(#"it works");
}
However, the handleSingleTap: has never been called. Instead, every tap on the calloutView will only simply dismiss the calloutView. I also tried to add a button on the calloutView, but tap on it will also cause the calloutView dismissing, rather than calling the button action.
Can anyone help?
Update:
I've tried to change the code
[view addSubview:calloutView];
to
[self.view addSubview:calloutView];
which add the customized calloutView into the main container view rather than the mapView.
Then, it works fine with the tap gesture. Therefore, I think the problem should be caused by the mapView, it seems that mapView passes all the touch event on calloutView to itself. Anyone have ideas regarding this?
OK. I worked out the solution finally. We need to have a customized class for the MKAnnotationView to solve this. Here is the code in the .m file
- (UIView*)hitTest:(CGPoint)point withEvent:(UIEvent*)event
{
UIView* hitView = [super hitTest:point withEvent:event];
if (hitView != nil)
{
[self.superview bringSubviewToFront:self];
}
return hitView;
}
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent*)event
{
CGRect rect = self.bounds;
BOOL isInside = CGRectContainsPoint(rect, point);
if(!isInside)
{
for (UIView *view in self.subviews)
{
isInside = CGRectContainsPoint(view.frame, point);
if(isInside)
break;
}
}
return isInside;
}
Hopefully this can be helpful for others.
You need to modify your tapgesture implemenation & the method called.Have a look & check whether it work or not.
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleSingleTap)];
singleTap.numberOfTapsRequired = 1;
[calloutView addGestureRecognizer:singleTap];
[view addSubview:calloutView];
- (void)handleSingleTap:(UIGestureRecognizer *)recognizer {
NSLog(#"it works");
}
In my project I would like to make a hidden image view appear when it is tapped for more than 3 seconds. I know I need to use NSTimer, but I have never created a UIImageView touch event. How can I combine the TapGestureRecognizer with NSTimer to achieve what I want to do? I am completely new to touch events in iOS, and I am just beginning to explore this. So, any help would be appreciated. Thank you!
UPDATE:
I implemented the UILongPressGestureRecognizer as below, but now, the hidden image appears even if I press somewhere outside of the image. How can I make it appear only if pressing the hidden image itself?
- (void)viewDidLoad
{
[super viewDidLoad];
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:#selector(handleLongPress:)];
longPress.numberOfTouchesRequired = 1;
longPress.minimumPressDuration = 3;
[self.view addGestureRecognizer:longPress];
}
-(void)handleLongPress:(UILongPressGestureRecognizer *)gesture
{
if (gesture.state == UIGestureRecognizerStateBegan)
{
BrokenGlass.hidden = NO;
}
}
You don't want a UITapGestureRecognizer and a timer, you want a UILongPressGestureRecognizer.
The image is appearing because you are using the gesture recognizer in the entire view.
[**self.view** addGestureRecognizer:longPress];
The gesture will not trigger on a hidden element.
Here is my solution:
#interface ViewController () <UIGestureRecognizerDelegate>
#property (nonatomic, strong) UIImageView *imageView;
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// create the imageView
_imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
// enable the user interaction in the imageView (otherwise it will not receive events)
_imageView.userInteractionEnabled = YES;
// add as a subview of the main view
[self.view addSubview:_imageView];
// create the gesture recognizer
UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(longPressHandler:)];
longPressGesture.delegate = self;
longPressGesture.minimumPressDuration = 3;
// add the gesture to the imageView
[_imageView addGestureRecognizer:longPressGesture];
}
#pragma mark - UIGestureRecognizerDelegate
- (void)longPressHandler:(UILongPressGestureRecognizer *)gestureRecognizer {
// show the image
_imageView.image = [UIImage imageNamed:#"cat.jpeg"];
}
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];
}
I have created a UIViewController, which contains a UIView object.
I want the UIView object response to a single tap by 1 finger, and also pinch by 2 fingers.
I have clicked the "User Interaction Enabled" and the "Multiple touch" options in the xib.
I create both UITapGestureRecognizer, and UIPinchGestureRecognizer inside the UIView function initWithFrame:, as it is the only entry point for the UIView object. But the object doesn't response to the UIGestureRecognizer objects. Why? Or, where is the best place to put the UIGestureRecognizer objects?
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
// single tap
UITapGestureRecognizer* singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleSingleTap:)];
singleTap.numberOfTapsRequired = 1;
singleTap.numberOfTouchesRequired = 1;
[self addGestureRecognizer: singleTap];
// 2 fingers pinch
UIPinchGestureRecognizer* doubleMove = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:#selector(handleDoubleMove:)];
[self addGestureRecognizer: doubleMove];
}
return self;
}
Your problem is that when you instantiate your custom view subclass in IB, initWithCoder: is called. If you had done it programmatically using initWithFrame: it would have worked properly.
Two ways you can fix this,
Move the gestures setup to a different method and call them in both initWithFrame: and initWithCoder:. I would recommend doing this if you intend to reuse this component and expose some kind of callbacks on gestures.
If you want to implement this once and have a lot of interacting with the controller element, add them in viewDidLoad.
I tried your code and it works for me.
Where are you setting the view? Maybe it has any other view in front, or its superview has userInteractionEnabled disabled.
I created a UIView subclass and added this code:
-(void) handleSingleTap:(UITapGestureRecognizer *)gr {
NSLog(#"handleSingleTap");
}
-(void) handleDoubleMove:(UIPinchGestureRecognizer *)gr {
NSLog(#"handleDoubleMove");
}
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
// single tap
UITapGestureRecognizer* singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleSingleTap:)];
singleTap.numberOfTapsRequired = 1;
singleTap.numberOfTouchesRequired = 1;
[self addGestureRecognizer: singleTap];
// 2 fingers pinch
UIPinchGestureRecognizer* doubleMove = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:#selector(handleDoubleMove:)];
[self addGestureRecognizer: doubleMove];
}
return self;
}
Then, in my controller:
-(void) viewDidLoad {
[super viewDidLoad];
MyView * myView = [[MyView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
myView.backgroundColor = [UIColor greenColor];
[self.view addSubview:myView];
[myView release];
}
And it works.
i think if you are working with .xib, initialization can also be done in
-(void)awakeFormNib;
Adding the GestureRecognizers is typically done in the viewDidLoad method of the UIViewController.