Ios 使用内部图形旋转UIView时出现的问题

Ios 使用内部图形旋转UIView时出现的问题,ios,swift,uiview,rotation,drawrect,Ios,Swift,Uiview,Rotation,Drawrect,我想旋转内部有图形(圆形、矩形或直线)的UIView。问题是当我旋转视图并刷新图形时(当我更改图形的某些属性时,我需要ir,即),图形不符合视图 ui无旋转视图: ui旋转视图: 以下是我的简单代码(从原始代码中摘录): 主视图控制器 class TestRotation: UIViewController { // MARK: - Properties var drawingView:DrawingView? // MARK: - Actions @IBAction func ac

我想旋转内部有图形(圆形、矩形或直线)的UIView。问题是当我旋转视图并刷新图形时(当我更改图形的某些属性时,我需要ir,即),图形不符合视图

ui无旋转视图:

ui旋转视图:

以下是我的简单代码(从原始代码中摘录):

主视图控制器

class TestRotation: UIViewController {

// MARK: - Properties

var drawingView:DrawingView?

// MARK: - Actions

@IBAction func actionSliderRotation(_ sender: UISlider) {

    let degrees = sender.value
    let radians = CGFloat(degrees * Float.pi / 180)
    drawingView?.transform = CGAffineTransform(rotationAngle: radians)

    drawingView?.setNeedsDisplay()
}

// MARK: - Init

override func viewDidLoad() {
    super.viewDidLoad()

    drawingView = DrawingView(frame:CGRect(x:50, y:50, width: 100, height:75))

    self.view.addSubview(drawingView!)

    drawingView?.setNeedsDisplay() 
}

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

}
UIView

class DrawingView: UIView {

override func draw(_ rect: CGRect) {

    let start = CGPoint(x: 0, y: 0)
    let end = CGPoint(x: self.frame.size.width, y: self.frame.size.height)

    drawCicrle(start: start, end: end)
}

func drawCicrle(start:CGPoint, end:CGPoint) {

    //Contexto
    let context = UIGraphicsGetCurrentContext()

    if context != nil {

        //Ancho
        context!.setLineWidth(2)

        //Color
        context!.setStrokeColor(UIColor.black.cgColor)
        context!.setFillColor(UIColor.orange.cgColor)

        let rect = CGRect(origin: start, size: CGSize(width: end.x-start.x, height: end.y-start.y))

        let path = UIBezierPath(ovalIn: rect)

        path.lineWidth = 2
        path.fill()
        path.stroke()

    }
}

// MARK: - Init

override init(frame: CGRect) {
    super.init(frame: frame)

    self.backgroundColor = UIColor.gray
}

required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
}

}
如果在旋转后不刷新视图,问题似乎暂时解决了,但如果由于其他原因(属性已更改)必须稍后刷新,则问题再次出现


我能做什么?提前感谢

当您应该使用
边界时,您正在使用
框架

框架是包含
视图的矩形。它在
视图
超级视图
的坐标系中指定,旋转
视图
时,
会发生变化(因为旋转的正方形比未旋转的正方形在X和Y方向延伸得更远)

边界是包含
视图内容的矩形。它在
视图
本身的坐标系中指定,并且不会随着
视图
的旋转或移动而改变

如果在
draw(:)
函数中将
frame
更改为
bounds
,它将按预期工作:

override func draw(_ rect: CGRect) {

    let start = CGPoint(x: 0, y: 0)
    let end = CGPoint(x: self.bounds.size.width, y: self.bounds.size.height)

    drawCicrle(start: start, end: end)
}

下面是一个演示,显示了随着滑块的移动,椭圆形被重新绘制


以下是更改后的代码:

class DrawingView: UIView {

    var fillColor = UIColor.orange {
        didSet {
            setNeedsDisplay()
        }
    }

    override func draw(_ rect: CGRect) {

        let start = CGPoint(x: 0, y: 0)
        let end = CGPoint(x: self.bounds.size.width, y: self.bounds.size.height)

        drawCircle(start: start, end: end)
    }

    func drawCircle(start:CGPoint, end:CGPoint) {

        //Contexto
        let context = UIGraphicsGetCurrentContext()

        if context != nil {

            //Ancho
            context!.setLineWidth(2)

            //Color
            context!.setStrokeColor(UIColor.black.cgColor)
            context!.setFillColor(fillColor.cgColor)

            let rect = CGRect(origin: start, size: CGSize(width: end.x-start.x, height: end.y-start.y))

            let path = UIBezierPath(ovalIn: rect)

            path.lineWidth = 2
            path.fill()
            path.stroke()

        }
    }

    // MARK: - Init

    override init(frame: CGRect) {
        super.init(frame: frame)

        self.backgroundColor = UIColor.gray
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }

}


谢谢我已经看到了你的答案后,斗争了很多,在我看到它之前,我发现了关于矩形与边界的问题…;)我假设帧高度/宽度等于边界高度/宽度,但旋转时这似乎是错误的。给阅读的每个人:确保用
frame
To
bounds
替换所有代码!
@IBAction func actionSliderRotation(_ sender: UISlider) {

    let degrees = sender.value
    let radians = CGFloat(degrees * Float.pi / 180)
    drawingView?.transform = CGAffineTransform(rotationAngle: radians)

    drawingView?.fillColor = UIColor(hue: CGFloat(degrees)/360.0, saturation: 1.0, brightness: 1.0, alpha: 1.0)

}