2011-10-15 38 views
0

我正在做一个应用程序,用户可以从水平滚动视图中选择图像,并且一个人的图像被选中,他应该也能够拖动所有选择的图像?如何从水平滚动视图拖动多个图像?

我可以一次选择一张图片,并且可以拖动单张图片,但是我希望多张图片能够一张接一张地挑选出来。

这是我的代码:

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
    if (self) { 
     // Custom initialization. 
     imagesName = [[NSArray alloc]initWithObjects:@"hat3.png",@"hat4-1.png",@"t-shirt.png",@"t-shirt.png", 
        @"Untitled-2.png",@"logo1.jpg",@"logo2.jpg",@"logo3.jpg",nil]; 
     images = [[NSMutableArray alloc]init]; 
    } 
    return self; 
} 


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib. 
- (void)viewDidLoad { 

    [super viewDidLoad]; 

     [self.view setMultipleTouchEnabled:YES]; 

    imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"t-shirt.png"]]; 
    imageView.center = self.view.center; 



    [self.view addSubview:imageView]; 

    scrollView.delegate = self; 
    scrollView.scrollEnabled = YES; 
    int scrollWidth = 120; 
    scrollView.contentSize = CGSizeMake(scrollWidth,80); 


    int xOffset = 0; 
    imageView.image = [UIImage imageNamed:[imagesName objectAtIndex:0]]; 

    //for(int index=0; index < [imagesName count]; index++) 
     for (int index=0; index < [imagesName count]; index++) 
    { 

     UIImageView *img = [[UIImageView alloc] init]; 
     //img.bounds = CGRectMake(10, 10, 50, 50);a 
     img.bounds = CGRectMake(0, 0, 20, 20); 
     //img.frame = CGRectMake(5+xOffset, 0, 160, 110); 
     img.frame = CGRectMake(0+xOffset, 0, 60, 61); 

     //img.frame = CGRectMake(<#CGFloat x#>, <#CGFloat y#>, <#CGFloat width#>, <#CGFloat height#>); 
     NSLog(@"image: %@",[imagesName objectAtIndex:index]); 
     img.image = [UIImage imageNamed:[imagesName objectAtIndex:index]]; 
     [images insertObject:img atIndex:index];   
     scrollView.contentSize = CGSizeMake(scrollWidth+xOffset,110); 
     [scrollView addSubview:[images objectAtIndex:index]]; 
     xOffset += 170; 
    } 

} 


- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 

    UITouch * touch = [[event allTouches] anyObject]; 

    for(int index=0;index<[images count];index++) 
    { 
     UIImageView *imgView = [images objectAtIndex:index]; 
     NSLog(@"x=%f,y=%f,width =%f,height=%f",imgView.frame.origin.x,imgView.frame.origin.y,imgView.frame.size.width,imgView.frame.size.height); 
     NSLog(@"x= %f,y=%f",[touch locationInView:self.view].x,[touch locationInView:self.view].y) ; 

     if(CGRectContainsPoint([imgView frame], [touch locationInView:scrollView])) 
     { 
      [self ShowDetailView:imgView]; 
      break; 
     } 
    } 
} 


-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{ 
    [super touchesMoved:touches withEvent:event]; 

    NSArray *allTouches = [touches allObjects]; 
    int count = [allTouches count]; 

    if (count == 1) { 
     if (CGRectContainsPoint([imageView frame], [[allTouches objectAtIndex:0] locationInView:self.view])) { 
      imageView.center = [[allTouches objectAtIndex:0] locationInView:self.view]; 
      return; 
     } 
    } 
} 

请帮 建议我一些教程

+0

在此处粘贴代码时,请正确格式化。别人必须阅读你的代码并回答你。请客气... –

+0

先生,我是新来这个网站...我真的没有任何关于这个东西的想法....我很抱歉这个 – Nilesh

回答

0

我认为一个好的想法是绑定所有选择的图像变成一个NSMutableArray维持对它们的引用 - 添加/删除它们,因为它们分别被选中/取消选择。然后,在您的“touchesMoved”中,不是只更新接收触摸的UIImageView,而是遍历所有选定的图像视图并更改它们的中心。希望这可以帮助!

+0

你可以PLZ给我一些相关的教程东西.............这将是对我有帮助 – Nilesh

+0

对不起,我从来没有见过这在任何教程完成 – Stavash