如何从effectView

时间:2018-03-17 04:01:01

标签: ios swift image uiimageview uivisualeffectview

如何从imageView中删除effectView?目前,当用户失去Internet连接时,我正在制作动画。但是,一旦建立连接,我无法弄清楚如何删除imageVieweffectView被动画化,但图像仍然在视图上。我尝试使用removeFromSuperview(),但很可能我没有以正确的方式使用它。

func internetConnection() {
    let image = UIImage(named: "nointernet.png")
    let imageView = UIImageView(image: image!)

    if reach.connection == .none {

        imageView.frame = CGRect(x: 0, y: 0, width: 100, height: 100)
        imageView.center = self.effectView.center
        self.effectView.contentView.addSubview(imageView)
        effectView.isUserInteractionEnabled = true

        imageView.alpha = 0

        UIView.animate(withDuration: 0.4) {
            self.effectView.effect = self.effect
            imageView.alpha = 1
            imageView.transform = CGAffineTransform.identity
        }
    }else{
        effectView.isUserInteractionEnabled = false
        UIView.animate(withDuration: 0.3, animations: {
            imageView.transform = CGAffineTransform.init(scaleX: 1.3, y: 1.3)
            imageView.alpha = 0

            self.effectView.effect = nil

        }) { (Bool) in
            imageView.removeFromSuperview()
        }
    }
}

1 个答案:

答案 0 :(得分:1)

问题是当连接回来时,您创建一个新的图像视图,然后尝试删除新的图像视图。但是您需要删除现有的图像视图。更新您的代码以访问func internetConnection() { if reach.connection == .none { let image = UIImage(named: "nointernet.png")! let imageView = UIImageView(image: image) imageView.frame = CGRect(x: 0, y: 0, width: 100, height: 100) imageView.center = self.effectView.center imageView.alpha = 0 self.effectView.contentView.addSubview(imageView) effectView.isUserInteractionEnabled = true UIView.animate(withDuration: 0.4) { self.effectView.effect = self.effect imageView.alpha = 1 imageView.transform = CGAffineTransform.identity } } else { if let imageView = self.effectView.contentView.subviews.last as? UIImageView { effectView.isUserInteractionEnabled = false UIView.animate(withDuration: 0.3, animations: { imageView.transform = CGAffineTransform.init(scaleX: 1.3, y: 1.3) imageView.alpha = 0 self.effectView.effect = nil }) { (finished) in imageView.removeFromSuperview() } } } } 中的现有图片视图并将其删除。

internetConnection

唯一的潜在问题。当连接丢失时,确保连续两次调用print。否则,您最终会将多个图像视图添加到effectView。