【问题标题】:how to drag dynamically created UIImageview in obj c (xcode)如何在obj c(xcode)中拖动动态创建的UIImageview
【发布时间】:2013-02-14 12:34:01
【问题描述】:

我已经阅读了许多教程来使用 touchesmoved 方法在屏幕上拖动图像 但其中大部分不适用于动态创建的图像视图

在我的项目中,我以编程方式创建了 5 个 UIImageview,

现在我希望用户可以将它们拖到屏幕上>>我也按照这个答案 Question 但一切都是徒劳的,到目前为止没有成功>

动态创建imageview的代码是

for (int i = 0; i < index ; i++) 
{
UIImageView *imageView1 = [[UIImageView alloc] initWithImage:
[UIImageimageNamed:image_name]];        
self.imageView1.userInteractionEnabled = YES;
imageView1.frame = CGRectMake(xvalue, yvalue, 80.0f, 80.0f);
self.view.userInteractionEnabled = YES;
[self.view addSubview:imageView1];
}

提前感谢您的好评

【问题讨论】:

    标签: ios objective-c xcode drag-and-drop uiimageview


    【解决方案1】:

    我对您创建 UIImageViews 的方式有点困惑,因为您使用的是 for 循环,但使用的是静态名称。因此,您可能有 15 个名称为“imageView1”的 UIImageView。 但无论如何,如果您使用链接到的问题中的代码,这根本不重要。也许你把线弄乱了……这是正确的顺序。 (如果它不起作用,请告诉我,以及您遇到的错误)

    //-(void)ViewDidLoad? {???? is this your function??
        int xvalue = 0;//?? not sure where you declared these
        int yvalue = 0;//?? not sure where you declared these
        for (int i = 0; i < index ; i++) {
            UIImageView *imageView1 = [[UIImageView alloc] initWithImage:
            [UIImageimageNamed:image_name]];        
            self.imageView1.userInteractionEnabled = YES;
            imageView1.frame = CGRectMake(xvalue, yvalue, 80.0f, 80.0f);
            self.view.userInteractionEnabled = YES;
            [self.view addSubview:imageView1];
        }
    
        self.elements=[myElements getElements];
        imagesElements = [[NSMutableArray alloc]init];
        for(ElemetsList *item in self.elements) {
                UIImageView *oneelement = [[UIImageView alloc] initWithImage:[UIImage imageNamed:item.imgElement]];
                oneelement.frame = CGRectMake(item.positionX, item.positionY, item.width, item.height);
                oneelement.userInteractionEnabled=YES;
                [imagesElements addObject:oneelement];
        }
    
        for(UIImageView *img in imagesElements) {
            [self.view addSubview:img];
        }
    //}? End Function, whatever it may be...?
    
    -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
    {
        UITouch *touch = [[event allTouches] anyObject];
    
        for(UIImageView *img in imagesElements) 
        {  
            if([self view] == img)
            {
                CGPoint location = [touch locationInView:self.view];
                img.center=location;
            }
        }
    }
    

    如果您只想在移动中为一张图像进行硬编码,请尝试以下操作:

    -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
        UITouch *touch = [[event allTouches] anyObject];
        imageView1.center=[touch locationInView:self.view];
    }
    

    【讨论】:

    • 我已经尝试了上面更正的代码>>只有 for 循环内的行>>但还没有工作
    • 你只试过for (int i = 0; i &lt; index ; i++) {里面的代码?
    • 我已经使用了 touchesmoved 方法,但程序无法进行所需的活动 >> 告诉我 self.elements 代码的用途以及在哪里添加它???
    • 此元素代码让 Xcode 检测您正在拖动的内容,如果它是图像,它允许您拖动该图像。如果您不想拥有所有这些元素的代码(它应该进入您拥有用于创建图像的 for 循环的任何函数中),那么您需要在 touchesMoved 代码中进行硬编码,尽管我不认为这是一个好主意,因为您正在创建多个具有相同名称的图像...(如果您决定硬编码,请考虑使用标签)
    • :: 谢谢问题解决了我唯一做错的事情是发布声明::除此之外,所有代码都很好,现在正在工作::感谢这次长时间的对话>>你的建议肯定对我有帮助>>
    【解决方案2】:

    有很多方法可以解决这个问题。您可以在创建每个 UIImageView 时将 UIPanGestureRecognizer 添加到每个 UIImageView,也可以在 self 上使用 touchesBegan:、touchesMoved:(假设它是 UIViewController)并遍历每个 UIImageView 以查看其框架是否包含触摸点。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-10-08
      • 2023-04-05
      • 1970-01-01
      • 2014-08-19
      • 1970-01-01
      • 1970-01-01
      • 2015-09-10
      相关资源
      最近更新 更多