【问题标题】:Animate alpha of a subview in a collection view cell集合视图单元格中子视图的动画 alpha
【发布时间】:2020-07-11 06:16:27
【问题描述】:

我有一个集合视图单元格,其中有一个UIVisualEffectView 作为子视图。此视图最初位于alpha = 0.0,当用户点击单元格时,它变为alpha = 1.0

let blurEffect = UIBlurEffect(style: .light)
blurEffectView.effect = blurEffect
blurEffectView.frame = cardView.bounds
blurEffectView.alpha = 0.0
cardView.addSubview(blurEffectView)
blurEffectView.snp.makeConstraints { (make) in
    make.edges.equalToSuperview()
}

我正在做这样的动画,如下所示:

DispatchQueue.main.async {
    UIView.animate(withDuration: 0.5) {
           cell.blurEffectView.alpha = 1.0
     }
}

问题是模糊视图变得可见但没有任何动画。知道我做错了什么吗?

编辑: 感谢@TylerTheCompiler 的回答,我现在已经更改了模糊效果本身的 alpha 动画。问题是动画一直没有发生。

我在func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath)中使用cell.blurEffectView.fadeInEffect()

func fadeInEffect(_ style:UIBlurEffect.Style = .light, withDuration duration: TimeInterval = 1.0) {
        if #available(iOS 10.0, *) {
            let animator = UIViewPropertyAnimator(duration: duration, curve: .easeIn) {
                self.effect = UIBlurEffect(style: style)
            }
            animator.startAnimation()
        }else {
            // Fallback on earlier versions
            UIView.animate(withDuration: duration) {
                self.effect = UIBlurEffect(style: style)
            }
        }
    }

谢谢!

【问题讨论】:

标签: ios swift uicollectionview uiviewanimation


【解决方案1】:

要动画模糊效果,您应该将效果设置为 nil:

blurEffectView.effect = nil

然后执行动画:

UIView.animate(withDuration: 0.5) {
    blurEffectView.effect = UIBlurEffect(style: .light)
}

【讨论】:

  • @JoanCardona 你在动画之前把 effect 设为 nil 了吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多