Question

How can I move multiple UIImage views around a view controller?

I have managed to use this code;

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    printf("touch began --------- |n");


}


-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
    printf("touch moved --------- |n");


    UITouch *myTouch = [touches anyObject];
    startPoint = [myTouch locationInView:self.view];



    ball.center = CGPointMake(startPoint.x, startPoint.y);

    }


-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
    printf("touch end --------- |n");

    }

This code above has made it so that I can move one UIImage view around with touch, however, I want to be able to move 100's around. This code also, currently, when you move your finger around an area of the screen the image jumps to your finger.

Is this the best way to do it? Or is a pan gesture better?

Please help me make it so I can move multiple images around my view controller with touch and if you use the code above, please stop the image from jumping!

Please help!

.H FILE FOR ANSWER REFERRAL;

@interface CMViewController : UIViewController {
    CGPoint startPoint;


}


@property CGPoint startPoint;

@property (strong, nonatomic) IBOutlet UIImageView *smyImageView;
@property (strong, nonatomic) IBOutlet UIImageView *smyImageView1;





@end

Thanks

Was it helpful?

Solution

You can use below. I used this for multiple images moving on screen. It's working for me.

UIPanGestureRecognizer *span=[[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(onsPan:)];

 [smyImageView addGestureRecognizer:span];

   UIPanGestureRecognizer *span1=[[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(onsPan1:)];

   [smyImageView1 addGestureRecognizer:span1];

Moving (Pan):

- (void)onsPan:(UIPanGestureRecognizer *)recognizer {

   CGPoint translation = [recognizer translationInView:self.view];
    recognizer.view.center = CGPointMake(recognizer.view.center.x + translation.x,
                                         recognizer.view.center.y + translation.y);
    [recognizer setTranslation:CGPointMake(0, 0) inView:self.view];

}

- (void)onsPan1:(UIPanGestureRecognizer *)recognizer {

    CGPoint translation = [recognizer translationInView:self.view];
    recognizer.view.center = CGPointMake(recognizer.view.center.x + translation.x,
                                         recognizer.view.center.y + translation.y);
    [recognizer setTranslation:CGPointMake(0, 0) inView:self.view];

}

OTHER TIPS

This will teach you everything you need to know for what you want to do.

http://www.raywenderlich.com/44270/sprite-kit-tutorial-how-to-drag-and-drop-sprites

This is another answer that may help you

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top