2016-03-23 19 views
0

私はコレクションビューと2つのコンテナビューをスライド表示する(これをmenugridと呼ぶ)。 menugridを上にスライドさせると、フレームが画面全体を覆うように設定されます。ユーザーがUICollectionViewセルをコンテナビューにドラッグアンドドロップできる機能を構築しようとしています。ドラッグ&ドロップを開始すると、メニューグリッドビューは画面の下部にある最初の位置に戻ります。ここでSwift:サブビューが追加されたときにビューのフレームが変更される

はいくつかのコードです:それを引き起こしている可能性が何

func handlePanForCell(gestureRecognizer: UILongPressGestureRecognizer) { 

     let pressPoint: CGPoint = gestureRecognizer.locationInView(gestureRecognizer.view) 
     let localPoint : CGPoint = self.menuGridView.convertPoint(pressPoint, fromView: gestureRecognizer.view!) 
     var cell : MenugridCollectionViewCell = MenugridCollectionViewCell() 
     if(gestureRecognizer.view is UICollectionViewCell){ 
      if (gestureRecognizer.state == UIGestureRecognizerState.Began) { 
       cell = gestureRecognizer.view as! MenugridCollectionViewCell 
       UIGraphicsBeginImageContext(cell.bounds.size); 
       cell.layer.renderInContext(UIGraphicsGetCurrentContext()!) 
       let cellImage : UIImage = UIGraphicsGetImageFromCurrentImageContext(); 
       UIGraphicsEndImageContext(); 
       self.movingCell = UIImageView.init(image: cellImage) 
       self.movingCell?.alpha = 0.75 
       //let cellFrameInSuperview : CGRect = self.menuGridCollectionView.convertRect(cell.frame, toView: self.menuGridView) 
       self.movingCell?.center = localPoint 
       //self.movingCell?.frame = cellFrameInSuperview 
       self.view.addSubview(self.movingCell!) 
      } 
      if (gestureRecognizer.state == UIGestureRecognizerState.Changed) { 
       self.movingCell?.center = localPoint 
       setMenuGridViewOnTop() 
      } 
      if (gestureRecognizer.state == UIGestureRecognizerState.Ended) { 

       // Check to see if the drop area is in the personalized info-area 
       let isContaining = self.menuGridView.convertRect(self.firstContainerView.frame, toView: self.menuGridView).contains(self.movingCell!.frame) || self.middleView.convertRect(self.secondInfoView.frame, toView: self.menuGridView).contains(self.movingCell!.frame) 
       if isContaining 
       { 
        if(self.middleView.convertRect(self.firstInfoView.frame, toView: self.view).contains(self.movingCell!.frame)) { 
         //let index = self.findIndexOfViewController(self.infoViewArray[0]) 
         //let oldViewController : UIViewController = self.childViewControllers[Int(index)] as UIViewController 
         self.switchInfoViews(nil, toView: (gestureRecognizer.view as! MenugridCollectionViewCell).name!, atPosition: 0, parentView: "menuGridView") 
        } 
        else { 
         self.switchInfoViews(nil, toView: (gestureRecognizer.view as! MenugridCollectionViewCell).name!, atPosition: 1, parentView: "menuGridView") 
        } 
        //self.movingCell?.removeFromSuperview() 
       } 
       self.movingCell?.removeFromSuperview() 
       self.movingCell = nil 
       //setMenuGridViewOnTop() 

       //self.view.viewWithTag(1000)?.removeFromSuperview() 
       //self.view.viewWithTag(1001)?.removeFromSuperview() 
       //self.middleView.alpha = 1 
       //self.childViewControllers[0].view.alpha = 1 
       //self.childViewControllers[1].view.alpha = 1 
       //self.topView.alpha = 1 
      } 
     } 
     //setMenuGridViewOnTop() 
    } 



//MARK: - Switch Views 
    func switchInfoViews(fromViewController : UIViewController?, toView : String, atPosition : CGFloat, parentView : String) { 

     var newViewController : InfoViewController = InfoViewController() 
     var storyBoardIdOfNewView = String() 
     if(toView == "Doctor"){ 
      newViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("ProviderSearchInfoViewControllerID") as! ProviderSearchInfoViewController 
      storyBoardIdOfNewView = "ProviderSearchInfoViewControllerID" 
     } 
     else if(toView == "PayBill"){ 
      newViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("PayBillInfoViewControllerID") as! PayBillInfoViewController 
      storyBoardIdOfNewView = "PayBillInfoViewControllerID" 
     } 
     else if(toView == "Symptoms"){ 
      newViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("SymptomsInfoViewControllerID") as! SymptomsInfoViewController 
      storyBoardIdOfNewView = "SymptomsInfoViewControllerID" 
     } 
     else if(toView == "Handouts"){ 
      newViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("PatientHandoutInfoViewControllerID") as! PatientHandoutInfoViewController 
      storyBoardIdOfNewView = "PatientHandoutInfoViewControllerID" 
     } 
     else if(toView == "FirstAid"){ 
      newViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("FirstAidInfoViewControllerID") as! FirstAidInfoViewController 
      storyBoardIdOfNewView = "FirstAidInfoViewControllerID" 
     } 

     if(parentView == "menuGridView") { 

       let viewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier(storyBoardIdOfNewView) 
       if(atPosition == 0){ 
        self.firstContainerView.subviews.forEach({ $0.removeFromSuperview() }) 
        viewController.view.frame = CGRectMake(0, 0, (self.menuGridView.frame.width/2) - 3, self.menuGridView.frame.height * 0.3)//self.firstContainerView.frame 
        self.firstContainerView.addSubview(viewController.view) 
       } 
       else { 
        self.secondContainerView.subviews.forEach({ $0.removeFromSuperview() }) 
        viewController.view.frame = CGRectMake(0, 0, (self.menuGridView.frame.width/2) - 3, self.menuGridView.frame.height * 0.3)//self.secondContainerView.frame 
        self.secondContainerView.addSubview(viewController.view) 
       } 
      if(self.menuAtTop){ 
       self.setMenuGridViewOnTop() 
      } 
      } 
      else if(parentView == "middleView"){ 

       if(!self.checkIfViewControllerIsAlreadyDisplayed(newViewController)) 
       { 
        fromViewController?.willMoveToParentViewController(nil) 
        fromViewController?.view.removeFromSuperview() 
        fromViewController?.removeFromParentViewController() 
        self.movingCell?.removeFromSuperview() 
        newViewController.view.frame = CGRectMake(0 + atPosition * self.middleView.frame.size.width/2, 0 , self.middleView.frame.size.width/2, self.middleView.frame.size.height) 
        self.middleView.addSubview(newViewController.view) 
        self.addChildViewController(newViewController) 
        newViewController.didMoveToParentViewController(self) 
        if(self.infoViewArray.count > 0) { 
         self.infoViewArray.removeAtIndex(Int(atPosition)) 
         self.infoViewArray.insert(toView, atIndex: Int(atPosition)) 
        } 
       } 
      } 
    } 

?私はなぜこれが起こるのかが分かりません。ありがとうございました。

+0

ビューに自動レイアウトは設定されていません。レイアウトがコードで設定されています。 – userx

答えて

0

私はプロジェクトからAutoLayoutを削除し、コードのレイアウトを設定することで問題を解決しました。

なぜ私はそれがAutoLayoutをよりよく理解するのに役立つのかを知りたいと思います。

関連する問題