Rotate (angle) a UIView, using User Defined Runtime Attributes
Asked Answered
T

2

12

Is it possible to rotate a UIView or UIImageView, say 10 or 15 degrees,

actually using User Defined Runtime Attributes?

Or can it only be done in code?

(Note .. I mention "angle" and "by degrees" for the sake of google; it can get mixed-up with the idea of "rotating" for device orientation change.)


Alternately, could you perhaps subclass UIView in such a way that, a User Defined Runtime Attribute would be exposed, which does just this? (I'm rusty :/ )

Tambac answered 15/8, 2015 at 17:50 Comment(0)
C
33

You can use layer.transform.rotation.z key path for UIView object.
Value has to be set in radians:

enter image description here

Result:

enter image description here

All transformation key paths are presented in Apple documentation.

Casar answered 22/8, 2015 at 0:6 Comment(3)
I confirmed it worked - I checked it with a UILabel. Fantastic!Tambac
Is there a way to do this today? It isn't working for me in Xcode 9.4.1.Timpani
This doesn't update in the xib, but if you run it will render in the app.Shack
B
4

In extension to this answer How to access User Defined Runtime Attribute from the 'sender' object?, you can retrieve the runtime value as an NSNumber in degrees, and then perform the rotation. Trigger the IBAction in interface builder when you want the rotation to occur.

#define RADIANS(degrees) ((degrees * M_PI) / 180.0)

@interface RTFRotatingView : UIView
@end

@implementation RTFRotatingView

- (IBAction)performRotation:(UIControl *)sender {
    if ([sender respondsToSelector:@selector(keyName)]) {
        NSNumber *degrees = [sender valueForKey:@"keyName"];
        CGAffineTransform rotateTransform = CGAffineTransformRotate(CGAffineTransformIdentity, RADIANS(degrees.doubleValue));
        self.transform = rotateTransform;
    }
}

@end
Bromoform answered 15/8, 2015 at 18:9 Comment(5)
Max, I'm not totally sure how this solves the problem posed in the question here??Tambac
As mentioned in the other question, you can set the number of degrees i.e. 10 as a value in the user defined runtime attributes for the view in interface builder. From there, by subclassing the UIView, you can retrieve the value and perform the rotation.Bromoform
Max, what the heck call do you use in a subclass of UIView (so, I remember vaguely it's something like "when unloaded" .. kind of equivalent to "viewDidLoad" in a VC) ... to do that automatically when the view is unboxed from the IB? cheers...Tambac
Oh yeah .. it's awakeFromNib. But note the warning from Joe Blow in the comment here ... https://mcmap.net/q/477018/-iboutlet-isn-39-t-connected-in-awakefromnib I do not know if you can CGAffineTransformRotate in awakeFromNib .. it may be like animations, which you can't do.Tambac
@JoeBlow give it a try, otherwise run it on a timer to perform the rotation after x seconds. Alternatively see https://mcmap.net/q/272907/-ios-viewdidload-for-uiviewBromoform

© 2022 - 2024 — McMap. All rights reserved.