怎样实现UIView的旋转

首先创建界面,在viewDidLoad创建view以及button

 

相关代码如下

-(void)viewDidLoad

{

    [super viewDidLoad];

 

    viewDemo = [[UIView alloc] initWithFrame:CGRectMake(0, 50, 320, 50)];

    [viewDemo setBackgroundColor:[UIColor brownColor]];

    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 50)];

    label.text =@"Test";

    label.tag=100;

    [viewDemo addSubview:label];

    [self.view addSubview:viewDemo];

    [label release];

 

    // Do any additional setup after loading the view, typically from a nib.

    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 400, 100, 50)];

    [btn setBackgroundColor:[UIColor blueColor]];

    btn.showsTouchWhenHighlighted = YES;

    [btn setTitle:@"点击旋转" forState:UIControlStateNormal];

    [self.view addSubview:btn];

    btn.tag = 10010;

    [btn addTarget:self action:@selector(testViewDemo) forControlEvents:UIControlEventTouchUpInside];

    [btn release];

    number = 0;

 

}

-(void)testViewDemo

{

    number++;

    if (number%3 == 0) {

        CGAffineTransform at = CGAffineTransformMakeRotation(M_PI/2);

        at = CGAffineTransformTranslate(at, 200, 0);

        [viewDemo setTransform:at];

    }else if (number%3 ==1 ){

        CGAffineTransform at = CGAffineTransformMakeRotation(M_PI/1);

        at = CGAffineTransformTranslate(at, 0, 0);

        [viewDemo setTransform:at];

    }else if(number%3 ==2){

        CGAffineTransform at = CGAffineTransformMakeRotation(M_PI*2);

        at = CGAffineTransformTranslate(at, 0, 0);

        [viewDemo setTransform:at];

    }

}

 

 

-(void)testViewDemo

{

    //旋转,左旋

    number++;

    CGAffineTransform rotate = CGAffineTransformMakeRotation(number / 180.0 * M_PI );

    [viewDemo setTransform:rotate];

}

 

如果使用此方法连续点击button就可以显示处一点一点的移动效果

 

每次旋转90度的话可以使用一下的代码实现效果

 

 

 

-(void)testViewDemo

{

//旋转,右旋

    number = number+90.0;

    CGAffineTransform rotate = CGAffineTransformMakeRotation(number / 180.0 * M_PI );

    [viewDemo setTransform:rotate];

 

UIView实现左旋效果

 

//旋转,左旋

    number = number-90.0;

    CGAffineTransform rotate = CGAffineTransformMakeRotation(number / 180.0 * M_PI );

    [viewDemo setTransform:rotate];

通过以上的讲解,我想,关于点击UIButton实现UIView的旋转大家就可以轻松的操作了。

 

 

传送门:

http://jingyan.baidu.com/article/3aed632e7164b070108091de.html

posted @ 2015-12-31 09:54  小毛驴  阅读(920)  评论(0编辑  收藏  举报