触摸对象时移动对象

时间:2014-04-13 14:56:41

标签: ios objective-c

如果您触摸特定物体并移动手指,则物体会跟随您的手指。当您触摸屏幕但对象时,则没有任何反应。

这是我的代码:

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *myTouch = [touches anyObject];
    CGPoint myPoint = [myTouch locationInView:self.view];
    _plaatje.center = CGPointMake(myPoint.x, myPoint.y);
}

我怎样才能做到这一点?

1 个答案:

答案 0 :(得分:0)

假设_plaatjeSKNode

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
CGPoint mytouch = [[[event allTouches] anyObject]locationInView:self.view];
if((myTouch.x>_plaatje.center.x-(_plaatje.frame.size.width)/2)&&(myTouch.x<_plaatje.center.x+(_plaatje.frame.size.width)/2)&&(myTouch.y>_plaatje.center.y-(_plaatje.size.frame.height)/2)&&(myTouch.y<_plaatje.center.y+(_plaatje.frame.size.height)/2))
{
_plaatje.center = [mytouch locationInView:self.view];
}
}