2

I try to move 2 UIImageView.

Only one picture is supposed to move when I drag.

What I trying to accomplish is move UIImageView when I touch and drag the UIImageView.

This Is my viewDidLoad :

-(void)viewDidLoad 
{
 [super viewDidLoad];
  myImageView = [[UIImageView alloc]initWithFrame:CGRectMake(100, 100, 200, 100)];

 [myImageView setImage:[UIImage imageNamed:@"image1.png"]];

 [self.view addSubview:myImageView];


 myImageView1 = [[UIImageView alloc]initWithFrame:CGRectMake(100, 200, 200,100)];

 [myImageView1 setImage:[UIImage imageNamed:@"image2"]];

 [self.view addSubview:myImageView1]; 
}

This Is my touchesMoved method :

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

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

     for (UIImageView *image in [self.view subviews])
    {
       if ([touch view] == image)
       {
          // move the image view
          image.center = touchLocation;
       }

   }
}

In this line I don't get the images view

for (UIImageView *image in [self.view subviews])

It is possible to get the current touched UIIamgeView ?

Roei Nadam
  • 1,780
  • 1
  • 15
  • 33
  • 1
    This is not yet a question. You need to tell us more information, such as: 1. What are you trying to accomplish. 2. What is the current outcome? I'm guessing that you are trying to move just the `UIImageView` that contains the touch, but you need to be more specific and provide more details. – mbm29414 Jan 29 '15 at 20:10
  • 1
    thanks I edit the post , and add more information. – Roei Nadam Jan 29 '15 at 20:20
  • You still haven't really told us what's going wrong. – mbm29414 Jan 29 '15 at 20:20
  • Whats going wrong?? You mean u drag 2 pictures instead of one? – Evgeniy Kleban Jan 29 '15 at 20:22
  • I can not move at the moment image, I try to move one image Every time the user pan the screen. – Roei Nadam Jan 29 '15 at 20:26

1 Answers1

1

There is methods you can use to drag UIImage's (or maybe other interface elements):

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

I suggest you to download and understand sample Apple app, that is design to illustrate drawing, touch handling, and animation using UIKit and Core Animation. There it is: https://developer.apple.com/library/ios/samplecode/MoveMe/Introduction/Intro.html

Hope i that is helpful.

Evgeniy Kleban
  • 6,794
  • 13
  • 54
  • 107
  • My problem is how to get the current UIIimageview that touch. – Roei Nadam Jan 29 '15 at 20:31
  • 1
    Roel you need to implement gesture recogniser, please take a read following tutorial: http://www.raywenderlich.com/6567/uigesturerecognizer-tutorial-in-ios-5-pinches-pans-and-more – Evgeniy Kleban Jan 29 '15 at 21:19