代码之家  ›  专栏  ›  技术社区  ›  Ahmed

当视图由纵向变为横向时,调整透明孔层的位置

  •  1
  • Ahmed  · 技术社区  · 6 年前

    let radius = min(self.view.frame.size.width,self.view.frame.size.height)
        print(radius)
        let path = UIBezierPath(roundedRect: CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: self.view.frame.size.height), cornerRadius: 0)
        let circlePath = UIBezierPath(roundedRect: CGRect(x: 0, y: self.view.frame.size.height/2 - radius/2 , width: radius, height: radius), cornerRadius: radius/2)
        path.append(circlePath)
    
        path.usesEvenOddFillRule = true
    
        fillLayer.path = path.cgPath
        fillLayer.fillRule = kCAFillRuleEvenOdd
        fillLayer.fillColor = UIColor.black.cgColor
        fillLayer.opacity = 0.5
        view.layer.addSublayer(fillLayer)
    

    enter image description here

    但是,如果视图从纵向更改为横向或从横向更改为纵向

    层的方向不正确,如图所示

    enter image description here

    1 回复  |  直到 6 年前
        1
  •  1
  •   Chris Hansen    6 年前

    看起来每当用户旋转设备时,都需要更新路径的帧。你在寻找类似的答案 here

    如果需要随过渡设置动画,请尝试以下操作:

    override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
        super.viewWillTransition(to: size, with: coordinator)
        coordinator.animate(alongsideTransition: { context in            
            // update frames here
        }, completion: nil)
    }