Selecting images and then moving them

I am trying to figure out how to select an object first then I am able to move it. For example I want to select cow first by touching it then I am able to move it. The reason being is when I am touching the screen it is moving both cow and cow1. When I only want to move one cow at a time. Any help will be appreciated.

-(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);

}

Answers


Try using UIPanGestureRecognizer, like this:

@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];
    }
}

There are many ways to implement this sort of thing. It depends on how your graphics are implemented, amongst other things, check out this Apple sample code:

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


The standard solution would be to add separate gesture recognizers for your two cow objects, e.g., in 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];

And then you have your moveCow method look something like:

- (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);
    }
}

I haven't tested this code, but you get the idea. This is how I generally move separate subviews around...


Need Your Help

Scroll Position in HTML document - How to determine

javascript html

Imagine an HTML documetn with a long long list of paragraphs. The user scrolls 52% down the document with the scroll bar.

cannot debug GWT native code within gwtmockito

gwt gwtmockito

I have a GwtMockitoTestCase and the debugger seems not to enter inside any vanilla GWT class like Widget, ResizeLayoutPanel, etc.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.