3

我有一个包含许多图像的滚动视图。我选择使用点击手势。 在此处输入图像描述

选择图像是这样设置的。这部分已经成功完成了。!在此处输入图像描述

但我的问题是当我点击另一个时它给了我多个选择。我想删除以前的选择。在此处输入图像描述

我想要这样的东西,当我选择下一张图片时,前一张被取消选择。 在此处输入图像描述

抱歉,解释不好。

提前致谢。

创建滚动视图的代码

//below code are for create scroll view 



-(void)viewDidLoad{   
    scrollView.delegate = self;
 scrollView.scrollEnabled = YES;
 int scrollWidth = 70;
 scrollView.contentSize = CGSizeMake(scrollWidth,50);

    int xOffset = 0;

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

  img = [[UIImageView alloc] init];
        [img setUserInteractionEnabled:YES];
  img.bounds = CGRectMake(0, 0, 60, 40);
  img.frame = CGRectMake(5+xOffset, 5, 60, 40);

        //below line add here
        img.image = [UIImage imageNamed:[imagesName objectAtIndex:index]];

        img.tag = 303 + index;

      //  [img.layer setBorderColor:[UIColor whiteColor].CGColor];
       // [img.layer setBorderWidth:2.0f];

        [Scrollimages insertObject:img atIndex:index];

        scrollView.contentSize = CGSizeMake(scrollWidth+xOffset,50);
        [scrollView addSubview:[Scrollimages objectAtIndex:index]];

       //xOffset += 170;

        xOffset += 70;
 }


    for (UIImageView *scrollimage in Scrollimages) {

           UITapGestureRecognizer *singleTapRecognizerInScroll = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(scrollViewsingleTappedInScrollMethod:)];

        singleTapRecognizerInScroll.delegate = self;

        singleTapRecognizerInScroll.numberOfTapsRequired = 1;
        singleTapRecognizerInScroll.numberOfTouchesRequired = 1;


        [scrollimage  addGestureRecognizer:singleTapRecognizerInScroll];

        }     
  }  
 -(void)scrollViewsingleTappedInScrollMethod:(UITapGestureRecognizer *)recognizer{

       scrollimageview = (UIImageView *)recognizer.view;

      //we select the image using tag.(contain many images)
    if ([scrollimageview tag] == 303) {

        selectLimitSet = 3;

        }

        }
4

3 回答 3

4

当您选择图像时,将其引用保存到所选图像。选择下一个时,取消选择当前图像视图并选择新的点击手势。1.创建一个属性来记住当前选择的图像视图标签@property (nonatomic, assign) NSInteger currentTag;

-(void)onTapGesture(id)sender{
    if(self.currentTag == sender.tag){
     //tap on previously selected image
     //you can deselect same image or any thing else you want to do.
     return;
    }
    UIImageView *prevImageView = [self.view viewWithTag:self.currentTag];
    //deselect prevImageView, if you want to change image do it now
    UIImageView *selectedImageView = (UIImageView*)sender; 
    self.currentTag = selectedImageView.tag;
   //select selectedImageView now, change image now.
}
于 2013-10-02T15:03:21.697 回答
3

使用 UICollectionView。它会为你做所有的工作。

于 2013-10-02T15:04:48.423 回答
3
   scrollView.delegate = self;
        scrollView.scrollEnabled = YES;
        int scrollWidth = 70;
        scrollView.contentSize = CGSizeMake(scrollWidth,50);

        int xOffset = 0;

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

            img = [[UIImageView alloc] init];
            [img setUserInteractionEnabled:YES];
            img.bounds = CGRectMake(0, 0, 60, 40);
            img.frame = CGRectMake(5+xOffset, 5, 60, 40);

            //below line add here
            img.image = [UIImage imageNamed:[imagesName objectAtIndex:index]];

            img.tag = 303 + index;

            //add below code is here


//take A UIView * displayView make it global (create Instance).

            displayView = img;
            img.alpha = 1.0;


            [Scrollimages insertObject:img atIndex:index];

            scrollView.contentSize = CGSizeMake(scrollWidth+xOffset,50);
            [scrollView addSubview:[Scrollimages objectAtIndex:index]];

            //xOffset += 170;

            xOffset += 70;


        }

-(void)scrollViewsingleTappedInScrollMethod:(UITapGestureRecognizer *)recognizer{

    //selectedImageInScrollView it's a integer value

    selectedImageInScrollView = recognizer.view.tag;

// set your instance displayView Alpha & set tag them. 

//displayView is first deselect your first view then select next image

    displayView.alpha = 1.0;
    displayView =recognizer.view;

    recognizer.view.alpha = 0.5;


    if (selectedImageInScrollView == 303) {

        selectLimitSet = 3;
        NSLog(@"selectLimitSet : %i",selectLimitSet);

    }
}
于 2013-10-16T06:22:05.463 回答