Rotate UIView around its center keeping its size

前端 未结 10 1108
没有蜡笔的小新
没有蜡笔的小新 2020-12-02 14:17

I\'m trying to rotate an UIView a few radians but after applying the transformation it doesn\'t look to be keeping its size. What\'s the proper way to achieve t

相关标签:
10条回答
  • 2020-12-02 14:34

    The CGAffineTransformRotate transformation rotates from an existing affine transform. The fact that you are using CGAffineTransformIdentity might be the issue. You must specify the current transform of your view.

    #define DEGREES_TO_RADIANS(angle) ((angle) / 180.0 * M_PI)
    ...
    double rads = DEGREES_TO_RADIANS(240);
    CGAffineTransform transform = CGAffineTransformRotate(self.arrowView.transform, rads);
    self.arrowView.transform = transform;
    

    Also, you might want to consider:

    self.arrowView.transform = CGAffineTransformMakeRotation(rads);
    

    EDIT : If you can, share what you kind of transformation (animated/inanimate , single/iterative) you want to achieve. I believe there might be a better, optimized way of doing this.

    0 讨论(0)
  • 2020-12-02 14:35

    Swift + extension are your friends!

    // MARK: - UIView Extension -
    
    extension UIView {
    
        /**
         Rotate a view by specified degrees
    
         - parameter angle: angle in degrees
         */
        func rotate(angle: CGFloat) {
            let radians = angle / 180.0 * CGFloat.pi
            let rotation = CGAffineTransformRotate(self.transform, radians);
            self.transform = rotation
        }
    
    }
    

    In this way, anywhere in your code:

    let view = UIView(frame: CGRectMake(0, 0, 100, 100))
    view.backgroundColor = UIcolor.redColor()
    view.rotate(angle: 90)
    
    0 讨论(0)
  • 2020-12-02 14:36

    Update Luca Davanzo's answer with Swift 4:

    /**
     Rotate a view by specified degrees
    
     - parameter angle: angle in degrees
     */
    func rotate(angle: CGFloat) {
        let radians = angle / 180.0 * CGFloat.pi
        let rotation = self.transform.rotated(by: radians)
        self.transform = rotation
    }
    
    0 讨论(0)
  • 2020-12-02 14:43

    On Swift 3:

    let degrees: CGFloat = -90
    let radians = CGFloat(__sinpi(degrees.native/180.0))
    view.transform = CGAffineTransform(rotationAngle: radians)
    

    I'm using -90 because of this specific part of the documentation about the rotationAngle you have to pass to CGAffineTransform:

    The angle, in radians, by which this matrix rotates the coordinate system axes. In iOS, a positive value specifies counterclockwise rotation and a negative value specifies clockwise rotation.

    0 讨论(0)
  • 2020-12-02 14:48

    To my mind you need to calculate the center of your triangle and rotate around this point. Now you rotate the arrow around the center of your square.

    See: One step affine transform for rotation around a point?

    I Hope it will help you.

    0 讨论(0)
  • 2020-12-02 14:49

    I avoid using macros unless necessary. This works perfectly well

    float degrees = 20; //the value in degrees
    view.transform = CGAffineTransformMakeRotation(degrees * M_PI/180);
    
    0 讨论(0)
提交回复
热议问题