多个物体开始接触?
我试图在 TouchsBegan 方法(2 UIImageViews)中拥有多个对象,
我正在使用下面的代码,但不起作用。没有错误,但位置混乱。我该怎么办?
- (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event
{
UITouch *touch = [[event allTouches] anyObject];
if (image1.image == [UIImage imageNamed:@"ball.png"]){
CGPoint location = [touch locationInView:touch.view];
image1.center = location;
}
if (image1.image == [UIImage imageNamed:@"ball2.png"]){
CGPoint location = [touch locationInView:touch.view];
image2.center = location;
}
}
I am trying to have multiple objects in a touchesbegan method (2 UIImageViews)
I am using the below code but isn't working. No errors but the location is just messed up. What should I do instead?
- (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event
{
UITouch *touch = [[event allTouches] anyObject];
if (image1.image == [UIImage imageNamed:@"ball.png"]){
CGPoint location = [touch locationInView:touch.view];
image1.center = location;
}
if (image1.image == [UIImage imageNamed:@"ball2.png"]){
CGPoint location = [touch locationInView:touch.view];
image2.center = location;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您想识别 Touchsbegen 中的两个图像视图,请尝试此
希望有帮助
If you want to identify both image view in touchesbegen try this
Hope this help
我猜,如果您需要重叠
image1
,第二个if
条件中的image1.image
需要image2.image
> 和image2
中心。但如果您需要移动图像,则必须执行以下操作 -例如:如果
image1
中心位于(x1,y1)并且image2
中心位于(x2,y2)。现在触摸点 (x3, y3) 同时属于图像image1
和image2
。如果新拖动的位置为 (x4,y4),则沿x,y
方向的拖动量分别为x4-x3
和y4-y3
。将此拖动量添加到图像的中心,以使图像出现在新位置。伪代码
下载iPhone Game Dev第3章的源代码并查看图像如何移动。
I guess,
image1.image
in your secondif
condition needs toimage2.image
if you need to over lapimage1
andimage2
center. But if you need to move the images you have to do the follow -Ex: If
image1
center is at (x1, y1) andimage2
center is at (x2, y2). Now the touch point (x3, y3) belongs both to imageimage1
andimage2
. If the new dragged is at (x4,y4), the drag amount isx4-x3
andy4-y3
alongx,y
directions respectively. Add this drag amount to both the image's center for the image to appear at the new location.Pseudo Code
Download the source code of iPhone Game Dev chapter 3 and see how images are being moved.