【问题标题】:How to make the left and right image opacity down?如何降低左右图像的不透明度?
【发布时间】:2018-04-05 11:35:53
【问题描述】:

如何降低左右图像的不透明度?我正在使用 CollectionView 来显示图像中的所有数据。我想降低中心左右视图的不透明度。我不知道该怎么做。我可以在 viewDidLayoutSubviews 中做所有事情。请解决此问题。这是我的代码:

var arrHeading = ["Total Visitors in last 30 Days", "Total Departments", "Total Employees"]

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
    {
        return arrHeading.count
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "VisitorDashboardCell", for: indexPath) as! VisitorDashboardCell

        cell.imgBG.layer.cornerRadius = 10
        cell.imgBG.layer.masksToBounds = true

        //Set Gradient Color to CollectionViewCell
        cell.layer.shadowColor = UIColor.black.cgColor
        cell.layer.shadowOffset = CGSize(width: 0, height: 0)
        cell.layer.shadowRadius = 2.0;
        cell.layer.shadowOpacity = 0.9;
        cell.layer.cornerRadius = 10
        cell.layer.masksToBounds = false
        cell.imgBG.layer.shadowPath = UIBezierPath(rect: CGRect(x: -8, y: -18, width: cell.frame.size.width + 8 , height: cell.frame.size.height + 18)).cgPath
        cell.layoutIfNeeded()



        cell.imgBG.image = UIImage.init(named: arrImage[indexPath.row])
        cell.imgIcon.image = UIImage.init(named: arrIcon[indexPath.row])
        cell.lblHeading.text = arrHeading[indexPath.row]
        cell.lblNum.text = arrNum[indexPath.row]

        return cell
    }

    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()

        let middleValue = round(Double(arrHeading.count / 2))

        print("Middle Value \(middleValue)")


        if arrHeading.count < Int(middleValue)
        {
            var leftArray = [Int]()

            for i in 0..<(Int(middleValue) - 1) {
                leftArray.append(i)

            }


            print("Left Array \(leftArray)")
        }
        else if arrHeading.count > Int(middleValue)
        {
            var rightArray = [Int]()

            for i in Int(middleValue)..<arrHeading.count
            {
                rightArray.append(i)
            }

            print("Right Array \(rightArray)")
        }
        else
        {
            //Show middle element of the collection view
            let selectedIndex = IndexPath(item: Int(middleValue), section: 0)
            self.collectionView.scrollToItem(at: selectedIndex, at: .centeredHorizontally, animated: false)
        }

    }

【问题讨论】:

  • 我会在您选择中间元素的位置更改元素 alpha。
  • 我不会在viewDidLayoutSubviews 中编写所有代码。我要做的是实现 didScroll 委托,然后相应地更改 alpha。
  • 你能告诉我滚动后可见单元格数组中显示的内容吗?

标签: ios swift uicollectionview


【解决方案1】:

在你的委托上尝试这样的事情:

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        UIView.animate(withDuration: 0.5) {
            cell.alpha = cell.isSelected ? 1 : 0.5
        }
    }

【讨论】:

  • didSelectItemAt 函数在单元格被选中时触发,而不是在它在 collectionView 中居中时触发。
  • 当他居中时,他可以做类似的事情。
猜你喜欢
  • 2014-05-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-02-07
  • 2017-08-29
  • 1970-01-01
  • 2018-01-31
  • 1970-01-01
相关资源
最近更新 更多