iOS开发点击UIButton实现UIView的旋转
更多阅读请访问http://www.hopean.com
http://www.hopean.com
首先创建界面,在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; }
更多阅读请访问http://www.hopean.com
http://www.hopean.com
首先button的点击方法实现
-(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];
更多阅读请访问http://www.hopean.com
http://www.hopean.com