Objective c IOS7风格模糊UICollectionViewCell';s的观点随时可用

Objective c IOS7风格模糊UICollectionViewCell';s的观点随时可用,objective-c,ios7,uicollectionview,uicollectionviewcell,Objective C,Ios7,Uicollectionview,Uicollectionviewcell,我正在尝试在点击时模糊UICollectionViewCell。我试着拍下手机的截图,然后模糊截图。使用模糊的屏幕截图作为背景并隐藏所有标签 我注意到的行为是 点击单元格1:单元格1消失 轻触单元格2:有点模糊,但我可以看出错误单元格的内容被模糊了 轻触第三单元:模糊一点 点击第四单元:大量模糊 我认为问题是: 错误的单元格内容变得模糊 当另一个单元格模糊时,模糊的屏幕截图将被重新使用,因此模糊会增加 然后我编辑了代码,将背景设置为原始的未模糊屏幕截图 点击单元格1:单元格1消失 轻触单元格2:

我正在尝试在点击时模糊UICollectionViewCell。我试着拍下手机的截图,然后模糊截图。使用模糊的屏幕截图作为背景并隐藏所有标签

我注意到的行为是

  • 点击单元格1:单元格1消失

  • 轻触单元格2:有点模糊,但我可以看出错误单元格的内容被模糊了

  • 轻触第三单元:模糊一点

  • 点击第四单元:大量模糊

  • 我认为问题是:

  • 错误的单元格内容变得模糊

  • 当另一个单元格模糊时,模糊的屏幕截图将被重新使用,因此模糊会增加

  • 然后我编辑了代码,将背景设置为原始的未模糊屏幕截图

  • 点击单元格1:单元格1消失

  • 轻触单元格2:显示单元格1的屏幕截图

  • 轻触单元格3:显示单元格2的屏幕截图

  • 等等

    代码如下:

    -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
     //Code to get data from database and add to labels
    
      if(![[modelArray objectAtIndex:indexPath.row] isEqualToString:@"1"]){
          UIGraphicsBeginImageContextWithOptions(cell.overviewView.bounds.size, NO, cell.overviewView.window.screen.scale);
          [cell.overviewView drawViewHierarchyInRect:cell.overviewView.frame afterScreenUpdates:NO];
          UIImage *snapshotImage = UIGraphicsGetImageFromCurrentImageContext();
          UIGraphicsEndImageContext();
    
          snapshotImage = [snapshotImage applyLightEffect];
    
          cell.overviewView.backgroundColor = [UIColor colorWithPatternImage:snapshotImage];
          cell.overviewView.alpha = 1;
    
          cell.descLabel.alpha = 0;
          cell.dayLabel.alpha = 0;
          cell.weatherImage.alpha = 0;
          cell.tempLabel.alpha = 0;
      }
    
    
      NSLog(@"Cell :%d condition : %@", indexPath.row, weather.desc );
      return cell;
    }
    -(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
      WeekCell *cell = (WeekCell*)[collectionView cellForItemAtIndexPath:indexPath];
      NSLog(@"%@", cell.descLabel.text);
      if([[modelArray objectAtIndex:indexPath.row] isEqualToString:@"1"]){
         [modelArray setObject:@"0" atIndexedSubscript:indexPath.row];
      }else{
        [modelArray setObject:@"1" atIndexedSubscript:indexPath.row];
      }
    
      NSLog(@"sequence : %@", modelArray);
      [collectionView reloadItemsAtIndexPaths:@[indexPath]];
    
    }
    

    我只是发布了你可以尝试的示例代码

    在自定义
    UICollectionViewCell
    中,我将其命名为
    CustomCollectionVIewCell
    ,其nib文件如下所示

     - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
     {
    
       CustomCollectionVIewCell *cell = (CustomCollectionVIewCell *)[collectionView cellForItemAtIndexPath:indexPath];
       UIGraphicsBeginImageContext(cell.cellImageView.bounds.size);
       [cell.forGroundView.layer renderInContext:UIGraphicsGetCurrentContext()];
       UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
       UIGraphicsEndImageContext();
       //   self.blurrImage = [image applyTintEffectWithColor:[UIColor colorWithRed:25.0f/255.0f green:77.0f/255.0f blue:108.0f/255.0f alpha:1.0f ]];
       cell.forGroundView.alpha = 0.0f;
       cell.blurImageVIew.image = [image applyLightEffect];
     }
    

    在上面的图像中,u可以看到
    视图背景视图
    包含用于保存模糊图像的图像视图和
    视图前景
    是一个包含普通图像和两个标签的视图,一个带有绿色和黑色文本,这两个标签添加到集合视图单元,而不是任何其他视图

    单元层次结构如下图所示

    在代码中,将所有这些连接为单元中的插座,
    CustomCollectionVIewCell.h

      @interface CustomCollectionVIewCell : UICollectionViewCell
    
      @property (nonatomic, assign) id<CellFooterDelegate> myCustomdelegate;//define a delegate
      @property (weak, nonatomic) IBOutlet UIView *backGroundView;
      @property (weak, nonatomic) IBOutlet UIImageView *blurImageVIew;
      @property (weak, nonatomic) IBOutlet UIView *forGroundView;
      @property (weak, nonatomic) IBOutlet UIImageView *cellImageView; 
      //..other objects
    
      @end
    
    在控制器中

      -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
     {
        CustomCollectionVIewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
        cell.myCustomdelegate = self;
        cell.cellImageView.image = [dataSourceArray objectAtIndex:indexPath.row];
        return cell;
     }
    
    选择单元格后,u可以放置模糊图像,如下所示

     - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
     {
    
       CustomCollectionVIewCell *cell = (CustomCollectionVIewCell *)[collectionView cellForItemAtIndexPath:indexPath];
       UIGraphicsBeginImageContext(cell.cellImageView.bounds.size);
       [cell.forGroundView.layer renderInContext:UIGraphicsGetCurrentContext()];
       UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
       UIGraphicsEndImageContext();
       //   self.blurrImage = [image applyTintEffectWithColor:[UIColor colorWithRed:25.0f/255.0f green:77.0f/255.0f blue:108.0f/255.0f alpha:1.0f ]];
       cell.forGroundView.alpha = 0.0f;
       cell.blurImageVIew.image = [image applyLightEffect];
     }
    
    取消选择后,只需更改前景视图的alpha

     - (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath
     {
        CustomCollectionVIewCell *cell = (CustomCollectionVIewCell *)[collectionView cellForItemAtIndexPath:indexPath];
        cell.blurImageVIew.image = nil;
        cell.forGroundView.alpha = 1.0f;
     }
    
    我们会像下面一样

     - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
     {
    
       CustomCollectionVIewCell *cell = (CustomCollectionVIewCell *)[collectionView cellForItemAtIndexPath:indexPath];
       UIGraphicsBeginImageContext(cell.cellImageView.bounds.size);
       [cell.forGroundView.layer renderInContext:UIGraphicsGetCurrentContext()];
       UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
       UIGraphicsEndImageContext();
       //   self.blurrImage = [image applyTintEffectWithColor:[UIColor colorWithRed:25.0f/255.0f green:77.0f/255.0f blue:108.0f/255.0f alpha:1.0f ]];
       cell.forGroundView.alpha = 0.0f;
       cell.blurImageVIew.image = [image applyLightEffect];
     }
    


    希望这对你有帮助:)

    你需要什么?你能分享一张图片吗?@Bhaskar添加了一张图片谢谢@Shan,很好的解释。非常感谢。