Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to rotate an image in Storyboard or do you have to do it programmatically?

Assume you have a triangle image you want to use at different angles (e.g., 180 degrees, 90 degrees).

Is it possible to rotate the triangle image within Storyboard, or do you need to do it programmatically?

like image 284
Crashalot Avatar asked May 17 '16 17:05

Crashalot


People also ask

How do you rotate view on storyboard?

From the Tools toolbar, select the Rotate View tool. Hold down Ctrl+Alt (Windows) or ⌘+Alt (Mac OS X).

How do I rotate an image in Swift?

Basic Swift Code for iOS AppsStep 1 − Open Xcode→SingleViewApplication→name it RotateImage. Step 2 − Open Main. storyboard, add UIImageView and add 2 buttons as shown below name them ROTATE BY 90 DEGREES AND ROTATE BY 45 DEGREES. Add some sample images to UIImage View.


3 Answers

You could probably create an IBDesignable & IBInspectable UIView subclass that had a rotation angle property, and applied a transform to the image it contained.

IBInspectable allows you to expose custom properties of your custom views in IB's Attributes inspector.

Making a view IBDesignable allows you to view a preview of your custom view object in IB.

Edit:

This is a very old thread, but I decided to implement a custom UIView that allows rotation, as I described. Since it's now 2021, I used Swift:

@IBDesignable class RotatableView: UIView {

    @objc @IBInspectable var rotationDegrees: Float = 0 {
        didSet {
            print("Setting angle to \(rotationDegrees)")
            let angle = NSNumber(value: rotationDegrees / 180.0 * Float.pi)
            layer.setValue(angle, forKeyPath: "transform.rotation.z")
        }
    }
}

That yields the following in Interface Builder:

enter image description here

like image 117
Duncan C Avatar answered Sep 30 '22 04:09

Duncan C


It is possible to set layer.transform.rotation.z in User Defined Runtime Attributes. Check this answer: https://stackoverflow.com/a/32150954/2650588

like image 27
Vitalii Avatar answered Sep 30 '22 02:09

Vitalii


Programmatically some thing like this can help:

//rotate rect
    myImageView.transform = CGAffineTransformMakeRotation(M_PI_2); //90 degree//rotation in radians

//For 180 degree use M_PI

Or make a macro like this:

#define DEGREES_TO_RADIANS(degree) (M_PI * (degree) / 180.0)

and use this way:

CGAffineTransformMakeRotation(DEGREES_TO_RADIANS(90));//here may be anything you want 45/90/180/270 etc.

More here : apple link

like image 42
maddy Avatar answered Sep 30 '22 04:09

maddy