【问题标题】:How to move array of UIImageViews smoothly?如何顺利移动 UIImageViews 数组?
【发布时间】:2017-02-10 02:30:59
【问题描述】:

我正在开发一种座位安排应用程序。该应用程序允许管理员通过单击并移动每个座位来安排座位,然后将座位安排与每个座位的框架尺寸一起保存。我尝试在自定义视图上加载 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,但只有一个图像视图被移动。不知道哪里做错了?你能帮我么?谢谢。

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

【问题讨论】:

    标签: ios objective-c animation uiview uitouch


    【解决方案1】:

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

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

    【讨论】:

    • 嗨,马特。感谢您的回复。我也试过 UIPanGestureRecognizer。我编辑了我的问题。仍然只有一个图像视图被移动。你能帮助我吗?谢谢。
    • 当然,我发现您的新代码存在问题。等一下,我会补充我的答案。
    • 谢谢马特。我得到了这个问题,现在它的工作。谢谢。
    猜你喜欢
    • 2022-10-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-26
    • 1970-01-01
    • 1970-01-01
    • 2011-04-26
    • 2014-02-11
    相关资源
    最近更新 更多