Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/109.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 具有相同定位点的两个UIView_Iphone_Ios_Uiview_Calayer - Fatal编程技术网

Iphone 具有相同定位点的两个UIView

Iphone 具有相同定位点的两个UIView,iphone,ios,uiview,calayer,Iphone,Ios,Uiview,Calayer,我一直在尝试下面的代码,将两个UIView与anchorPoint(1,0)相交 这段代码的输出如下所示 我看到的输出像红色视图和绿色视图重叠,它们都有相同的定位点,如下所示 您的意思是,您希望红色视图的大小是绿色视图和原始红色视图之间重叠区域的大小吗 如果是, v.frame = CGRectIntersection(v.frame, v2.frame); 您的意思是,您希望红色视图的大小与绿色视图和原始红色视图之间的重叠区域的大小相同吗 如果是, v.frame = CGRectInte

我一直在尝试下面的代码,将两个UIView与anchorPoint(1,0)相交

这段代码的输出如下所示

我看到的输出像红色视图和绿色视图重叠,它们都有相同的定位点,如下所示


您的意思是,您希望红色视图的大小是绿色视图和原始红色视图之间重叠区域的大小吗

如果是,

v.frame = CGRectIntersection(v.frame, v2.frame);

您的意思是,您希望红色视图的大小与绿色视图和原始红色视图之间的重叠区域的大小相同吗

如果是,

v.frame = CGRectIntersection(v.frame, v2.frame);

请检查有关
锚点和坐标系的原始文档

图2三个锚点值:

更新#1:


(此图显示OSX的坐标系!)

请查看有关
主播点和坐标系的原始文档

图2三个锚点值:

更新#1:


(此图显示了OSX的坐标系!)

这可能有助于描述示例中发生的情况:

您正在屏幕上放置两个大小精确的矩形,位置精确
(50,50)
(相对于它们左上角的原点) 它们都有默认位于其中心的锚点。 想象一下在这些中心点上放置一个销。 现在你说你想要的是右上角的别针,而不是中间的

两件事中的一件也会发生

1) 矩形将保持静止,插针将自行移动到右上角
红色:(150,50)
绿色:(250,50)
,这不是您想要的

2) 引脚将保持静止,矩形将自行移动,直到其右上角的引脚位于
红色引脚:(100100)
绿色引脚:(150200)
,这也不是您想要的

第二种选择是实际发生的情况

根据文档,
层.位置
是相对于
层.锚点
,因此您应该尝试将
锚点
设置到右上角,然后将两个矩形层的
位置
设置到您想要的准确位置。e、 g

UIView *v=[[UIView alloc] initWithFrame:CGRectMake(50,50, 100, 100)];
v.backgroundColor=[UIColor redColor];
v.layer.anchorPoint=CGPointMake(1,0);
v.layer.position = CGPointMake(200, 50);

UIView *v2=[[UIView alloc] initWithFrame:CGRectMake(50,50,200,300)];
v2.backgroundColor=[UIColor greenColor];
v2.layer.anchorPoint=CGPointMake(1, 0);
v2.layer.position = CGPointMake(200, 50);

[self.view addSubview:v2];
[self.view addSubview:v];
只是为了好玩:下面是一个动画,显示矩形如何从
initWithFrame
中的初始位置到其最终位置,相对于其中心的锚定点

UIView *v=[[UIView alloc] initWithFrame:CGRectMake(50,50, 100, 100)];
v.backgroundColor=[UIColor redColor];

UIView *v2=[[UIView alloc] initWithFrame:CGRectMake(50,50,200,300)];
v2.backgroundColor=[UIColor greenColor];

[self.view addSubview:v2];
[self.view addSubview:v];   

UIView *ap=[[UIView alloc] initWithFrame:CGRectMake(0,0, 10, 10)];
ap.backgroundColor=[UIColor blackColor];
ap.center = v.layer.position;

UIView *ap2=[[UIView alloc] initWithFrame:CGRectMake(0,0, 10, 10)];
ap2.backgroundColor=[UIColor blackColor];
ap2.center = v2.layer.position;

[self.view addSubview:ap]; 
[self.view addSubview:ap2];   

CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"anchorPoint"];
animation.duration = 2.0;
animation.toValue = [NSValue valueWithCGPoint:CGPointMake(1,0)];
animation.autoreverses = YES;
animation.repeatCount = 10;
[v.layer addAnimation:animation forKey:@"anchorPoint"];
[v2.layer addAnimation:animation forKey:@"anchorPoint"];

这可能有助于描述您的示例中发生的情况:

您正在屏幕上放置两个大小精确的矩形,位置精确
(50,50)
(相对于它们左上角的原点) 它们都有默认位于其中心的锚点。 想象一下在这些中心点上放置一个销。 现在你说你想要的是右上角的别针,而不是中间的

两件事中的一件也会发生

1) 矩形将保持静止,插针将自行移动到右上角
红色:(150,50)
绿色:(250,50)
,这不是您想要的

2) 引脚将保持静止,矩形将自行移动,直到其右上角的引脚位于
红色引脚:(100100)
绿色引脚:(150200)
,这也不是您想要的

第二种选择是实际发生的情况

根据文档,
层.位置
是相对于
层.锚点
,因此您应该尝试将
锚点
设置到右上角,然后将两个矩形层的
位置
设置到您想要的准确位置。e、 g

UIView *v=[[UIView alloc] initWithFrame:CGRectMake(50,50, 100, 100)];
v.backgroundColor=[UIColor redColor];
v.layer.anchorPoint=CGPointMake(1,0);
v.layer.position = CGPointMake(200, 50);

UIView *v2=[[UIView alloc] initWithFrame:CGRectMake(50,50,200,300)];
v2.backgroundColor=[UIColor greenColor];
v2.layer.anchorPoint=CGPointMake(1, 0);
v2.layer.position = CGPointMake(200, 50);

[self.view addSubview:v2];
[self.view addSubview:v];
只是为了好玩:下面是一个动画,显示矩形如何从
initWithFrame
中的初始位置到其最终位置,相对于其中心的锚定点

UIView *v=[[UIView alloc] initWithFrame:CGRectMake(50,50, 100, 100)];
v.backgroundColor=[UIColor redColor];

UIView *v2=[[UIView alloc] initWithFrame:CGRectMake(50,50,200,300)];
v2.backgroundColor=[UIColor greenColor];

[self.view addSubview:v2];
[self.view addSubview:v];   

UIView *ap=[[UIView alloc] initWithFrame:CGRectMake(0,0, 10, 10)];
ap.backgroundColor=[UIColor blackColor];
ap.center = v.layer.position;

UIView *ap2=[[UIView alloc] initWithFrame:CGRectMake(0,0, 10, 10)];
ap2.backgroundColor=[UIColor blackColor];
ap2.center = v2.layer.position;

[self.view addSubview:ap]; 
[self.view addSubview:ap2];   

CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"anchorPoint"];
animation.duration = 2.0;
animation.toValue = [NSValue valueWithCGPoint:CGPointMake(1,0)];
animation.autoreverses = YES;
animation.repeatCount = 10;
[v.layer addAnimation:animation forKey:@"anchorPoint"];
[v2.layer addAnimation:animation forKey:@"anchorPoint"];

我不想检查它是否相交。红色图像必须与彩色图像相交,锚定点(1,0)对不起,安迪波尔,你必须更清楚你想要什么。CGRectInsersectsRect返回相交区域的CGRect。很抱歉,您错了,请转到此链接,CGRectInsersectsRect返回boolMy bad,输入时未打开Xcode。仍然没有花很长时间找到我所指的函数CGRECTINSERSECTION。我仍然不知道这是否是你想要的。谢谢你的更正。黑色的点,代表锚定点。所以,我看的是绿色和红色视图必须有相同的定位点。我不想检查它是否相交。红色图像必须与彩色图像相交,锚定点(1,0)对不起,安迪波尔,你必须更清楚你想要什么。CGRectInsersectsRect返回相交区域的CGRect。很抱歉,您错了,请转到此链接,CGRectInsersectsRect返回boolMy bad,输入时未打开Xcode。仍然没有花很长时间找到我所指的函数CGRECTINSERSECTION。我仍然不知道这是否是你想要的。谢谢你的更正。黑色的点,代表锚定点。因此,我要看的是绿色和红色视图必须具有相同的定位点。上面代码中的
值的输出是否正确?这张照片上的
P(50.f,50.f)
在哪里?如果注释掉
主播点
行,结果会是什么?上述代码中
值的输出是否正确?这张照片上的
P(50.f,50.f)
在哪里?如果您注释掉
主播点
行,结果会是什么?添加动画以说明:-)添加动画以说明:-)