我正在尝试在其 iOS 应用程序上复制 eBay 的列表菜单,用户可以在其中滚动浏览列表的不同图像。我注意到背景颜色是纯色,复制每个图像中背景的周围颜色。
当我滚动浏览具有不同环绕背景的不同图像(不同页面)时,实际背景UICollectionView
改变以某种方式反映它。
这就是我的意思:
正如您在第一张图片中看到的,背景是浅色的,有点类似于图像的背景。当我滚动到第二张图片的一半时,背景变暗。
Finally:
我的设置类似:
Using 主色 https://github.com/indragiek/DominantColor,我能够设置UICollectionView
每个的背景颜色UIImage
的主色。当用户在第一页和第二页之间滚动一半时,UICollectionView
背景颜色设置为第二页UIImage
的主色。
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
{
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as UICollectionViewCell
let frame: CGRect = CGRect(x: 0, y: 0, width: imagesView.frame.size.width, height: imagesView.frame.size.height)
let subView: UIImageView = UIImageView(frame: frame)
subView.image = imagesArray[indexPath.row].image
subView.contentMode = UIViewContentMode.scaleAspectFit
if colorsArray.count > 0
{
// Set background dominant color of first image
collectionView.backgroundColor = colorsArray[0]
}
cell.addSubview(subView)
return cell
}
func scrollViewDidScroll(_ scrollView: UIScrollView)
{
let scrollValue = (scrollView.contentOffset.x / UIScreen.main.bounds.width)
let pageWidth: CGFloat = imagesCollectionView.bounds.size.width
let currentPage: Int = Int( floor( (imagesCollectionView.contentOffset.x - pageWidth / 2) / pageWidth) + 1)
if scrollValue != 1
{
UIView.animate(withDuration: 1, animations: {
self.imagesCollectionView.backgroundColor = self.colorsArray[currentPage]
})
}
else
{
UIView.animate(withDuration: 1, animations: {
self.imagesCollectionView.backgroundColor = self.colorsArray[currentPage]
})
}
}
但是,我在如何缓慢地从当前背景颜色过渡到下一个背景颜色方面遇到了困难soon当用户开始滚动时,如上面的第二张图所示。
目前的实现方式如上scrollViewDidScroll
,当页面在下一页之间滚动一半时,它开始淡入下一种颜色,并且带有1 second动画,无论下一页是显示一半还是完全显示,它都会在 1 秒内变成该颜色。
我怎样才能实现这个目标?