如何从水平滚动视图拖动多个图像?

时间:2011-10-15 10:47:27

标签: iphone ios ios4

我正在制作一个应用程序,用户可以从水平滚动视图中选择图像,并且选择了一个图像,他还应该能够拖动所有选定的图像吗?

我能够一次选择一个图像并且能够拖动单个图像,但是我想要一个接一个地选择多个图像来逐个选择它们吗?

这是我的代码:

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

请帮忙 建议我一些教程

1 个答案:

答案 0 :(得分:0)

我认为一个好主意是将所有选定的图像绑定到一个NSMutableArray中,该NSMutableArray维护对它们的引用 - 分别在选择/取消选择它们时添加/删除它们。然后,在“touchesMoved”中,不是仅更新接收触摸的UIImageView,而是迭代所有选定的图像视图并更改其中心。希望这有帮助!