ios - 如何顺利移动 UIImageViews 数组?

标签 ios objective-c animation uiview uitouch

我正在研究一种座位安排应用程序。该应用程序允许管理员通过单击并移动每个座位来安排座位,然后使用每个座位的框架尺寸保存座位安排。我尝试在自定义 View 上加载 5 个 UIImageView 并移动 ImageView ,但是只有一个 ImageView 被移动。其余的 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;
        [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);
    }
}

最佳答案

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

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

关于ios - 如何顺利移动 UIImageViews 数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42150522/

相关文章:

ios - 使用按钮更改 UIAlertView 内容

iOS PhoneGap 应用升级到 Cordova 2.1 - 内容完全忽略状态栏

objective-c - 如何阻止 NSSavePanel 在完成 block 后关闭?

ios - UIView Swift 倾斜动画

Java 在同一个 JFrame 中绘制多个正方形

ios - 从 Xamarin 中的代码隐藏创建 UIViewController

iphone - 如何启动有关设备类型的特定 View

ios - 为什么 NSTimer 可以在后台模式下延迟?

iOS,在 Objective C 中使用结构,结构值不可从 super View 分配

java - 将两个 Activity JPanel 层叠在一起