Yuvaraj.M
Yuvaraj.M

Reputation: 9801

How to move array of UIImageViews smoothly?

Am working on kind of seating arrangement application. Where the app allows the admin to arrange the seatings by click and move the each seat and then save the seating arrangement with frame sizes of each seat. I tried by loading 5 UIImageView on custom view and move the imageview but, only one imageview is getting moved. Rest of the imageviews not getting moved. Can you please help me on this?

- (void) addSeatsToTheView {

    for (int i=0; i<5; i++) {

        self.hotelImage = [[UIImageView alloc] initWithFrame:CGRectMake(5, 5, 50, 50)];
        self.hotelImage.image = [UIImage imageNamed:@"DiningIcon"];
        self.hotelImage.userInteractionEnabled = YES;
        [self.hotelImage setUserInteractionEnabled:YES];
        self.hotelImage.tag = i;
        [self.hotelView addSubview:self.hotelImage];
    }
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    if (touch.view == self.hotelImage) {
        CGPoint location = [touch locationInView:self.hotelView];
        NSLog(@"Begen Touch Location: %@", NSStringFromCGPoint(location));
        self.hotelImage.frame=CGRectMake(location.x, location.y, 50, 50);
    }
 }

 - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
     UITouch *touch = [touches anyObject];
     if (touch.view == self.hotelImage) {
         CGPoint location = [touch locationInView:self.hotelView];
         NSLog(@"Begen Touch Location: %@", NSStringFromCGPoint(location));
         self.hotelImage.frame=CGRectMake(location.x, location.y, 50, 50);
     }
 }

 - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
     UITouch *touch = [touches anyObject];
     if (touch.view == self.hotelImage) {
         CGPoint location = [touch locationInView:self.hotelView];
         NSLog(@"Begen Touch Location: %@", NSStringFromCGPoint(location));
         self.hotelImage.frame=CGRectMake(location.x, location.y, 50, 50);
     }
 }

Once the admin arranged the seatings we have to save all the visible uiimageview framesizes. Thanks.

Edited:

I tried the using UIPanGestureRecognizer but, only one imageview getting moved. Don't know where am doing wrong? Can you please help me? Thanks.

- (void) addSeatsToTheView {

    for (int i=0; i<5; i++) {

        self.hotelImage = [[UIImageView alloc] initWithFrame:CGRectMake(5, 5, 50, 50)];
        self.hotelImage.image = [UIImage imageNamed:@"DiningIcon"];
        self.hotelImage.userInteractionEnabled = YES;
        [self.hotelImage setUserInteractionEnabled:YES];
        self.hotelImage.tag = i;
        UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(gestureRecognizerMethod:)];
        [self.hotelImage addGestureRecognizer:panGestureRecognizer];
        [self.hotelView addSubview:self.hotelImage];
    }
}

- (void)gestureRecognizerMethod:(UIPanGestureRecognizer *)recogniser
{
    if (recogniser.state == UIGestureRecognizerStateBegan || recogniser.state == UIGestureRecognizerStateChanged)
    {
        CGPoint touchLocation = [recogniser locationInView:self.hotelView];
        self.hotelImage.frame = CGRectMake(touchLocation.x, touchLocation.y, 50, 50);
    }
}

Upvotes: 0

Views: 54

Answers (1)

matt
matt

Reputation: 535944

Don't use touches methods for this; you'll go insane. Make each image view user-interactive and give it a UIPanGestureRecognizer. There is standard code for the pan gesture recognizer's action handler for following a finger, i.e. making the view draggable.

Once you've done that, everything stems from your misuse of self.hotelImage. Get rid of it entirely. In your for loop, just use a local variable, UIImageView* hotelImage = .... In the gesture recognizer method, use [recogniser view] to refer to the image view to which this gesture recognizer is attached. Everything will sort itself out after that!

Upvotes: 3

Related Questions