淡入淡出的UICollectionViewCells有重叠会出错

时间:2019-01-27 01:51:02

标签: ios xamarin.ios uicollectionview

我有一个带有一些UICollectionViewCells的UICollectionView。单元格应该相互重叠,但也可以根据它们的位置淡入淡出一点。看到下面的结果:enter image description here

如何避免这些角变得可见? (顶部介于3和4之间,或介于4和5之间,或者最右侧介于5和6之间)。它们应该重叠,但这不会影响图像。

1 个答案:

答案 0 :(得分:1)

为了创建淡入淡出效果,我将使用这样的叠加层:

  • 将原始图像保存在变量中,以便能够针对不同的alpha值重置过程
  • 在当前图像上方绘制与背景颜色相同的形状(颜色alpha应与商品位置成正比)
  • 将结果图像替换为当前图像

我会给你一个例子来更好地说明:

private UIImage baseImage; 

private UIImage ChangeImageColor(UIImage image, nfloat alpha, UIColor color)
{
   var alphaColor = color.ColorWithAlpha(alpha);

   if(baseImage == null)
   {
      baseImage = image;       
   }
   else
   {
      image = baseImage;
   }

   UIGraphics.BeginImageContextWithOptions(image.Size, false, UISCreen.MainScreen.Scale);

   var context = UIGraphics.GetCurrentContext();
   alphaColor.SetFill();

   context.TranslateCTM(0, image.Size.Height);
   context.ScaleCTM(new nfloat(1.0), new nfloat(-1.0));
   context.SetBlendMode(CGBlendMode.Lighten);

   var rect = new CGRect(0, 0, image.Size.Width, image.Size.Height);
   context.DrawImage(rect, image.CGImage);

   context.SetBlendMode(CGBlendMode.SourceAtop);
   context.AddRect(rect);
   context.DrawPath(CGPathDrawingMode.Fill);

   image = UIGraphics.GetImageFromCurrentImageContext();
   UIGraphics.EndImageContext();

   return image;
}