如何顺利移动UIImageViews数组?

时间:2017-02-10 02:30:59

标签: ios objective-c animation uiview uitouch

我正在进行各种座位安排申请。应用程序允许管理员通过点击并移动每个座位来安排座位,然后使用每个座位的框架尺寸保存座位布置。我尝试在自定义视图上加载5个UIImageView并移动图像视图,但只有一个图像视图被移动。其余的图像视图没有被移动。你能帮帮我吗?

- (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);
     }
 }

管理员安排座位后,我们必须保存所有可见的uiimageview框架。感谢。

编辑:

我尝试使用UIPanGestureRecognizer,但只有一个imageview被移动。不知道哪里出错了?你能帮我么?感谢。

- (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);
    }
}

1 个答案:

答案 0 :(得分:3)

不要使用touches方法;你会疯了。使每个图像视图用户交互,并为其提供UIPanGestureRecognizer。平移手势识别器的动作处理程序有标准代码,用于跟随手指,即使视图可拖动。

一旦你完成了这一切,一切都源于你滥用self.hotelImage。完全摆脱它。在for循环中,只需使用局部变量UIImageView* hotelImage = ...即可。在手势识别器方法中,使用[recogniser view]来引用手势识别器所附加到的图像视图。在那之后,一切都会自行解决!