0

.m

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];

然后我尝试与元素交互:

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

但是“if ([self view] == img)”总是“NO”。因此,元素不会拖动。如果它们在数组中,如何拖放项目?

4

1 回答 1

1

添加图像视图时,会将它们设为 的子视图self.view因此您需要测试是否imgsubviews数组中。它不能等于包含它的视图。

于 2012-07-18T16:18:42.197 回答