Хотите представить модальный вид, но как заставить его частично занимать экран? - PullRequest
0 голосов
/ 15 сентября 2018

У меня есть два viewcontrollers.(См. Вложение). Viewcontroller слева называется «ViewController», а модальный viewController справа называется «draggableViewController».Когда я запускаю это приложение, синий вид объекта справа перехватит первый viewController.Синий объект вида справа Модальный viewController должен скользить поверх первого viewController, но я представляю этот вид без анимации, поэтому он будет выглядеть так, как будто синий вид покоится поверх первого ViewController.

Чего я хочу добиться, так это показать только 1/3 синего вида, и я хочу, чтобы он располагался на нижней трети экрана.Я понятия не имею, как установить положение синего вида, чтобы оно отображалось только на нижней трети экрана.

Вот мой исходный код для каждого из контроллеров представления.

1: ViewController:

class ViewController: UIViewController {

    override func viewDidAppear(_ animated: Bool) {
        let newView = self.storyboard!.instantiateViewController(withIdentifier: "draggableViewController") as! draggableViewController
        newView.modalPresentationStyle = UIModalPresentationStyle.overFullScreen
        self.present(newView, animated: false, completion: nil)
    }

}

2: draggableViewController:

class draggableViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        self.view.frame = CGRect(x: 0, y: 300, width: self.view.frame.width, height: self.view.frame.height)

        let panGestureRecognizer = UIPanGestureRecognizer(target: self, action: #selector(panGestureRecognizerAction(_:)))

        self.view.addGestureRecognizer(panGestureRecognizer)
    }

    @objc func panGestureRecognizerAction(_ gestureRecognizer: UIPanGestureRecognizer){

        // 1. use these values to restrict the left and right sides so the orangeImageView won't go beyond these points
        let leftSideRestrction = self.view.frame.minX
        let rightSideRestriction = self.view.frame.maxX
        let topRestriction = 0

        // 2. use these values to redraw the orangeImageView's correct size in either Step 6 or Step 8 below
        let imageViewHeight = self.view.frame.size.height
        let imageViewWidth = self.view.frame.size.width

         if gestureRecognizer.state == .changed || gestureRecognizer.state == .began {

            let translation: CGPoint = gestureRecognizer.translation(in: self.view)

            gestureRecognizer.view!.center = CGPoint(x: gestureRecognizer.view!.center.x  + translation.x, y: gestureRecognizer.view!.center.y + translation.y)

            gestureRecognizer.setTranslation(CGPoint.zero, in: self.view)     
            /*
             3.
             -get the the upper left hand corner of the imageView's X and Y origin to get the current location of the imageView as it's dragged across the screen.
             -you need the orangeImageView.frame.origin.x value to make sure it doesn't go beyond the left or right edges
             -you need the orangeImageView.frame.origin.y value to redraw it in Steps 6 and 8 at whatever Y position it's in when it hits either the left or right sides
             */
            var imageViewCurrentOrginXValue = self.view.frame.origin.x
            var imageViewCurrentOrginYValue = self.view.frame.origin.y

            print("origin x: \(imageViewCurrentOrginXValue)")
            print("origin y: \(imageViewCurrentOrginYValue)")

            // 4. get the right side of the orangeImageView. It's computed using the orangeImageView.frame.origin.x + orangeImageView.frame.size.width
            let imageViewRightEdgePosition = imageViewCurrentOrginXValue + imageViewWidth


            // 5. if the the orangeImageView.frame.origin.x touches the left edge of the screen or beyond it proceed to Step 6
            if imageViewCurrentOrginXValue <= leftSideRestrction {

                // 6. redraw the orangeImageView's frame with x: being the far left side of the screen and Y being where ever the current orangeImageView.frame.origin.y is currently positioned at
                if imageViewCurrentOrginYValue <= 0 {

                    // 6. redraw the orangeImageView's frame with x: being the far left side of the screen and Y being where ever the current orangeImageView.frame.origin.y is currently positioned at
                    imageViewCurrentOrginYValue = 0

                    self.view.frame = CGRect(x: leftSideRestrction, y: imageViewCurrentOrginYValue, width: imageViewWidth, height: imageViewHeight)    
                }

                if imageViewCurrentOrginYValue >= self.view.frame.height-200 {

                    // 6. redraw the orangeImageView's frame with x: being the far left side of the screen and Y being where ever the current orangeImageView.frame.origin.y is currently positioned at
                    imageViewCurrentOrginYValue = self.view.frame.height - 200

                    print(imageViewCurrentOrginYValue)

                    self.view.frame = CGRect(x: leftSideRestrction, y: imageViewCurrentOrginYValue, width: imageViewWidth, height: imageViewHeight)   
                }

                self.view.frame = CGRect(x: leftSideRestrction, y: imageViewCurrentOrginYValue, width: imageViewWidth, height: imageViewHeight)
            }

            // 7. if the the orangeImageView.frame.origin.x touches the right edge of the screen or beyond it proceed to Step 8
            if imageViewRightEdgePosition >= rightSideRestriction{

                // 8. redraw the orangeImageView's frame with x: being the rightSide of the screen - the orangeImageView's width and y: being where ever the current orangeImageView.frame.origin.y is currently positioned at
                if imageViewCurrentOrginYValue <= 0 {

                    // 6. redraw the orangeImageView's frame with x: being the far left side of the screen and Y being where ever the current orangeImageView.frame.origin.y is currently positioned at
                    imageViewCurrentOrginYValue = 0

                    self.view.frame = CGRect(x: leftSideRestrction, y: imageViewCurrentOrginYValue, width: imageViewWidth, height: imageViewHeight)
                }

                if imageViewCurrentOrginYValue >= self.view.frame.height - 200 {

                    // 6. redraw the orangeImageView's frame with x: being the far left side of the screen and Y being where ever the current orangeImageView.frame.origin.y is currently positioned at

                    imageViewCurrentOrginYValue = self.view.frame.height - 200

                    print(imageViewCurrentOrginYValue)

                    self.view.frame = CGRect(x: leftSideRestrction, y: imageViewCurrentOrginYValue, width: imageViewWidth, height: imageViewHeight)
                }

                self.view.frame = CGRect(x: rightSideRestriction - imageViewWidth, y: imageViewCurrentOrginYValue, width: imageViewWidth, height: imageViewHeight)
            }
    }

  }

}

Viewcontroller on the left is called

Где мне установить положение этого синего вида?

Ответы [ 2 ]

0 голосов
/ 16 сентября 2018

Где я могу установить позицию этого синего вида

В подклассе UIPresentationController, когда вы делаете это должным образом как пользовательская анимация перехода.

0 голосов
/ 16 сентября 2018

Простое решение состоит в том, чтобы установить ограничения в синем представлении равными 1/3 размера основного контроллера, и сделать фон прозрачным или размытым, чтобы он показывал его скольжение вверх только на 1/3 размера.Шаги к этому, установите вид внутри синего цвета и используйте этот вид и все, что за ним, чтобы сделать его прозрачным.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...