ios 视图的旋转及应用
有时候,需要做出如下图所示的效果,这就需要用到视图的旋转了
1.首先将旋转的值由角度转换为弧度:
#define degreesToRadinas(x) (M_PI * (x)/180.0)
注:M_PI是ios内置的常量,值为:
3.14159265358979323846264338327950288
2.每一个视图都有一个transform属性,默认为:
CGAffineTransformIdentity
3.使一个视图旋转90度:
_display.transform= CGAffineTransformIdentity;//重置视图角度旋转为默认值
_display.transform= CGAffineTransformMakeRotation(degreesToRadinas(90));//旋转90度(将90度转换为弧度)
注:_display = [UIButtonbuttonWithType:UIButtonTypeRoundedRect];
示例代码
a、视图旋转
UIView * view = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 30, 30)]; view.backgroundColor = [UIColor yellowColor]; view.clipsToBounds = YES; view.transform = CGAffineTransformIdentity; view.transform = CGAffineTransformMakeRotation(degreeToRadinas(45)); [self.view addSubview:view]; [view release];
b、视图中,label旋转
UIView * view = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 30, 30)]; view.backgroundColor = [UIColor yellowColor]; view.clipsToBounds = YES; [self.view addSubview:view]; UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(0, 10, 50, 10)]; label.text = @"倾斜"; label.backgroundColor = [UIColor greenColor]; label.transform = CGAffineTransformIdentity; label.transform = CGAffineTransformMakeRotation(degreeToRadinas(-45)); [view addSubview:label]; [label release]; [view release];
注:
clipsToBounds属性
取值:BOOL(YES/NO)
作用:决定了子视图的显示范围。具体的说,就是当取值为YES时,剪裁超出父视图范围的子视图部分;当取值为NO时,不剪裁子视图。默认值为NO。