2016-01-13 75 views
3

我正在創建一個聊天應用程序,其中有一些預定義的常見問題與不同的類別,當用戶單擊按鈕時將滑動。以下是圖像,應該如何看待:創建pagingmenucontroller與視圖在快速滑動

[最後輸出]

我使用PagingMenucontroller庫從下面的鏈接:我面臨 https://github.com/kitasuke/PagingMenuController

問題的看法犯規佔據的寬度裝置。 uitableview的滾動不起作用。 也免費和商業部分的數據被加載。

以下是類ChatViewcontroller代碼這是在圖像上面顯示的父類:

import Foundation 
import UIKit 
import PagingMenuController 

class ChatViewController: UIViewController, UITextFieldDelegate//, UITableViewDelegate, UITableViewDataSource 
{ 

    @IBOutlet var askQuestionTxtField: UITextField! 
    @IBOutlet var bottomTypingView: UIView! 

    @IBOutlet var bottomQuestionsView: UIView! 
    @IBOutlet var chatView: UIView! 
    var questionViewYPos:CGFloat = 0.0 
    var keyboardFrame:CGRect = CGRect(x: 0, y: 0, width: 0, height: 0) 
    @IBOutlet var questionOpenBtn: UIButton! 

    @IBOutlet var questionViewBottomConstraint: NSLayoutConstraint! 
    var isQuestionViewOpen:Bool = false 
    var isKeyboardVisible:Bool = false 
    var timer = NSTimer() 
    var questionCategoriesArray:[String] = ["FREE", "BUSINESS", "RELATIONSHIPS", "CAREER", "OTHERS"] 
    var questionsDictionary:Dictionary<String,[String]> = [:] 

    @IBAction func onQuestionViewBtnClick(sender: AnyObject) 
    { 
     if(isKeyboardVisible) 
     { 
      animateViewMoving(false, moveValue: questionViewYPos-bottomTypingView.frame.size.height) 
      bottomQuestionsView.hidden = false 
      askQuestionTxtField.resignFirstResponder() 

      timer = NSTimer.scheduledTimerWithTimeInterval(NSTimeInterval(0.4), target: self, selector: "openQuestionView", userInfo: nil, repeats: false) 
     } 
     else 
     { 
      isQuestionViewOpen = !isQuestionViewOpen 
      print("isQuestionViewOpen",isQuestionViewOpen) 
      print("bottomQuestionsView height :: ",bottomQuestionsView.frame.size.height) 
      if(isQuestionViewOpen) 
      { 
       questionViewAnimation(true, moveValue: (questionViewYPos)) 
       animateViewMoving(true, moveValue: (questionViewYPos)) 
      } 
      else 
      { 

       animateViewMoving(false, moveValue: (questionViewYPos)) 
      } 
     } 
    } 

    func openQuestionView() 
    { 
     isQuestionViewOpen = !isQuestionViewOpen 
     questionViewAnimation(true, moveValue: (questionViewYPos)) 
     animateViewMoving(true, moveValue: (questionViewYPos)) 
    } 

    @IBAction func onFreeBtnClick(sender: AnyObject) 
    { 
     print("onFreeBtnClick") 
    } 
    @IBAction func onBusinessBtnClick(sender: AnyObject) 
    { 
     print("onBusinessBtnClick") 
    } 
    @IBAction func onRelationshipBtnClick(sender: AnyObject) 
    { 
     print("onRelationshipBtnClick") 
    } 
    @IBAction func onCareerBtnClick(sender: AnyObject) 
    { 
     print("onCareerBtnClick") 
    } 
    @IBAction func onOthersBtnClick(sender: AnyObject) 
    { 
     print("onOthersBtnClick") 
    } 
    var freeItems: [String] = ["What Shall I keep in mind about money 1", "Heart shall I keep in mind", "What shall I keep in mind about money \n matters today?","We keep in mind about money 2","We keep in mind about money 3", "We keep in mind about money 4", "Heart shall I keep in mind 2", "Heart shall I keep in mind 3"] 

    var businessItems: [String] = ["What is my Business Future 1", "What is my Business Future 2", "What is my Business Future 3 \n and other matters today?","What is my Business Future 4","What is my Business Future 5", "What is my Business Future 6", "What is my Business Future 7", "What is my Business Future 8","What is my Business Future 9", "What is my Business Future 10"] 

    var relationShipsItems: [String] = ["How Will be my RelationShip this Year 1", "How Will be my RelationShip this Year 2", "How Will be my RelationShip today \n and other matters","How Will be my RelationShip this Year 3","How Will be my RelationShip this week 4", "How Will be my RelationShip this Year 5", "How Will be my RelationShip this Year 6", "How Will be my RelationShip tomorrow 7", "How Will be my RelationShip this Year 8"] 

    var careerItems: [String] = ["How will be my career 1", "How will be my career 2", "How will be my career 3","We keep in mind about money 2","How will be my career 4", "How will be my career 5", "How will be my career 6", "How will be my career 7","How will be my career 8","How will be my career 9","How will be my career 10","How will be my career 11","How will be my career 12","How will be my career 13", "How will be my career 14","How will be my career 15"] 

    var otherItems: [String] = ["Other Future Related Questions 1", "Other Future Related Questions 2", "Other Future Related Questions 3", "Other Future Related Questions 4", "Other Future Related Questions 5", "Other Future Related Questions 6", "Other Future Related Questions 7", "Other Future Related Questions 8", "Other Future Related Questions 9", "Other Future Related Questions 10", "Other Future Related Questions 11", "Other Future Related Questions 12", "Other Future Related Questions 13", "Other Future Related Questions 14", "Other Future Related Questions 15", "Other Future Related Questions 16", "Other Future Related Questions 17", "Other Future Related Questions 18", "Other Future Related Questions 19", "Other Future Related Questions 20"] 
    var questionsVariableArray:[[String]]! 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     //NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillShow:", name: UIKeyboardWillShowNotification) 

     NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillShow:", name: UIKeyboardDidShowNotification, object: nil) 
     NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillHide:", name: UIKeyboardWillHideNotification, object: nil) 
     bottomQuestionsView.userInteractionEnabled = true 
     askQuestionTxtField.delegate = self 
     questionViewYPos = self.chatView.frame.size.height-(self.chatView.frame.size.height - bottomQuestionsView.frame.size.height) 
     print("bottomQuestionsView.frame.origin.y",bottomQuestionsView.frame.origin.y) 
     print("pos :: ",questionViewYPos) 

     self.questionsVariableArray = [self.freeItems, self.businessItems, self.relationShipsItems, self.careerItems, self.otherItems] 
     print("self.questionsVariableArray ::",self.questionsVariableArray) 
     print("self.freeItems ::",self.freeItems) 
     print("self.businessItems ::",self.businessItems) 
     print("self.relationShipsItems ::",self.relationShipsItems) 
     print("self.careerItems ::",self.careerItems) 
     print("self.otherItems ::",self.otherItems) 

     createQuestionsDictionary() 

    } 

    func createQuestionsDictionary() 
    { 
     for(var i:Int = 0;i < self.questionCategoriesArray.count; i++) 
     { 
      print("self.questionCategoriesArray[i] :: ",self.questionCategoriesArray[i]) 
      print("self.questionsVariableArray[i] :: ",self.questionsVariableArray[i]) 
      questionsDictionary[self.questionCategoriesArray[i]] = self.questionsVariableArray[i] 
     } 

     print("questionsDictionary :: ",questionsDictionary) 
     loadCategoriesQuestionsView() 
    } 

    func loadCategoriesQuestionsView() 
    { 
     var viewControllersArray:[ChatQuestionsViewController] = [] 
     /*for(var i:Int = 0; i < self.questionCategoriesArray.count; i++) 
     { 
      let _viewController = ChatQuestionsViewController.init(nibName: "ChatQuestionsViewController", bundle: nil) 

      _viewController.title = self.questionCategoriesArray[i] 
      Constants.questionCategoriesArray = self.questionCategoriesArray 
      Constants.questionsDictionary = self.questionsDictionary 

      _viewController.setQuestionsCategoryArray(self.questionCategoriesArray, _questionsDictionary: self.questionsDictionary, _categoryName: self.questionCategoriesArray[i]) 
      viewControllersArray.append(_viewController) 
      _viewController.view.translatesAutoresizingMaskIntoConstraints = true 
     }*/ 

     let _freeviewController = ChatQuestionsViewController.init(nibName: "ChatQuestionsViewController", bundle: nil) 

     _freeviewController.title = self.questionCategoriesArray[0] 
     Constants.questionCategoriesArray = self.questionCategoriesArray 
     Constants.questionsDictionary = self.questionsDictionary 

     viewControllersArray.append(_freeviewController) 
     //_freeviewController.view.translatesAutoresizingMaskIntoConstraints = true 

     let _businessviewController = ChatQuestionsViewController.init(nibName: "ChatQuestionsViewController", bundle: nil) 

     _businessviewController.title = self.questionCategoriesArray[1] 
     Constants.questionCategoriesArray = self.questionCategoriesArray 
     Constants.questionsDictionary = self.questionsDictionary 

     viewControllersArray.append(_businessviewController) 
     //_freeviewController.view.translatesAutoresizingMaskIntoConstraints = true 

     let _relationshipController = ChatQuestionsViewController.init(nibName: "ChatQuestionsViewController", bundle: nil) 

     _relationshipController.title = self.questionCategoriesArray[2] 
     Constants.questionCategoriesArray = self.questionCategoriesArray 
     Constants.questionsDictionary = self.questionsDictionary 

     viewControllersArray.append(_relationshipController) 
     //_freeviewController.view.translatesAutoresizingMaskIntoConstraints = true 

     let _careerController = ChatQuestionsViewController.init(nibName: "ChatQuestionsViewController", bundle: nil) 

     _careerController.title = self.questionCategoriesArray[3] 
     Constants.questionCategoriesArray = self.questionCategoriesArray 
     Constants.questionsDictionary = self.questionsDictionary 

     viewControllersArray.append(_careerController) 
     //_freeviewController.view.translatesAutoresizingMaskIntoConstraints = true 

     let _otherController = ChatQuestionsViewController.init(nibName: "ChatQuestionsViewController", bundle: nil) 

     _otherController.title = self.questionCategoriesArray[4] 
     Constants.questionCategoriesArray = self.questionCategoriesArray 
     Constants.questionsDictionary = self.questionsDictionary 

     viewControllersArray.append(_otherController) 
     //_freeviewController.view.translatesAutoresizingMaskIntoConstraints = true 



     let options = PagingMenuOptions() 
     options.menuItemMargin = 5 
     //options.menuDisplayMode = .SegmentedControl 
     let pagingMenuController = PagingMenuController(viewControllers: viewControllersArray , options: options) 
     self.addChildViewController(pagingMenuController) 
     self.bottomQuestionsView.addSubview(pagingMenuController.view) 

     //self.bottomQuestionsView.translatesAutoresizingMaskIntoConstraints = true 
     //self.bottomQuestionsView.translatesAutoresizingMaskIntoConstraints = true 
     pagingMenuController.didMoveToParentViewController(self) 

     if _freeviewController.isViewLoaded() 
     { 
      // viewController is visible 
      _freeviewController.setQuestionsCategoryArray(self.questionCategoriesArray, _questionsDictionary: self.questionsDictionary, _categoryName: self.questionCategoriesArray[0]) 
      //_freeviewController.chatTableView.reloadData() 
     } 
     if _businessviewController.isViewLoaded() 
     { 
      // viewController is visible 
      _businessviewController.setQuestionsCategoryArray(self.questionCategoriesArray, _questionsDictionary: self.questionsDictionary, _categoryName: self.questionCategoriesArray[1]) 
      //_businessviewController.chatTableView.reloadData() 

     } 
     if _relationshipController.isViewLoaded() 
     { 
      // viewController is visible 
      _relationshipController.setQuestionsCategoryArray(self.questionCategoriesArray, _questionsDictionary: self.questionsDictionary, _categoryName: self.questionCategoriesArray[2]) 
      //_relationshipController.chatTableView.reloadData() 
     } 
     if _careerController.isViewLoaded() 
     { 
      // viewController is visible 
      _careerController.setQuestionsCategoryArray(self.questionCategoriesArray, _questionsDictionary: self.questionsDictionary, _categoryName: self.questionCategoriesArray[3]) 
      //_careerController.chatTableView.reloadData() 
     } 
     if _otherController.isViewLoaded() 
     { 
      // viewController is visible 
      _otherController.setQuestionsCategoryArray(self.questionCategoriesArray, _questionsDictionary: self.questionsDictionary, _categoryName: self.questionCategoriesArray[4]) 
      //_otherController.chatTableView.reloadData() 
     } 
    } 

    func textFieldDidBeginEditing(textField: UITextField) 
    { 
     print("keyboardFrame.size.height",self.view.frame.origin.y) 
     //animateViewMoving(true, moveValue: questionViewYPos-bottomTypingView.frame.size.height) 

     if(self.view.frame.origin.y < 0.0) 
     { 
      animateViewMoving(false, moveValue: (questionViewYPos)) 
      timer = NSTimer.scheduledTimerWithTimeInterval(NSTimeInterval(0.4), target: self, selector: "update", userInfo: nil, repeats: false) 
      isQuestionViewOpen = !isQuestionViewOpen 
      print("isQuestionViewOpen ###",isQuestionViewOpen) 
     } 
     else 
     { 
      animateViewMoving(true, moveValue: questionViewYPos-bottomTypingView.frame.size.height) 
      bottomQuestionsView.hidden = true 
     } 


    } 
    func update() { 
     // Something cool 
     animateViewMoving(true, moveValue: questionViewYPos-bottomTypingView.frame.size.height) 
     bottomQuestionsView.hidden = true 
    } 

    func textFieldDidEndEditing(textField: UITextField) 
    { 
     //animateViewMoving(false, moveValue: questionViewYPos-bottomTypingView.frame.size.height) 
     textField.resignFirstResponder() 
    } 

    func textFieldShouldReturn(textField: UITextField) -> Bool 
    { 
     //isQuestionViewOpen = !isQuestionViewOpen 
     animateViewMoving(false, moveValue: questionViewYPos-bottomTypingView.frame.size.height) 
     bottomQuestionsView.hidden = false 
     //bottomQuestionsView.translatesAutoresizingMaskIntoConstraints = true 
     textField.resignFirstResponder() 

     return true 
    } 

    /*func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int 
    { 
     return self.freeItems.count; 
    } 

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell 
    { 
     //var cell:UITableViewCell = self.chatTableView.dequeueReusableCellWithIdentifier("cell")! as UITableViewCell 
     let cellIdentifier = "cell" 
     let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! ChatCellView 

     let cellTxtString = freeItems[indexPath.row] 

     cell.questionNoLbl.text = String(indexPath.row + 1) 
     cell.questionLbl.text = cellTxtString 
     return cell 
    } 

    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) 
    { 

    } 

    func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
     // #warning Incomplete implementation, return the number of sections 
     return 1 
    }*/ 

    func animateViewMoving (up:Bool, moveValue :CGFloat){ 
     let movementDuration:NSTimeInterval = 0.3 
     let movement:CGFloat = (up ? -moveValue : moveValue) 
     UIView.beginAnimations("animateView", context: nil) 
     UIView.setAnimationBeginsFromCurrentState(true) 
     UIView.setAnimationDuration(movementDuration) 
     self.view.frame = CGRectOffset(self.view.frame, 0, movement) 
     UIView.commitAnimations() 
    } 

    func questionViewAnimation(up:Bool, moveValue: CGFloat) 
    { 
     let movementDuration:NSTimeInterval = 0.3 
     let movement:CGFloat = (up ? -moveValue : moveValue) 
     UIView.beginAnimations("animateView", context: nil) 
     UIView.setAnimationBeginsFromCurrentState(true) 
     UIView.setAnimationDuration(movementDuration) 
     self.bottomQuestionsView.frame = CGRectOffset(self.bottomQuestionsView.frame, 0, movement) 

     UIView.commitAnimations() 
    } 


    func keyboardWillShow(notification: NSNotification) { 
     keyboardFrame = (notification.userInfo![UIKeyboardFrameEndUserInfoKey] as! NSValue).CGRectValue() 
     print("keyboardFrame",keyboardFrame.height) 
     isKeyboardVisible = true 
     // do stuff with the frame... 
    } 

    func keyboardWillHide(notification: NSNotification) { 
     keyboardFrame = (notification.userInfo![UIKeyboardFrameEndUserInfoKey] as! NSValue).CGRectValue() 
     print("keyboardFrame",keyboardFrame.height) 
     isKeyboardVisible = false 
     // do stuff with the frame... 
    } 


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


} 

以下是用於連接到XIB具有表視圖聊天問題視圖控制器的代碼:

import UIKit 
class ChatQuestionsViewController: UIViewController, UITableViewDelegate, UITableViewDataSource 
{ 
    @IBOutlet var chatTableView: UITableView! 

    var categoryArray:[String]! 
    var categoryName:String = "" 
    var questionsDictionary:Dictionary<String,[String]> = [:] 
    var categoryQuestion:[String] = [] 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     print("self.chatTableView :: ",chatTableView) 

     self.chatTableView.registerNib(UINib(nibName: "ChatQuestionCellRowView", bundle: nil), forCellReuseIdentifier: "cell") 

     self.categoryArray = Constants.questionCategoriesArray 
     self.questionsDictionary = Constants.questionsDictionary 

     self.view.autoresizingMask = [UIViewAutoresizing.FlexibleWidth, UIViewAutoresizing.FlexibleHeight] 

    } 
    override func awakeFromNib() { 
     super.awakeFromNib() 
     // Initialization code 

    } 


    func setQuestionsCategoryArray(_categoryArray: [String], _questionsDictionary:Dictionary<String,[String]>, _categoryName:String) 
    { 
     //self.categoryArray = _categoryArray 
     self.categoryName = _categoryName 
     //self.questionsDictionary = _questionsDictionary 
     //self.chatTableView.delegate = self 
     //self.chatTableView.dataSource = self 
     self.chatTableView.reloadData() 
    } 

    // MARK: - UITableViewDataSource 

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     print("self.categoryName :: ",self.categoryName) 
     if self.categoryName == "" 
     { 
      return 0 
     } 
     else 
     { 
      categoryQuestion = self.questionsDictionary[self.categoryName]! 
      print("categoryQuestion count :: ",categoryQuestion.count) 
      return categoryQuestion.count 
     } 

    } 

    // MARK: - UITableViewDelegate 

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
     let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! ChatCellView 

     //let repository = self.categoryArray[indexPath.row] 
     cell.questionNoLbl.text = String(indexPath.row) 
     cell.questionLbl.text = categoryQuestion[indexPath.row] 
     //cell.questionLbl.text = repository["name"] as? String 
     return cell 
    } 

    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 
     tableView.deselectRowAtIndexPath(indexPath, animated: true) 

     /*let detailViewController = storyboard?.instantiateViewControllerWithIdentifier("DetailViewController") as! DetailViewController 
     navigationController?.pushViewController(detailViewController, animated: true)*/ 
    } 

    func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool { 
     return true 
    } 

} 

也有對的UITableView以下自定義單元代碼:

import UIKit 

class ChatCellView: UITableViewCell 
{ 

    @IBOutlet var questionNoLbl: UILabel! 
    @IBOutlet var questionLbl: UILabel! 


} 

我的輸出看起來像下面的圖片:

[電流輸出圖像]

我也無法點擊類別名稱,如免費業務等

對不起,長職位,但不能!找出最近3天的解決方案。

UPDATE: 上傳故事板和多層截圖

[主要情節提要]

+0

我可以看到你的Storyboard在哪裏實現了'UIPageViewController'? –

+0

當然,你可以但幫助我上傳stryboard我怎麼上傳它,或者我應該截圖和上傳?有提供上傳整個故事板文件嗎? –

+0

不!更好的是你可以顯示截圖,或者如果你上傳了這個項目以便我可以查看它,它會很好! –

回答

3

在這裏,我已經修改了該項目PagingMenuController,當你想創建一個UI。

enter image description here

這裏是演示項目,你可以檢查出來!

我做了什麼?

我剛剛將ContainerView的限制更改爲特定高度,只要您想要。

並確保您已對其ChildViewController給出適當的限制。

項目下載鏈接:https://www.dropbox.com/s/zili4l7yot7dnvo/Example.zip?dl=0

答案更新

如果您正在使用自動佈局,那麼你可以使用框架來改變約束,必將導致錯誤。約束值相應

questionViewBottomConstraint.constant = 250.0 //any value (here you can use `UITableView` height) 

OR

questionViewBottomConstraint.constant = -250.0 

變化,看到了差距:如果你能想改變,那麼你可以通過下面的方式做到這一點。

讓我知道,如果有任何改變你想要的!

+0

pagingmenucontroller視圖來自設備視圖下方,我正在更改y位置以使其向上滑動。可能是導致約束的問題?請提供鏈接演示沒有鏈接。 –

+0

@NiranjanBalkrishnaPrajapati請檢查演示的鏈接。我已經提供了! –

+0

對不起,有幾天我休假了,我檢查了編譯項目時出現錯誤的演示。但我將代碼和ib與所有約束進行了比較,除此之外,我的視圖在運行時不在屏幕上,並且在按鈕單擊它時滑過。我認爲這與可能導致錶行或tabeleview不佔用寬度的滑動有關。你能幫我解決嗎? –