android 3dtouch插件,标注点支持3DTouch效果

使用场景

如果希望利用iPhone的3DTouch功能,对地图上的标注点进行信息预览、快捷操作,利用该示例说明可以实现。

用到产品

核心类/接口

类接口说明版本

AMapSearchAPI- (void)AMapPOIAroundSearch:(AMapPOIAroundSearchRequest *)request;POI 周边查询接口V4.0.0版本起

核心难点

1.检查是否支持3D Touch功能

- (void)check3DTouch

{

// register for 3D Touch (if available)

if (self.traitCollection.forceTouchCapability == UIForceTouchCapabilityAvailable)

{

[self registerForPreviewingWithDelegate:(id)self sourceView:self.view];

NSLog(@"3D Touch is available! Hurra!");

}

else

{

NSLog(@"3D Touch is not available on this device. Sniff!");

}

}

2.实现3D Touch delegate

- (UIViewController *)previewingContext:(id)previewingContext viewControllerForLocation:(CGPoint)location

{

__block BOOL touchOnAnnotation = NO;

__block AMapPOI *poi = nil;

[self.annotationArray enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {

if ([obj isKindOfClass:[MAPinAnnotationView class]])

{

MAPinAnnotationView *view = (MAPinAnnotationView *)obj;

CGPoint point = [view convertPoint:location fromView:self.mapView];

if (CGRectContainsPoint(view.bounds, point))

{

touchOnAnnotation = YES;

if ([view.annotation isKindOfClass:[POIAnnotation class]])

{

POIAnnotation *annotion = (POIAnnotation *)view.annotation;

poi = annotion.poi;

}

*stop = YES;

}

}

}];

if (!touchOnAnnotation)

{

return nil;

}

// check if we're not already displaying a preview controller

if ([self.presentedViewController isKindOfClass:[POIPreviewViewController class]])

{

return nil;

}

// shallow press: return the preview controller here (peek)

self.selectedPoi = poi;

POIPreviewViewController *previewController = [[POIPreviewViewController alloc] initWithUserPoint:self.userLocation.coordinate selectedPOI:self.selectedPoi];

previewController.delegate = self;

previewController.preferredContentSize = CGSizeMake(0, 400);

return previewController;

}

- (void)previewingContext:(id)previewingContext commitViewController:(UIViewController *)viewControllerToCommit

{

// deep press: bring up the commit view controller (pop)

UIViewController *commitController = [[POIPreviewViewController alloc] initWithUserPoint:self.userLocation.coordinate selectedPOI:self.selectedPoi];

[self showViewController:commitController sender:self];

}

//是否支持3D Touch,如果支持,注册一下

func check3DTouch() {

if #available(iOS 9.0, *) {

if self.traitCollection.forceTouchCapability == UIForceTouchCapability.available {

self.registerForPreviewing(with: self, sourceView: self.view)

print("3D Touch is available! Hurra!");

}

} else {

print("3D Touch is not available on this device. Sniff!");

}

}

//3DTouch刚触发的时候弹出的模态视图

func previewingContext(_ previewingContext: UIViewControllerPreviewing, viewControllerForLocation location: CGPoint) -> UIViewController? {

var touchOnAnnotation = false

var selectPoi: AMapPOI?

//找到点击的哪个点

for (_, poiAnnotationView) in self.poiAnnotationViews.enumerated() {

if poiAnnotationView.frame.contains(location) {

touchOnAnnotation = true;

let poiAnno: POIAnnotation = poiAnnotationView.annotation as! POIAnnotation

selectPoi = poiAnno.poi

break;

}

}

//没有点击在任何一个Annotation的范围内

if touchOnAnnotation == false {

return nil

}

// check if we're not already displaying a preview controller

if self.presentedViewController != nil {

return nil

}

self.selectedPOI = selectPoi

let poiDetailVC = self.createPOIDetailVC()

poiDetailVC.isFrom3DTouchPresent = true

poiDetailVC.preferredContentSize = CGSize.init(width: 0, height: 400)

return poiDetailVC

}

//模态视图弹出后,继续按住屏幕不放,就会调用下面这句话,进入VC

func previewingContext(_ previewingContext: UIViewControllerPreviewing, commit viewControllerToCommit: UIViewController) {

let poiDetailVC = self.createPOIDetailVC()

self.show(poiDetailVC, sender: self)

}

//Preview Actions

@available(iOS 9.0, *)

lazy var previewActions: [UIPreviewActionItem] = {

func previewActionForTitle(_ title: String, index: NSInteger, style: UIPreviewActionStyle = .default) -> UIPreviewAction {

return UIPreviewAction(title: title, style: style) { previewAction, viewController in

self.showRoute(index:index)

}

}

let action1 = previewActionForTitle("步行",index:1)

let action2 = previewActionForTitle("公交",index:2)

let action3 = previewActionForTitle("驾车",index:3)

return [action1, action2, action3]

}()

@available(iOS 9.0, *)

override var previewActionItems: [UIPreviewActionItem] {

return previewActions

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值