2014-12-04 26 views
0

我一直在這個指南[https://www.youtube.com/watch?v=qaLiZgUK2T0]爲創建一個快捷工具欄菜單中的以下內容:代碼編譯,用了錯誤的......我一定在簡單的東西... 。菜單沒有出現。請協助我找出這個問題。雨燕側欄菜單中的規定,但沒有出現

ViewController.swift:

爲菜單中的代碼在三個文件下面列出

import UIKit 
class ViewController: UIViewController, SideBarDelegate { 

var sideBar:SideBar = SideBar() 

override func viewDidLoad() { 
    super.viewDidLoad() 

    sideBar = SideBar(sourceView: self.view, menuItems: ["first item","second item","third item"]) 

} 

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

func sideBarDidSelectButtonAtIndex(index: Int) { 

}  
}   

SideBarTableViewController.swift:

import UIKit 

protocol SideBarTableViewControllerDelegate{ 
func sideBarControlDidSelectRow(indexPath:NSIndexPath); 
} 

class SideBarTableViewController: UITableViewController { 

var delegate:SideBarTableViewControllerDelegate? 
var tableData:Array<String> = [] 


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

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

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    var cell:UITableViewCell? = tableView.dequeueReusableCellWithIdentifier("Cell") as? UITableViewCell 

    if cell == nil{ 
     cell = UITableViewCell(style :UITableViewCellStyle.Default, reuseIdentifier: "Cell") 
     // Configure the cell... 
     cell!.backgroundColor = UIColor.clearColor() 
     cell!.textLabel.textColor = UIColor.darkTextColor() 

     let selectedView:UIView = UIView(frame: CGRect (x: 0, y:0, width: cell!.frame.size.width, height: cell!.frame.size.height)) 
     selectedView.backgroundColor = UIColor.blackColor().colorWithAlphaComponent(0.3) 

     cell!.selectedBackgroundView = selectedView 

    } 

    cell!.textLabel.text = tableData[indexPath.row] 

    return cell! 
} 

override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat{ 
    return 45.0 
} 

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 
    delegate?.sideBarControlDidSelectRow(indexPath) 
} 

} 

SideBar.swift

import UIKit 

@objc protocol SideBarDelegate{ 
    func sideBarDidSelectButtonAtIndex(index:Int) 
    optional func sideBarWillClose() 
    optional func sideBarWillOpen() 
} 

class SideBar: NSObject, SideBarTableViewControllerDelegate { 
let barWidth : CGFloat = 150; 
let sideBarTableViewTopInset:CGFloat = 64.0; 
let sideBarContainerView: UIView = UIView() 
let sideBarTableViewController: SideBarTableViewController = SideBarTableViewController() 
let orginView : UIView! 
var animator: UIDynamicAnimator! 
var delegate:SideBarDelegate? 
var isSideBarOpen:Bool = false 

override init(){ 
    super.init() 
} 

init(sourceView: UIView, menuItems:Array<String>){ 
    super.init() 
    orginView = sourceView 
    sideBarTableViewController.tableData = menuItems 

    animator = UIDynamicAnimator(referenceView: orginView) 

    let showGestureRecognizer:UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: "handleSwipe:") 
    showGestureRecognizer.direction = UISwipeGestureRecognizerDirection.Right 
    orginView.addGestureRecognizer(showGestureRecognizer) 

    let hideGesturerecognizer:UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: "handleSwipe:") 
    hideGesturerecognizer.direction = UISwipeGestureRecognizerDirection.Left 
    orginView.addGestureRecognizer(hideGesturerecognizer) 

} 

func setupSideBar(){ 
    sideBarContainerView.frame = CGRectMake(-barWidth - 1,orginView.frame.origin.y, barWidth, orginView.frame.size.height) 
    sideBarContainerView.backgroundColor = UIColor.clearColor() 
    sideBarContainerView.clipsToBounds = false 

    orginView.addSubview(sideBarContainerView) 

    let blurView:UIVisualEffectView = UIVisualEffectView(effect: 
     UIBlurEffect(style: UIBlurEffectStyle.Light)) 
    blurView.frame = sideBarContainerView.bounds 
    sideBarContainerView.addSubview(blurView) 

    sideBarTableViewController.delegate = self 
    sideBarTableViewController.tableView.frame = sideBarContainerView.bounds 
    sideBarTableViewController.tableView.clipsToBounds = false 
    sideBarTableViewController.tableView.separatorStyle = UITableViewCellSeparatorStyle.None 
    sideBarContainerView.backgroundColor = UIColor.clearColor() 
    sideBarTableViewController.tableView.scrollsToTop = false 

    sideBarTableViewController.tableView.contentInset = UIEdgeInsetsMake(sideBarTableViewTopInset, 0,0,0) 

    sideBarTableViewController.tableView.reloadData() 

    sideBarContainerView.addSubview(sideBarTableViewController.tableView) 

} 

func handleSwipe(recognizer:UISwipeGestureRecognizer){ 
    if (recognizer.direction == UISwipeGestureRecognizerDirection.Left){ 
     showSideBar(false) 
     delegate?.sideBarWillClose?() 
    }else{ 
     showSideBar(true) 
     delegate?.sideBarWillClose?() 
    } 
} 

func showSideBar(shouldOpen:Bool){ 
    animator.removeAllBehaviors() 
    isSideBarOpen = shouldOpen 

    let gravityX:CGFloat = (shouldOpen) ? 0.5 : -0.5 
    let magnitude:CGFloat = (shouldOpen) ? 20 : -20 
    let boundryX: CGFloat = (shouldOpen) ? barWidth : -barWidth - 1 

    let gravityBehavior:UIGravityBehavior = UIGravityBehavior(items: [sideBarContainerView]) 
    gravityBehavior.gravityDirection = CGVectorMake(gravityX, 0) 

    let collisonBehavior:UICollisionBehavior = UICollisionBehavior(items: [sideBarContainerView]) 

    collisonBehavior.addBoundaryWithIdentifier("sideBarBoundary", fromPoint: CGPointMake(boundryX, 20), toPoint: CGPointMake(boundryX, orginView.frame.size.height)) 

    animator.addBehavior(collisonBehavior) 

    let pushBehavior:UIPushBehavior = UIPushBehavior(items: [sideBarContainerView], mode: UIPushBehaviorMode.Instantaneous) 
    animator.addBehavior(pushBehavior) 

    let sideBarBehavior:UIDynamicItemBehavior = UIDynamicItemBehavior(items: [sideBarContainerView]) 
    sideBarBehavior.elasticity = 0.3 
    animator.addBehavior(sideBarBehavior) 
} 

func sideBarControlDidSelectRow(indexPath: NSIndexPath) { 
    delegate?.sideBarDidSelectButtonAtIndex(indexPath.row) 
} 
} 
+0

只是爲了讓你知道,術語是'compiles'不符合'。 – erdekhayser 2014-12-04 13:29:03

+0

感謝,錯字已得到糾正。 – 2014-12-05 05:28:21

回答

0

當我下載源代碼時,我經歷並調試了項目文件,我必須做的所有工作是對SideBarTableViewController.swift進行一些小改動,並且所有工作都適用於我。這是它:

import UIKit 


protocol SideBarTableViewControllerDelegate{ 
    func sideBarControlDidSelectRow(indexPath:NSIndexPath) 
} 

class SideBarTableViewController: UITableViewController { 

    var delegate:SideBarTableViewControllerDelegate? 
    var tableData:Array<String> = [] 


    // MARK: - Table view data source 

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int { 

     return 1 
    } 

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 

     return tableData.count 
    } 

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
     var cell = tableView.dequeueReusableCellWithIdentifier("Cell") as? UITableViewCell 

     if cell == nil{ 
      cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "Cell") 
      // Configure the cell... 

      cell?.backgroundColor = UIColor.clearColor() 
      cell?.textLabel?.textColor = UIColor.darkTextColor() 

      let selectedView:UIView = UIView(frame: CGRect(x: 0, y: 0, width: cell!.frame.size.width, height: cell!.frame.size.height)) 
      selectedView.backgroundColor = UIColor.blackColor().colorWithAlphaComponent(0.3) 

      cell!.selectedBackgroundView = selectedView 
     } 
     cell?.textLabel?.text = tableData[indexPath.row] 
     return cell! 

    } 

    override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { 
     return 45.0 
    } 

    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 
     delegate?.sideBarControlDidSelectRow(indexPath) 

    } 

} 
+0

我用你這裏的代碼,我仍然有問題。難道還有一個隱藏的佈局設置的地方,可能會導致這個問題? – 2014-12-05 04:48:13

+0

不確定,但我把我的工作演示放在github https://github.com/iMac239/BlurrySideBar – Ian 2014-12-05 05:06:34

+0

好吧,演示工作正常,所以它不是我的電腦問題。如果我能找出有什麼區別,我會讓你知道的。再次感謝所有的幫助。 – 2014-12-05 05:38:02