ios - 如何从 ScrollView 中删除 subview

标签 ios swift scrollview

如何删除选定的 ScrollView 模糊效果

创建 ScrollView

 for i in 0 ... totalPage {

        let cardView = Bundle.main.loadNibNamed("cardView", owner: self, options: nil)?[0] as! UIView

        cardView.frame = CGRect(x: CGFloat(i) * CardScroll.frame.size.width, y: CardScroll.frame.origin.y, width: CardScroll.frame.size.width, height: CardScroll.frame.size.height)

        let blureffect = UIBlurEffect(style: UIBlurEffectStyle.light)

        blureffectView = UIVisualEffectView(effect: blureffect)
        blureffectView.frame = CGRect(x: CGFloat(i) * CardScroll.frame.size.width, y: CardScroll.frame.origin.y, width: CardScroll.frame.size.width, height: CardScroll.frame.size.height)

        CardScroll.addSubview(cardView)
        CardScroll.addSubview(blureffectView)

    }

双击 Action 移除模糊效果

func doubleTapped() {
    let pageNumber = CardPage.currentPage

    blureffectView.frame = CGRect(x: CGFloat(pageNumber) * CardScroll.frame.size.width, y: CardScroll.frame.origin.y, width: CardScroll.frame.size.width, height: CardScroll.frame.size.height)

    blureffectView.removeFromSuperview()
}

最佳答案

在 blureffectView.frame 下方添加以下内容

 blureffectView.tag = i ; // Set tag for every view you are adding on scrollview

添加 Action 如下

func doubleTapped(sender:UIButton!){ // Pass sender as UIButton so you can get tag here 

   for subview in scrollView.subviews{ // Loop thorough ScrollView view hierarchy 
       if subview is UIVisualEffectView && subview.tag == sender.tag { // Check if view is type of VisualEffect and tag is equal to the view clicked
           subview.removeFromSuperview()
       }
   }
}

关于ios - 如何从 ScrollView 中删除 subview ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43735944/

相关文章:

ios - 更改 Swift UILabel 的文本

android - 手势检测和 ScrollView 问题

javascript - React Native - 如何测量 ScrollView 中内容的大小?

ios - 以图形方式将对象添加到核心数据

ios - 在 iOS (swift) 应用程序中,注册第二个 Firebase 应用程序无法接收远程通知

ios - UIUserNotificationSettings 的问题

iOS - 使用触摸拖动分隔符调整多个 View 的大小

Swift 协议(protocol)实现 Equatable

swift - 具有 IBInspectable 颜色属性的子类 - 无法覆盖

iOS:scrollToRowAtIndexPath 不显示最后一页的正确顶部索引路径