Ios 确定触摸位置在Imageview中
当我的手指移动到imageview所在的相同位置时,我尝试运行一些代码 我定义了图像视图的位置:Ios 确定触摸位置在Imageview中,ios,iphone,xcode,Ios,Iphone,Xcode,当我的手指移动到imageview所在的相同位置时,我尝试运行一些代码 我定义了图像视图的位置: CGRect imagepos = myImage.frame; 以及实际触摸位置的位置: UITouch *myTouch = [[touches allObjects] objectAtIndex: 0]; CGPoint currentPos = [myTouch locationInView: self.view]; 我试过使用CGPointEqualToPoint它不起作用。然后我尝试
CGRect imagepos = myImage.frame;
以及实际触摸位置的位置:
UITouch *myTouch = [[touches allObjects] objectAtIndex: 0];
CGPoint currentPos = [myTouch locationInView: self.view];
我试过使用CGPointEqualToPoint
它不起作用。然后我尝试使用CGRectContainsPoint
它也不起作用,因为当imagepos和currentpos具有相同的x坐标时,我的“一些代码”正在运行(我的手指没有移动到图像,它们只有相同的x位置。Y位置完全不同)
当我的手指(也在方法touchesmoved中)触摸/移动到/位于图像的任何位置时,我怎么说运行一些代码或方法
感谢您的回答。在您的ToucheSBegind方法中,您可以尝试以下内容:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
if([touch view] == myImgView)
{
NSLog(@"My Image was touched");
}
}
这会告诉您何时触摸ImageView。确保在imageView中将userInteractionEnabled
设置为“是” 下面试试看-
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
for (UITouch *touch in touches) {
CGPoint location = [touch locationInNode:self]; //Now you got current location of click or tap
if(location.y == YOUR_IMAGE_VIEW_POSITION_Y)
{
NSLog(@"Got the TOUCH...OOOoopsss !!!");
}
}
}
希望这有帮助。有一个函数
CGRectContainsPoint(rect, point);
如果点位于矩形内,则返回布尔值
因此,获取触摸点并使用imageView框架运行此操作,以确定触摸是否在图像视图内。谢谢您的回答,我尝试过,但它不起作用,因为我的图像是spritenodes。我得到了警告:比较不同的指针类型“UIVIEW”和“SKSpriteNode”。@Mehmet63你不认为在你的问题中提到精灵或精灵套件是个好主意吗?而不是“Imageview”这个UIKit对象。@Fogmeister是的,你是对的。那是一次失败。我正在尝试答案。谢谢