gpt4 book ai didi

ios - MKMapView 不响应触摸事件或手势识别器

转载 作者:行者123 更新时间:2023-11-29 00:56:19 25 4
gpt4 key购买 nike

编辑:

发布此问题后,我立即发现我错过了注释掉的一行代码。就是这个……mapViewController.view.translatesAutoresizingMaskIntoConstraints = NO;

我把它注释掉了, map 又能用了。

您知道为什么这行代码会杀死地​​图手势识别器吗?


我有一个 MKMapView,最近几个月工作得很好,今天就死机了。

我今天做的唯一一件事就是搞乱代码,就是在 viewWillTransitionToSize:withTransitionCoordinator:viewWillLayoutSubviews 中使用一些方向代码,就是这样!我注释掉了我在那里所做的一切,但它仍然是死的。

它不仅不会响应我自己的手势识别器,也不会响应它自己的任何识别器……捏合、旋转等。它只是卡住了。

我在 map 顶部也没有会阻碍触摸的 View 。

我已经控制清理了我的构建文件夹。

我已经关闭了 XCode 项目并清除了该项目的派生数据文件夹。

@implementation MapViewController
{
MKMapView *appleMapView;
}



-(void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.

[self addMapViewToSelf];
}


-(void)addMapViewToSelf
{
// Create and add the apple map view to the side menu view.
appleMapView = [[MKMapView alloc] initWithFrame:self.view.frame];
[self.view addSubview:appleMapView];

appleMapView.delegate = self;

UILongPressGestureRecognizer *tapRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(windowTapped:)];
tapRecognizer.minimumPressDuration = 1.0f;
tapRecognizer.delegate = self;
[appleMapView addGestureRecognizer:tapRecognizer];
}


-(void)windowTapped:(UIGestureRecognizer *)recognizer
{
NSLog(@"The recognizer state is '%ld'", (long)recognizer.state);

if (recognizer.state != UIGestureRecognizerStateBegan) {
return;
}

}

最佳答案

AutoResizingMask 是边界更改时 UI 元素(例如标签)移动的属性。意味着当方向改变时,应用程序会适应并且不会崩溃。但是,如果您将 autoResizingMask 设置为 Constraint,那么您就是在告诉它根据 autoResizingMask 更改约束。这是每个 UI 元素的默认行为。这有助于我们使用 self.view.centre 或 self.view.frame 或 self.label.size.width 等方法快速动态地排列 UI 元素。

设置translatesAutoresizingMaskIntoConstraints = NO,您实际上正在删除此行为。因此,您必须提供 UI 元素固定坐标,以便无论屏幕方向如何,UI 元素都保持在那里。但你并没有这样做。因此 UIElement 没有约束或固定位置,并且行为不稳定。

如果您不想根据 UI 的位置或大小动态播放,则不得将其设置为 NO。就像基于输入的增长文本字段或一些原始动画。

苹果公司试图告诉你同样的事情。

When a view’s bounds change, that view automatically resizes its subviews according to each subview’s autoresizing mask. You specify the value of this mask by combining the constants described in UIViewAutoresizing using the C bitwise OR operator. Combining these constants lets you specify which dimensions of the view should grow or shrink relative to the superview. The default value of this property is UIViewAutoresizingNone, which indicates that the view should not be resized at all.

When more than one option along the same axis is set, the default behavior is to distribute the size difference proportionally among the flexible portions. The larger the flexible portion, relative to the other flexible portions, the more it is likely to grow. For example, suppose this property includes the UIViewAutoresizingFlexibleWidth and UIViewAutoresizingFlexibleRightMargin constants but does not include the UIViewAutoresizingFlexibleLeftMargin constant, thus indicating that the width of the view’s left margin is fixed but that the view’s width and right margin may change. Thus, the view appears anchored to the left side of its superview while both the view width and the gap to the right of the view increase.

If the autoresizing behaviors do not offer the precise layout that you need for your views, you can use a custom container view and override its layoutSubviews method to position your subviews more precisely.

我犯过这样的错误一百万次,这个博客有一个很好的解决方案。

https://www.innoq.com/en/blog/ios-auto-layout-problem/

关于ios - MKMapView 不响应触摸事件或手势识别器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37599081/

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