ios - 检测对标注标题的点击
<p><p>如何检测对注释标注的<strong>标题</strong>的点击?我已经有一个右侧标注附件和一个左侧附件,但我想检测用户是否点击了标题(位于标注的中心)。</p>
<p>如果这是不可能的,我如何在点击标题时禁用隐藏标注?</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>回答你的问题有点晚了,但我最近正在处理同样的问题,我自己通过反复试验来解决我的问题。
也许我可以帮助处理同样问题的人。</p>
<p>您还可以使用一些自定义注释和标注 View 类,那里有很多示例。
但是,还有另一种简单的方法可以解决这个问题,而无需使用复杂的外部类。</p>
<p>问题定义:
我希望能够通过在标注 View 中触摸来触发某些方法。
我不想使用右或左附件按钮。
但是当我触摸标注 View 时, View 会立即消失。</p>
<p>解决方案的简短说明:
我们只需使用自定义的“MKPinAnnotationView”和“hittest”来检测标注 View 中的触摸。</p>
<p>你也可以在文末找到github项目链接。</p>
<p>touchableCallOutsViewController.h</p>
<pre><code>//
//touchableCallOutsViewController.h
//touchableCallOuts
//
//Created by yasin turkoglu on 20.11.2012.
//Copyright (c) 2012 yasin turkoglu. All rights reserved.
//
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
@class myCustomPinAnnotationClass;
@interface touchableCallOutsViewController : UIViewController <MKMapViewDelegate> {
MKMapView *myMapView;
int pinCounter;
myCustomPinAnnotationClass *myAnnotation;
CLLocationCoordinate2D selectedPinCoordinate;
int selectedPinNumber;
}
@property (strong, nonatomic) MKMapView *myMapView;
@property (strong, nonatomic) myCustomPinAnnotationClass *myAnnotation;
@end
</code></pre>
<p>touchableCallOutsViewController.m</p>
<pre><code>//
//touchableCallOutsViewController.m
//touchableCallOuts
//
//Created by yasin turkoglu on 20.11.2012.
//Copyright (c) 2012 yasin turkoglu. All rights reserved.
//
#import "touchableCallOutsViewController.h"
#import "myCustomPinAnnotationClass.h"
@interface touchableCallOutsViewController ()
@end
@implementation touchableCallOutsViewController
@synthesize myMapView;
@synthesize myAnnotation;
- (void)viewDidLoad
{
//first we create mapview and add pin annotation
myMapView = [initWithFrame:self.view.frame];
myMapView.delegate = self;
MKCoordinateRegion region = {{0,0},{1.0,1.0}};
region.center.latitude = 41.036651; //user defined
region.center.longitude = 28.983870;//user defined
;
;
//we define long press recognizer for 2 seconds and add our map view to add new pin when you press 2 seconds on map view
UILongPressGestureRecognizer *lngPrs = [initWithTarget:self action:@selector(handleMyLongPress:)];
lngPrs.minimumPressDuration = 2.0;
;
pinCounter = 1; //this variable hold pin numbers and increment 1 when new pin added.
myAnnotation = [initWithName: description:@"touch here to see pin coordinates" pinNum:pinCounter coordinate:region.center];
;
}
- (void)handleMyLongPress:(UIGestureRecognizer *)gestureRecognizer
{
if (gestureRecognizer.state != UIGestureRecognizerStateBegan) {
}else{
//when we press more than 2 seconds on map view UILongPressGestureRecognizer triggered this method and call our custom MKPinAnnotationView class and add annotation.
CGPoint touchPoint = ;
CLLocationCoordinate2D touchMapCoordinate = ;
pinCounter++;
myAnnotation = [initWithName: description:@"touch here to see pin coordinates" pinNum:pinCounter coordinate:touchMapCoordinate];
;
}
}
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
//when new annotation added by us, new annotation property will set here
static NSString *identifier = @"myPins";
if (]) {
myCustomPinAnnotationClass *annotationView = (myCustomPinAnnotationClass *) ;
if (annotationView == nil) {
annotationView = [ initWithAnnotation:annotation reuseIdentifier:identifier];
} else {
annotationView.annotation = annotation;
}
myAnnotation = (myCustomPinAnnotationClass *)annotation;
annotationView.pinColor = MKPinAnnotationColorGreen;
annotationView.enabled = YES;
annotationView.draggable = YES;
annotationView.canShowCallout = YES;
;
return annotationView;
}
return nil;
}
- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view
{
//if you select any annotation to show their callout sellected annotation pin number and coordinate setted and call findcallot method
myAnnotation = (myCustomPinAnnotationClass *)view.annotation;
selectedPinCoordinate = myAnnotation.coordinate;
selectedPinNumber = myAnnotation.pinNum;
;
}
- (void)findCallOut:(UIView *)node
{
//this method dig our mapview with for loop until find callout view class named "UICalloutView"
if(]){
//this method trigered together with callout open animation.
//in UICalloutView we have callout bubble image
//we dig it with for loop to find this image height
float buubleHeight = 0.0;
for(UIImageView *bubbleComponents in node.subviews){
//when we find callout bubble image take height to set our touchable area height
buubleHeight = bubbleComponents.frame.size.height;
break;
}
//this method triger with callout open animation as I said before
//ant this bouncing open animation distort actual size of callout view
//so when this happens we also get transform value of callout view and make proportion to find exact sizes.
CGFloat nodeTransformRatio = node.transform.a;
CGFloat calculatedWidth = roundf(node.frame.size.width / nodeTransformRatio);
CGFloat calculatedHeight = roundf(buubleHeight / nodeTransformRatio);
//now create a new UIView and sized according to callout view.
UIView *touchableView = [initWithFrame:CGRectMake(0.0, 0.0, calculatedWidth, calculatedHeight)];
touchableView.userInteractionEnabled = YES;
//we add new single tap gesture recognizer to triger desired method when users touching to the callout
UITapGestureRecognizer *singleTap = [ initWithTarget:self action:@selector(doSomething)];
;
//and finaly we add this newly created view in callout view to receive touches.
;
}else{
//loop self until find a "UICalloutView"
for(UIView *child in node.subviews){
;
}
}
}
- (void)doSomething
{
//this method call allert view our preseted variables when users touch callout view.
UIAlertView *alertHolder = [ initWithTitle: message: delegate:self cancelButtonTitle:nil otherButtonTitles:@"ok",nil];
;
}
- (void)didReceiveMemoryWarning
{
;
// Dispose of any resources that can be recreated.
}
@end
</code></pre>
<p>myCustomPinAnnotationClass.h</p>
<pre><code>//
//myCustomPinAnnotationClass.h
//touchableCallOuts
//
//Created by yasin turkoglu on 20.11.2012.
//Copyright (c) 2012 yasin turkoglu. All rights reserved.
//
#import <MapKit/MapKit.h>
@interface myCustomPinAnnotationClass : MKPinAnnotationView <MKAnnotation> {
NSString *_name;
NSString *_description;
int _pinNum;
CLLocationCoordinate2D _coordinate;
}
@property (copy) NSString *name;
@property (copy) NSString *description;
@property (nonatomic, readonly) int pinNum;
@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
- (id)initWithName:(NSString*)header description:(NSString*)description pinNum:(int)pinNum coordinate:(CLLocationCoordinate2D)coordinate;
@end
</code></pre>
<p>myCustomPinAnnotationClass.m</p>
<pre><code>//
//myCustomPinAnnotationClass.m
//touchableCallOuts
//
//Created by yasin turkoglu on 20.11.2012.
//Copyright (c) 2012 yasin turkoglu. All rights reserved.
//
#import "myCustomPinAnnotationClass.h"
@implementation myCustomPinAnnotationClass
@synthesize name = _name;
@synthesize description = _description;
@synthesize pinNum = _pinNum;
@synthesize coordinate = _coordinate;
- (id)initWithName:(NSString*)header description:(NSString*)description pinNum:(int)pinNum coordinate:(CLLocationCoordinate2D)coordinate
{
self = ;
if (self) {
_name = ;
_description = ;
_pinNum = pinNum;
_coordinate = coordinate;
}
return self;
}
- (void)setCoordinate:(CLLocationCoordinate2D)newCoordinate
{
_coordinate = newCoordinate;
}
- (NSString *)title {
return _name;
}
- (NSString *)subtitle {
return _description;
}
- (int)tag {
return _pinNum;
}
-(UIView*)hitTest:(CGPoint)point withEvent:(UIEvent*)event
{
//test touched point in map view
//when hit test return nil callout close immediately by default
UIView* hitView = ;
// if hittest return nil test touch point
if (hitView == nil){
//dig view to find custom touchable view lately added by us
for(UIView *firstView in self.subviews){
if(]){
for(UIView *touchableView in firstView.subviews){
if(]){ //this is our touchable view class
//define touchable area
CGRect touchableArea = CGRectMake(firstView.frame.origin.x, firstView.frame.origin.y, touchableView.frame.size.width, touchableView.frame.size.height);
//test touch point if in touchable area
if (CGRectContainsPoint(touchableArea, point)){
//if touch point is in touchable area return touchable view as a touched view
hitView = touchableView;
}
}
}
}
}
}
return hitView;
}
@end
</code></pre>
<p>在这里您可以下载完整的项目 list 和源代码<a href="https://github.com/ytur/touchableCallOutsForIOSMaps" rel="noreferrer noopener nofollow">https://github.com/ytur/touchableCallOutsForIOSMaps</a> </p></p>
<p style="font-size: 20px;">关于ios - 检测对标注标题的点击,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/8469539/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/8469539/
</a>
</p>
页:
[1]