0

我试图弄清楚如何首先选择一个对象,然后我才能移动它。例如,我想先通过触摸它来选择牛,然后我可以移动它。原因是当我触摸屏幕时,它正在移动牛和牛1。当我一次只想移动一头牛时。任何帮助将不胜感激。

-(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {

    UITouch *touch = [touches anyObject];

    CGPoint touchLocation = [touch locationInView:touch.view];

    cow.center = CGPointMake(touchLocation.x, touchLocation.y);

    cow1.center = CGPointMake(touchLocation.x, touchLocation.y);

}
4

3 回答 3

0

尝试使用 UIPanGestureRecognizer,如下所示:

@synthesize myImage; //UIImageView

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self startMoveImage];
}

-(void) startMoveImage{
    UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
    [self.view addGestureRecognizer:panGestureRecognizer];
}

- (void)pan:(UIPanGestureRecognizer *)gesture
{
    if ((gesture.state == UIGestureRecognizerStateChanged) ||
        (gesture.state == UIGestureRecognizerStateEnded)) {

        CGPoint position = [gesture locationInView:[myImage superview]];
        [myImage setCenter:position];
    }
}
于 2012-09-10T01:48:32.383 回答
0

有很多方法可以实现这种事情。这取决于您的图形是如何实现的,除其他外,请查看此 Apple 示例代码:

http://developer.apple.com/library/ios/#samplecode/Touches/Introduction/Intro.html

于 2012-09-10T01:39:30.580 回答
0

标准解决方案是为您的两个奶牛对象添加单独的手势识别器,例如,在viewDidLoad

UIPanGestureRecognizer *panCow1 = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(moveCow:)];
[cow1 addGestureRecognizer:panCow1];
// if non ARC, make sure to add a line that says
// [panCow1 release];

UIPanGestureRecognizer *panCow2 = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(moveCow:)];
[cow2 addGestureRecognizer:panCow2];
// if non ARC, make sure to add a line that says
// [panCow2 release];

然后你的moveCow方法看起来像:

- (void)moveCow:(UIPanGestureRecognizer *)sender
{
    if (sender.state == UIGestureRecognizerStateChanged)
    {
        CGPoint translate = [sender translationInView:self.view];

        sender.view.center = CGPointMake(sender.view.center.x + translate.x, sender.view.center.y + translate.y);
    }
}

我没有测试过这段代码,但你明白了。这就是我通常如何移动单独的子视图...

于 2012-09-10T01:44:02.747 回答