0

I wanna to create dynamically UIImageView on every tap on main view and also wanna to move all the created UIImageView by finger movement.I've got success to dynamic creation on every touch my code is below :

-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *touch = [touches anyObject]; CGPoint pt = [touch locationInView:self.view]; imgView = [[[UIImageView alloc] initWithFrame:CGRectMake(pt.x,pt.y, 100, 100)] autorelease]; imgFilepath = [[NSBundle mainBundle] pathForResource:@"img" ofType:@"png"]; img = [[UIImage alloc] initWithContentsOfFile:imgFilepath]; imgView.tag=i; [imgView setImage:img]; [img release]; [self.view addSubview:imgView]; } 

Now I wanna to move any of the dynamically created UIImageView by finger movement on it. Thanks.

2 Answers 2

4

Set the userInteractionEnabled property of the UIImageView to yes in the method you create it (i. e. the method you posted):

imgView.userInteractionEnabled = YES; 

then you will most probably need to subclass UIImageView and implement the following methods to move your image view around:

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

Hint: in the touchesMoved: etc. method, you'll want to track where the user's finger moved -- after reading some more docs you'll most probably end up using -[UITouch locationInView:] -- you'll need to convert the coordinates to the superview of your image view and then move the image view itself in this case.

Sign up to request clarification or add additional context in comments.

Comments

1

In touchesBegan detect the imageView you have touched. In touchesMoved move the selected imageView by setting it's center to the touch point moved.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.