2016-08-08 44 views
1

我要實現的菜單,就像在以下庫中link如何模糊的UITableView的只有部分入門滾動

我使用這個相同的庫虛化背景的「example.gif」。 我已經使用了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

要考慮內容的插圖,你需要你提供的框架更改爲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) 
+0

沒有,它模糊了整個背景。 –

+0

哦,我明白你的意思了!您已將單元向下移動,這是您想要模糊的唯一部分。我現在要修改我的答案 –

+0

我認爲它現在可以工作,但仍然是一樣的。 :/ –