如何模糊uitableview滚动的部分

时间:2016-08-08 17:53:00

标签: ios swift uitableview

我必须实现一个菜单,就像" example.gif"在以下库link

我使用相同的库来模糊背景。 我已经使用了contentInset,因此前三行显示在底部。

现在,我的问题是当我开始滚动时整个屏幕模糊,而我想模糊屏幕中uitableviewcells滚动的部分。 (最终,一旦第一个细胞到达顶部,整个屏幕就会模糊不清。)

我怎样才能做到这一点。如果在不使用库的情况下有任何解决方法,也欢迎使用。这是代码 -

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, UIScrollViewDelegate{

    @IBOutlet weak var table: UITableView!

    var blurView: DKLiveBlurView!

    var unsortedCountryArray:[String] = []

    override func viewDidLoad() {
        super.viewDidLoad()

//        Do any additional setup after loading the view, typically from a nib.

        let topInset = self.view.frame.height - 120

//        Array to display.
        let countryArray = NSLocale.ISOCountryCodes()

        for countryCode in countryArray {
            let displayNameString = NSLocale.currentLocale().displayNameForKey(NSLocaleCountryCode, value: countryCode)
            if displayNameString != nil {
                unsortedCountryArray.append(displayNameString!)
            }
        }

//        =============setting background==============
//        self.bkgView = UIImageView(frame: self.view.bounds)
//        self.bkgView.image = UIImage(named: "bg1")
//        self.bkgView.contentMode = .ScaleAspectFill
//        self.view.addSubview(self.bkgView)

//        self.blurredBkgView = UIImageView(frame: self.view.bounds)
//        self.blurredBkgView.image = UIImage(named: "bg1")
//        self.blurredBkgView.contentMode = .ScaleAspectFill
//        self.view.addSubview(blurredBkgView)

//        self.blurredBkgView.alpha = 0.0

//
//        blurEffect = UIBlurEffect(style: .Light)
//        visualEffectView = UIVisualEffectView(effect: blurEffect)
//        visualEffectView.frame = self.blurredBkgView.bounds
//        self.visualEffectView.alpha = 0.0
//        self.view.addSubview(self.visualEffectView)

        self.table.backgroundColor = UIColor.clearColor()
        self.table.separatorColor = UIColor.clearColor()
        self.table.contentInset = UIEdgeInsetsMake(topInset, 0, 0, 0)
        self.table.rowHeight = 40

        print("view bounds: \(self.view.bounds)\n and table bounds: \(self.table.bounds)")
        self.blurView = DKLiveBlurView(frame: self.table.bounds)
        self.blurView.originalImage = UIImage(named: "bg1")
        self.blurView.scrollView = table
        self.blurView.setBlurLevel(6.0)
        self.blurView.isGlassEffectOn = true
        self.table.backgroundView = self.blurView


    }

    func scrollViewDidScroll(scrollView: UIScrollView) {
//        let height = CGFloat(scrollView.bounds.size.height)
//        let position = max(scrollView.contentOffset.y, 0.0)
//        let percent = min(position / height, 1.0)
//        self.blurredBkgView.alpha = percent;

//        print("scrollview bounds: \(scrollView.bounds)")
    }

    func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
//        cell.backgroundView = self.blurView

    }

    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return unsortedCountryArray.count
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("cell")!
        cell.textLabel?.text = unsortedCountryArray[indexPath.row]
        cell.textLabel?.textColor = UIColor.blueColor()
        cell.backgroundColor = UIColor.clearColor()
        cell.selectionStyle = .None
        return cell
    }



    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}  

滚动时,这么多代码模糊不清。

1 个答案:

答案 0 :(得分:1)

要考虑您的内容插入,您需要更改为blurView提供的框架

例如

let contentInset = CGFloat(/*Your content inset*/)
let blurFrame = CGRect(x: 0, y: contentInset, width: tableView.frame.width, height: tableView.frame.height - contentInset)
self.blurView = DKLiveBlurView(frame: blurFrame)
编辑:旧答案

您似乎正在为DKLiveBlurView使用边界而不是框架。这将使您模糊视图从屏幕的左上角开始(视图框架的原点)

尝试:

self.blurView = DKLiveBlurView(frame: self.table.frame)

而不是

self.blurView = DKLiveBlurView(frame: self.table.bounds)