UICollectionviewCell图像更改选择

时间:2015-05-07 12:15:58

标签: ios objective-c uicollectionview uicollectionviewcell

我在每个单元格中都有UICollectionview个控制器UIButton。在选择按钮时我必须更改按钮bg图像,在双击时我必须再次更改它。 (单击以点击,双击以在用户兴趣页面上爱概念)。 做这个的最好方式是什么?

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

    UICollectionViewCell *cell = [interestsCollection dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];

    UIButton* img=[[UIButton alloc]init];
    [img setImage:[UIImage imageNamed:[imgTitleArray objectAtIndex:indexPath.row]] forState:UIControlStateNormal];
    [img addTarget: self action: @selector(interestClicked:) forControlEvents: UIControlEventTouchUpInside] ;
    img.frame=CGRectMake(10, 10, 60, 60);
    [cell.contentView addSubview:img];

    UILabel* lbl=[[UILabel alloc]initWithFrame:CGRectMake(0, 75, 80, 20)];
    lbl.textAlignment = NSTextAlignmentCenter;
    lbl.text=[titleArray objectAtIndex:indexPath.row];
    lbl.font = [UIFont fontWithName:@"Arial" size:11];
    lbl.textColor = [UIColor colorWithRed:135.0/255.0 green:135.0/255.0 blue:135.0/255.0 alpha:1.0];
    [cell.contentView addSubview:lbl];

    return cell;
}

2 个答案:

答案 0 :(得分:1)

您可以使用单{{}} TapGesture处理此类情况。在受尊敬的选择器的帮助下,您可以更改UIImage的{​​{1}}。

以下是指导您的链接。如何在UIButton上创建双击手势。借助此功能,您还可以创建单击手势。

Collection View + Double Tap Gesture

答案 1 :(得分:0)

UITapGestureRecognizer添加到按钮并根据需要更改颜色。

  UITapGestureRecognizer *tapOnce = [[UITapGestureRecognizer alloc] initWithTarget:self  action:@selector(tapOnce:)];
  UITapGestureRecognizer *tapTwice = [[UITapGestureRecognizer alloc] initWithTarget:self  action:@selector(tapTwice:)];

  tapOnce.numberOfTapsRequired = 1;
  tapTwice.numberOfTapsRequired = 2;

  //stops tapOnce from overriding tapTwice
  [tapOnce requireGestureRecognizerToFail:tapTwice];

  [self.button addGestureRecognizer:tapOnce]; //remove the other button action which calls method `button`
  [self.button addGestureRecognizer:tapTwice];