Iphone 将元素拖动到图像视图中。使用NSMutableArray
.mIphone 将元素拖动到图像视图中。使用NSMutableArray,iphone,objective-c,xcode,ios5,Iphone,Objective C,Xcode,Ios5,.m self.elements=[myElements getElements]; imagesElements = [[NSMutableArray alloc]init]; for(ElemetsList *item in self.elements) { UIImageView *oneelement = [[UIImageView alloc] initWithImage:[UIImage imageNamed:item.imgElement]]; o
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;
}
}
}
但是“如果([自我视图]==img)”总是“否”。因此,图元不会拖动。
如果项目在数组中,如何拖放它们?当您添加图像视图时,您会使它们成为
self.view
的子视图,因此您需要测试img
是否在子视图
数组中。它不能等于包含它的视图。这一行的主要用途是什么<代码>用于(UIImageView*imagesElements中的img)[self.view addSubview:img]代码>谢谢您的回复!但如何正确地做到这一点呢?如果我在uberayu touchmoved中扫描一个项目,元素将被移动