如何在ios中使用点击手势选择另一个图像时取消选择上一个选定的图像?

时间:2013-10-02 14:54:18

标签: iphone ios objective-c uitapgesturerecognizer

我有一个包含许多图像的滚动视图。我选择使用轻拍手势。 enter image description here

选择图像就是这样设置的。这部分成功完成。!enter image description here

但我的问题是,当我点击另一个时,它会给我多个选择。我想删除以前的选择。 enter image description here

我想要一些类似的东西,当我选择下一张图片时,前一张图片将被取消选择。 enter image description here

抱歉,原因不好。

提前致谢。

创建滚动视图的代码

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

        }

        }

3 个答案:

答案 0 :(得分: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.
}

答案 1 :(得分:3)

使用UICollectionView。它将为您完成所有工作。

答案 2 :(得分: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);

    }
}