iPhone拖放

Sim*_*mon 16 iphone drag-and-drop objective-c

试图为iPhone应用程序发生一些基本的拖放功能.

我目前尝试执行此操作的代码如下:

- (void) touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event
{
    UITouch *touch = [touches anyObject];

    CGPoint location = [touch locationInView:self];
    self.center = location;
}
Run Code Online (Sandbox Code Playgroud)

此代码具有触摸的UIView闪烁的结果,同时它跟随触摸.稍微玩了一下之后,我也注意到UIView似乎从屏幕上的0,0位置闪烁到当前触摸的位置.

我有什么想法我做错了吗?

zpa*_*ack 21

需要在superview的坐标系中指定center属性,但是您已根据子视图询问触摸事件的位置.而是根据superview的坐标来询问它们,如下所示:

- (void) touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event {
    UITouch *touch = [touches anyObject];   
    CGPoint location = [touch locationInView:self.superview]; // <--- note self.superview

    self.center = location;
}
Run Code Online (Sandbox Code Playgroud)