走马灯(四个方格循环移动)
来源:互联网 发布:网络安全技术是什么 编辑:程序博客网 时间:2024/05/21 16:58
#import "ViewController.h"@interface ViewController ()@endstatic BOOL bool1 = YES;@implementation ViewController- (void)viewDidLoad{ [super viewDidLoad]; view1 = [[UIView alloc]init]; view1.frame = CGRectMake(0, 0, 320, 480); view1.backgroundColor =[UIColor greenColor]; [self.view addSubview:view1]; label1 = [[UILabel alloc]initWithFrame:CGRectMake(10, 100, 100, 100)];// label1.text = @"a"; label1.backgroundColor = [UIColor redColor]; [view1 addSubview:label1]; label2 = [[UILabel alloc]initWithFrame:CGRectMake(110, 100, 100, 100)];// label2.text = @"b"; label2.backgroundColor = [UIColor blueColor]; [view1 addSubview:label2]; label3 = [[UILabel alloc]initWithFrame:CGRectMake(10, 200, 100, 100)];// label3.text = @"c"; label3.backgroundColor = [UIColor purpleColor]; [view1 addSubview:label3]; label4 = [[UILabel alloc]initWithFrame:CGRectMake(110, 200, 100, 100)];// label4.text = @"d"; label4.backgroundColor = [UIColor yellowColor]; [view1 addSubview:label4]; button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect]; button1.frame = CGRectMake(10, 320, 80, 30); [button1 setTitle:@"顺时针" forState:UIControlStateNormal]; [button1 addTarget:self action:@selector(change) forControlEvents:UIControlEventTouchUpInside]; [view1 addSubview:button1]; button2 = [UIButton buttonWithType:UIButtonTypeRoundedRect]; button2.frame = CGRectMake(130, 320, 80, 30); [button2 setTitle:@"逆时针" forState:UIControlStateNormal]; [button2 addTarget:self action:@selector(changele) forControlEvents:UIControlEventTouchUpInside]; [view1 addSubview:button2]; // Do any additional setup after loading the view, typically from a nib.}//顺时针-(void)change{ if (bool1 == YES) { [UIView beginAnimations:@"testA" context:NULL];//开始动画 [UIView setAnimationDuration:1];//动画持续时间 [UIView setAnimationCurve:UIViewAnimationCurveLinear]; [UIView setAnimationDelegate:self];//动画代表// [UIView setAnimationRepeatAutoreverses:YES];//设置动画重复自动反转 [UIView setAnimationRepeatCount:99];//循环次数 a = [[UIView alloc]init]; a.center = label1.center; label1.center = label2.center; label2.center = label4.center; label4.center = label3.center; label3.center = a.center; [UIView commitAnimations]; [button1 setTitle:@"返回" forState:UIControlStateNormal]; [a release]; bool1 = NO; } else { [UIView beginAnimations:@"testA" context:NULL]; [UIView setAnimationDuration:1]; [UIView setAnimationCurve:UIViewAnimationCurveLinear]; [UIView setAnimationDelegate:self]; [UIView setAnimationRepeatAutoreverses:NO]; [UIView setAnimationRepeatCount:0]; a = [[UIView alloc]init]; a.center = label1.center; label1.center = label2.center; label2.center = label4.center; label4.center = label3.center; label3.center = a.center; [UIView commitAnimations]; [button1 setTitle:@"顺时针" forState:UIControlStateNormal]; [a release]; bool1 = YES; }}//逆时针-(void)changele{ if (bool1 == YES) { [UIView beginAnimations:@"testA" context:NULL]; [UIView setAnimationDuration:1]; [UIView setAnimationCurve:UIViewAnimationCurveLinear]; [UIView setAnimationDelegate:self]; [UIView setAnimationRepeatAutoreverses:NO]; [UIView setAnimationRepeatCount:99]; a = [[UIView alloc]init]; a.center = label3.center; label3.center = label4.center; label4.center = label2.center; label2.center = label1.center; label1.center = a.center; [UIView commitAnimations]; [button2 setTitle:@"返回" forState:UIControlStateNormal]; [a release]; bool1 = NO; } else { [UIView beginAnimations:@"testA" context:NULL]; [UIView setAnimationDuration:1]; [UIView setAnimationCurve:UIViewAnimationCurveLinear]; [UIView setAnimationDelegate:self]; [UIView setAnimationRepeatAutoreverses:NO]; [UIView setAnimationRepeatCount:0]; a = [[UIView alloc]init]; a.center = label3.center; label3.center = label4.center; label4.center = label2.center; label2.center = label1.center; label1.center = a.center; [UIView commitAnimations]; [button2 setTitle:@"逆时针" forState:UIControlStateNormal]; [a release]; bool1 = YES; }}-(void)dealloc{ [view1 release]; [label1 release]; [label2 release]; [label3 release]; [label4 release]; [super dealloc];}- (void)viewDidUnload{ [super viewDidUnload]; // Release any retained subviews of the main view.}- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{ return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);}@end