Roei Nadam
Roei Nadam

Reputation: 1780

Try to move UIImageView in screen with touchesMoved

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 ?

Upvotes: 2

Views: 324

Answers (1)

Evgeniy Kleban
Evgeniy Kleban

Reputation: 6975

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.

Upvotes: 1

Related Questions