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

徘徊边缘 提交于 2019-12-22 04:01:54

问题


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 :/ )


回答1:


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

Result:

All transformation key paths are presented in Apple documentation.




回答2:


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


来源:https://stackoverflow.com/questions/32027552/rotate-angle-a-uiview-using-user-defined-runtime-attributes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!