2017-09-10 1 views
0

두 개의보기가 있으며 그 중 하나는 mainVideoView (회색)이고 다른 하나는 subVideoView (빨간색)입니다. 둘 다 서브 뷰 UIApplication.shared.keyWindow입니다.) 아래 이미지와 같이 보기 크기가 자동으로 창 크기로 설정됩니다. 창 하위보기 중 하나가 제거되면

나는 시도하고 func minimiseOrMaximiseViews를 사용하여 (최소화

는, 그들은 (최소화 얻을.

된 후,이 창에서 subVideoView을 제거하고 싶습니다. 내가 시도하고 제거하는 순간을 (func minimiseOrMaximiseViews에서 호출 func removeSubVideoViewFromVideoView() 사용) subVideoView는 mainVideoView 내가, 나는 그것이 같은 크기로 있고 싶어 이런 일이 왜 확실하지 않다, 전체 화면 크기로 확대합니다.

누군가가 제안/조언을 주시겠습니까 어떻게 달성 할 수 있습니까?

내가보기

func configureVideoView(){ 
    videoView.backgroundColor = UIColor.darkGray 
    videoView.tag    = 0 // To identify view during animation 

    // ADDING TAP GESTURE TO MAXIMISE THE VIEW WHEN ITS SMALL 
    let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(handleTap(gestureRecognizer:))) 
    videoView.addGestureRecognizer(tapGestureRecognizer) 

    // ADDING PAN GESTURE RECOGNISER TO MAKE VIDEOVIEW MOVABLE INSIDE PARENT VIEW 
    videoView.addGestureRecognizer(UIPanGestureRecognizer(target: self, action: #selector(self.dragView))) 


    guard let window = UIApplication.shared.keyWindow else{ 
    return 
    } 

    window.addSubview(videoView) 

    videoView.translatesAutoresizingMaskIntoConstraints = false 

    let viewsDict = ["videoView" : videoView] as [String : Any] 

    // SETTING CONSTRAINTS 
    window.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|[videoView]|", options: [], metrics: nil, views: viewsDict)) 
    window.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|[videoView]|", options: [], metrics: nil, views: viewsDict)) 


    window.layoutIfNeeded() // Lays out subviews immediately 
    window.bringSubview(toFront: videoView) // To bring subView to front 

    print("Videoview constraints in configureVideoView \(videoView.constraints)") 

} 

func configureSubVideoView(){ 

    subVideoView.backgroundColor = UIColor.red 
    subVideoView.tag    = 1 // To identify view during animation 
    subVideoView.isHidden   = true 


    // Adding Pan Gesture recogniser to make subVideoView movable inside parentview 
    subVideoView.addGestureRecognizer(UIPanGestureRecognizer(target: self, action: #selector(self.dragView))) 

    // Constraining subVideoView to window to ensure that minimising and maximising animation works properly 
    constrainSubVideoViewToWindow() 

} 

func constrainSubVideoViewToWindow(){ 

    //self.subVideoView.setNeedsLayout() 
    guard let window = UIApplication.shared.keyWindow else{ 
     print("Window does not exist") 
     return 
    } 

    guard !window.subviews.contains(subVideoView)else{ // Does not allow to go through the below code if the window already contains subVideoView 
     return 
    } 

    if self.videoView.subviews.contains(subVideoView){ // If videoView contains subVideoView remove subVideoView 

     subVideoView.removeFromSuperview() 

    } 

    window.addSubview(subVideoView) 
     // Default constraints to ensure that the subVideoView is initialised with maxSubVideoViewWidth & maxSubVideoViewHeight and is positioned above buttons 

    let bottomOffset = buttonDiameter + buttonStackViewBottomPadding + padding 

    subVideoView.translatesAutoresizingMaskIntoConstraints = false 

    let widthConstraint = NSLayoutConstraint(item: subVideoView, attribute: .width, relatedBy: .equal, toItem: nil, attribute: NSLayoutAttribute.notAnAttribute, multiplier: 1.0, constant: maxSubVideoViewWidth) 

    let heightConstraint = NSLayoutConstraint(item: subVideoView, attribute: .height, relatedBy: .equal, toItem: nil, attribute: NSLayoutAttribute.notAnAttribute, multiplier: 1.0, constant: maxSubVideoViewHeight) 

    let rightConstraint = NSLayoutConstraint(item: subVideoView, attribute: .trailing, relatedBy: .equal, toItem: window, attribute: .trailing, multiplier: 1.0, constant: -padding) 

    let bottomConstraint = NSLayoutConstraint(item: subVideoView, attribute: .bottom, relatedBy: .equal, toItem: window, attribute: .bottom, multiplier: 1.0, constant: -bottomOffset) 

    var constraintsArray = [NSLayoutConstraint]() 
    constraintsArray.append(widthConstraint) 
    constraintsArray.append(heightConstraint) 
    constraintsArray.append(rightConstraint) 
    constraintsArray.append(bottomConstraint) 

    window.addConstraints(constraintsArray) 

    window.layoutIfNeeded() // Lays out subviews immediately 
    subVideoView.setViewCornerRadius() 
    window.bringSubview(toFront: subVideoView) // To bring subView to front 

} 

을 설정하고 방법

이 내가보기 애니메이션 및 제거 subVideoView에게

func minimiseOrMaximiseViews(animationType: String){ 

    let window = UIApplication.shared.keyWindow 

    let buttonStackViewHeight = buttonsStackView.frame.height 

    if animationType == "maximiseView" { 
     constrainSubVideoViewToWindow() 

    } 


    UIView.animate(withDuration: 0.3, delay: 0, options: [], 

     animations: { [unowned self] in // "[unowned self] in" added to avoid strong reference cycles, 
      switch animationType { 

      case "minimiseView" : 

       self.hideControls() 

       // Minimising self i.e videoView 
       self.videoView.frame = CGRect(x:  self.mainScreenWidth - self.videoViewWidth - self.padding, 
               y:  self.mainScreenHeight - self.videoViewHeight - self.padding, 
               width: self.videoViewWidth, 
               height: self.videoViewHeight) 


       // Minimising subVideoView 
       self.subVideoView.frame = CGRect(x:  self.mainScreenWidth - self.minSubVideoViewWidth - self.padding * 2, 
                y:  self.mainScreenHeight - self.minSubVideoViewHeight - self.padding * 2, 
                width: self.minSubVideoViewWidth, 
                height: self.minSubVideoViewHeight) 
       window?.layoutIfNeeded() 


       self.videoView.setViewCornerRadius() 
       self.subVideoView.setViewCornerRadius() 

       print("self.subVideoView.frame AFTER setting: \(self.subVideoView.frame)") 



      default: 
       break 
      } 
    }) { [unowned self] (finished: Bool) in // This will be called when the animation completes, and its finished value will be true 

     if animationType == "minimiseView" { 

      // **** Removing subVideoView from videoView **** 
      self.removeSubVideoViewFromVideoView() 

     } 
    } 


} 

func removeSubVideoViewFromVideoView(){ 

    //self.subVideoView.setNeedsLayout() 

    guard let window = UIApplication.shared.keyWindow else{ 
     return 
    } 

    guard !self.videoView.subviews.contains(subVideoView)else{ // Does not allow to go through the below code if the videoView already contains subVideoView 
     return 
    } 

    if window.subviews.contains(subVideoView) {  // removing subVideoView from window 
     subVideoView.removeFromSuperview() // *** CAUSE OF ISSUE *** 

    } 

    guard !window.subviews.contains(subVideoView) else{ 
     return 
    } 

    videoView.addSubview(subVideoView) 
} 

답변

2

VideoView를 지켜 보면서 및 subVideoView 자동 레이아웃으로 배치되어 있어요 방법이입니다. 프레임을 변경하려면 자동 레이아웃에서 구속 조건을 업데이트해야합니다.

removeFromSuperView을 호출하면 제거하려는 뷰를 참조하거나 제거하려는 뷰의 하위 트리에있는 뷰를 참조하는 제약 조건이 제거됩니다. 즉, 자동 레이아웃 시스템이 뷰를 재 배열합니다.

관련 문제