user1178258
user1178258

Reputation: 9

moving multiple images in iOS after being created programmatically using tags

I am trying to programmatically create multiple images then be able to move any of them around the view. I click a button and then I can move that image. I click the button again and I can move that image but no longer can move the first image created. I was trying to use tags.

header.

UIImageView *imageView;    
NSUInteger i;

and

@property (nonatomic, retain) UIImageView *imageView;

implementation

@synthesize imageView;

-(IBAction)printTheImage:(id)sender{
    UIImageView *theImage = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"animage.png"]];
    self.imageView = theImage;
    self.imageView.userInteractionEnabled = YES;
    self.imageView.frame = CGRectMake(500, 500, 200, 200);
    imageView.tag = i;
    [self.view addSubview:self.imageView];
    i++;
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [[event allTouches]anyObject];
    CGPoint location = [touch locationInView:self.view];
    if ([touch view]==self.imageView) 
        {
        if (ImageView.tag == 1){           
           self.imageView.center = location;
        }
        if (imageView.tag == 2){
           self.imageView.center = location;
        }
    }
}

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{ 
    [self touchesBegan:touches withEvent:event];
}

- (void)viewDidLoad{
    i = 1;
    [super viewDidLoad];
}

Upvotes: 0

Views: 1582

Answers (3)

Julio Montoya
Julio Montoya

Reputation: 1614

this approach works with gesture recognizer

    @interface ViewController : UIViewController {
        int tags;
    }

    - (IBAction)create:(id)sender;

    @end

        @implementation ViewController

        - (void)viewDidLoad
        {
            [super viewDidLoad];

            //Tag to start
            tags = 1;
        }

        - (IBAction)create:(id)sender
        {
            UIImageView *temp = [[UIImageView alloc] initWithFrame:CGRectMake(0, 100, 200, 200)];
            temp.image = [UIImage imageNamed:@"yourimage.jpg"];
            temp.userInteractionEnabled = YES;
            temp.tag = tags;

            UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self
                                                                                  action:@selector(handlePan:)];

            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
            tap.numberOfTapsRequired = 1;

            [temp addGestureRecognizer:pan];
            [temp addGestureRecognizer:tap];
            [self.view addSubview:temp];

            tags++;
        }


        //This is just to check if the tags are working
        -(void)handleTap:(UITapGestureRecognizer *)recognizer
        {
            UIView *piece = recognizer.view;
            int index = piece.tag;
            NSString *msg = [NSString stringWithFormat:@"Tag:%i", index];

            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Test"
                                                            message:msg
                                                           delegate:self
                                                  cancelButtonTitle:@"ok"
                                                  otherButtonTitles:nil, nil];
            [alert show];
        }

        //Drag action
        -(void)handlePan:(UIPanGestureRecognizer *)recognizer
        {
            UIView *piece = recognizer.view;

            if ([recognizer state] == UIGestureRecognizerStateBegan || [recognizer state] == UIGestureRecognizerStateChanged)
            {
                CGPoint translation = [recognizer translationInView:[piece superview]];
                [piece setCenter:CGPointMake([piece center].x + translation.x, [piece center].y + translation.y)];
                [recognizer setTranslation:CGPointZero inView:[piece superview]];
                [self.view bringSubviewToFront:piece];
            }
        }

@end

already tested and working

good luck

Upvotes: 0

Jeff Wolski
Jeff Wolski

Reputation: 6372

Try this instead.

@property (nonatomic, copy) NSMutableArray *imageViews;


@synthesize imageView;

-(IBAction)printTheImage:(id)sender{
    UIImageView *theImage = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"animage.png"]];

    [self.imageViews adObject:theImage];

    theImage.userInteractionEnabled = YES;
    theImage.frame = CGRectMake(500, 500, 200, 200);
    imageView.tag = [imageViews indexOfObject:theImage];
    [self.view addSubview:theImage];
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [[event allTouches]anyObject];
    CGPoint location = [touch locationInView:self.view];
    NSInteger viewIndex = [self.imageViews indexOfObject:[touch view]];
    if (viewIndex != NSNotFound) 

        UIView *theImage = self.imageViews[viewIndex];
        theImage.center = location;

    }
}

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{ 
    [self touchesBegan:touches withEvent:event];
}

Upvotes: 0

Dunja Lalic
Dunja Lalic

Reputation: 752

Here's what I came up with from your code

//header
NSUInteger imageCount;
@property (nonatomic, retain) UIImageView *selectedImageView;

//implementation

@synthesize selectedImageView;

-(IBAction)printTheImage:(id)sender {
    UIImageView *imageView = [[UIImageView alloc] initWithImage: [UIImage imageNamed: @"animage"]];
    [imageView sizeToFit];
    imageView.frame = CGRectMake(500, 500, 200, 200);
    imageView.userInteractionEnabled = YES;
    imageView.tag = imageCount;
    [self.view addSubview: imageView];
    [imageView release];

    imageCount++;
}

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

    //iterate trough all images
    for (int i = 0; i < imageCount; i++) {
        //get image from tag
        UIImageView *imageView = (UIImageView *)[self.view viewWithTag: i];
        //check which image was tapped
        if (CGRectContainsPoint(imageView.frame, [touch locationInView:self.view])) {
            NSLog(@"Image #%i was tapped",i);
            self.selectedImageView = imageView;
            //don't waste processor time for checking all other images, get the first and break the loop
            break;
    }
}

This is what moves the image:

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

    //move the tapped image
    self.selectedImageView.center = [touch locationInView:self.view];
}

Remeber to reset the image counter and set selectedImageView to nil on touches ended and touches canceled phase

Upvotes: 1

Related Questions