gpt4 book ai didi

ios - 在 MKMapview 上移动 MKCircle 并拖动 MKMapview

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:06:57 26 4
gpt4 key购买 nike

我在 MKMapView 上有一个 MKCircle。它是用户可拖动的,但如果用户拖动圆圈外的区域, map 应该会移动。

- (IBAction)createPanGestureRecognizer:(id)sender
{
_mapView.scrollEnabled=NO;
_panRecognizer = [[UIPanGestureRecognizer alloc]
initWithTarget:self action:@selector(respondToPanGesture:)];
[_mapView addGestureRecognizer:_panRecognizer];
}

-(void)respondToPanGesture:(UIPanGestureRecognizer*)sender {

static CGPoint originalPoint;

if (sender.state == UIGestureRecognizerStateBegan) {
CGPoint point = [sender locationInView:_mapView];
CLLocationCoordinate2D tapCoordinate = [_mapView convertPoint:point toCoordinateFromView:_mapView];

CLLocation *tapLocation = [[CLLocation alloc] initWithLatitude:tapCoordinate.latitude longitude:tapCoordinate.longitude];

CLLocationCoordinate2D originalCoordinate = [_circle coordinate];
CLLocation *originalLocation = [[CLLocation alloc] initWithLatitude:originalCoordinate.latitude longitude:originalCoordinate.longitude];

if ([tapLocation distanceFromLocation:originalLocation] > [_circle radius]) {
_mapView.scrollEnabled=YES;
_isAllowedToMove=NO;
}
else if ([tapLocation distanceFromLocation:originalLocation] < [_circle radius]) {
originalPoint = [_mapView convertCoordinate:originalCoordinate toPointToView:sender.view];
_isAllowedToMove=YES;
}
}

if (sender.state == UIGestureRecognizerStateChanged) {
if (_isAllowedToMove)
{
CGPoint translation = [sender translationInView:sender.view];
CGPoint newPoint = CGPointMake(originalPoint.x + translation.x, originalPoint.y + translation.y);

CLLocationCoordinate2D newCoordinate = [_mapView convertPoint:newPoint toCoordinateFromView:sender.view];

MKCircle *circle2 = [MKCircle circleWithCenterCoordinate:newCoordinate radius:[_circle radius]];
[_mapView addOverlay:circle2];
[_mapView removeOverlay:_circle];
_circle = circle2;
}
}

if (sender.state == UIGestureRecognizerStateEnded || sender.state == UIGestureRecognizerStateFailed || sender.state == UIGestureRecognizerStateCancelled) {
_mapView.scrollEnabled=NO;
_isAllowedToMove=NO;
}
}

拖动圆圈效果很好,但是当尝试拖动 map 时,它会保持静止。我的假设是

_mapView.scrollEnabled=YES;

使 map 可拖动,但需要另一个拖动手势才能启动。如何在不失去移动圆圈能力的情况下实现这一点?

最佳答案

如果用户开始在圆圈外拖动 map ,则可以拖动 map (如果用户开始在圆圈内拖动,则拖动 map ),不要从一开始就禁用 scrollEnabled -- 让它保持打开状态(直到拖动开始,我们可以决定是否禁用)。

为了让 scrollEnabled 保持开启状态(即让 map 自己平移)添加我们自己的手势识别器,我们需要实现 shouldRecognizeSimultaneouslyWithGestureRecognizer 并返回 YES

更新后的 createPanGestureRecognizer: 看起来像这样:

- (IBAction)createPanGestureRecognizer:(id)sender
{
//_mapView.scrollEnabled=NO; // <-- do NOT disable scrolling here
_panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(respondToPanGesture:)];
_panRecognizer.delegate = self; // <-- to implement shouldRecognize
[_mapView addGestureRecognizer:_panRecognizer];
}

-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}

然后在我们的手势处理程序中,当手势开始时,根据平移开始的位置(圆圈外或圆圈内)启用或禁用scrollEnabled:

if ([tapLocation distanceFromLocation:originalLocation] > [_circle radius]) {
_mapView.scrollEnabled=YES;
_isAllowedToMove=NO;
}
else //if ([tapLocation distanceFromLocation:originalLocation] < [_circle radius]) {
//NOTE: It's not really necessary to check if distance is less than radius
// in the ELSE part since in the IF we checked if it's greater-than.
// If we get to the ELSE, we know distance is <= radius.
// Unless for some reason you want to handle the case where
// distance exactly equals radius differently but this is unlikely.
{
originalPoint = [_mapView convertCoordinate:originalCoordinate toPointToView:sender.view];
_mapView.scrollEnabled=NO; // <-- disable scrolling HERE
_isAllowedToMove=YES;
}

最后,在手势结束时始终重新启用 scrollEnabled(以防万一)。
当下一个手势开始时,如有必要,它将被重新禁用:

if (sender.state == UIGestureRecognizerStateEnded || sender.state == UIGestureRecognizerStateFailed || sender.state == UIGestureRecognizerStateCancelled) {
_mapView.scrollEnabled=YES; // <-- enable instead of disable
_isAllowedToMove=NO;
}

请注意,如果允许用户多次调用 createPanGestureRecognizer:,您应该将识别器的创建和添加移动到其他地方(可能是 viewDidLoad),否则会多次实例将被添加到 map 中。

或者,将按钮更改为切换按钮,以便在“移动”模式打开时从 map 中删除手势识别器,而不是创建和添加它。

关于ios - 在 MKMapview 上移动 MKCircle 并拖动 MKMapview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22441059/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com