走马灯(四个方格循环移动)

来源:互联网 发布:考试系统的数据流程图 编辑:程序博客网 时间:2024/05/21 07:52
  1. #import "ViewController.h"  
  2.   
  3. @interface ViewController ()  
  4.   
  5. @end  
  6. static BOOL bool1 = YES;  
  7. @implementation ViewController  
  8.   
  9. - (void)viewDidLoad  
  10. {  
  11.     [super viewDidLoad];  
  12.       
  13.     view1 = [[UIView alloc]init];  
  14.     view1.frame = CGRectMake(0, 0, 320, 480);  
  15.     view1.backgroundColor =[UIColor greenColor];  
  16.     [self.view addSubview:view1];  
  17.       
  18.     label1 = [[UILabel alloc]initWithFrame:CGRectMake(10, 100, 100, 100)];  
  19. //    label1.text = @"a";  
  20.     label1.backgroundColor = [UIColor redColor];  
  21.     [view1 addSubview:label1];  
  22.       
  23.     label2 = [[UILabel alloc]initWithFrame:CGRectMake(110, 100, 100, 100)];  
  24. //    label2.text = @"b";  
  25.     label2.backgroundColor = [UIColor blueColor];  
  26.     [view1 addSubview:label2];  
  27.       
  28.     label3 = [[UILabel alloc]initWithFrame:CGRectMake(10, 200, 100, 100)];  
  29. //    label3.text = @"c";  
  30.     label3.backgroundColor = [UIColor purpleColor];  
  31.     [view1 addSubview:label3];  
  32.   
  33.     label4 = [[UILabel alloc]initWithFrame:CGRectMake(110, 200, 100, 100)];  
  34. //    label4.text = @"d";  
  35.     label4.backgroundColor = [UIColor yellowColor];  
  36.     [view1 addSubview:label4];  
  37.       
  38.     button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];  
  39.     button1.frame = CGRectMake(10, 320, 80, 30);  
  40.     [button1 setTitle:@"顺时针" forState:UIControlStateNormal];  
  41.     [button1 addTarget:self action:@selector(change) forControlEvents:UIControlEventTouchUpInside];  
  42.     [view1 addSubview:button1];  
  43.       
  44.     button2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];  
  45.     button2.frame = CGRectMake(130, 320, 80, 30);  
  46.     [button2 setTitle:@"逆时针" forState:UIControlStateNormal];  
  47.     [button2 addTarget:self action:@selector(changele) forControlEvents:UIControlEventTouchUpInside];  
  48.     [view1 addSubview:button2];  
  49.       
  50.     // Do any additional setup after loading the view, typically from a nib.  
  51. }  
  52. //顺时针  
  53. -(void)change  
  54. {  
  55.     if (bool1 == YES) {  
  56.         [UIView beginAnimations:@"testA" context:NULL];//开始动画  
  57.         [UIView setAnimationDuration:1];//动画持续时间  
  58.         [UIView setAnimationCurve:UIViewAnimationCurveLinear];  
  59.         [UIView setAnimationDelegate:self];//动画代表  
  60. //        [UIView setAnimationRepeatAutoreverses:YES];//设置动画重复自动反转  
  61.         [UIView setAnimationRepeatCount:99];//循环次数  
  62.         a = [[UIView alloc]init];  
  63.         a.center = label1.center;  
  64.         label1.center = label2.center;  
  65.         label2.center = label4.center;  
  66.         label4.center = label3.center;  
  67.         label3.center = a.center;  
  68.         [UIView commitAnimations];  
  69.         [button1 setTitle:@"返回" forState:UIControlStateNormal];  
  70.         [a release];  
  71.         bool1 = NO;  
  72.     }  
  73.     else {  
  74.         [UIView beginAnimations:@"testA" context:NULL];  
  75.         [UIView setAnimationDuration:1];  
  76.         [UIView setAnimationCurve:UIViewAnimationCurveLinear];  
  77.         [UIView setAnimationDelegate:self];  
  78.         [UIView setAnimationRepeatAutoreverses:NO];  
  79.         [UIView setAnimationRepeatCount:0];  
  80.         a = [[UIView alloc]init];  
  81.         a.center = label1.center;  
  82.         label1.center = label2.center;  
  83.         label2.center = label4.center;  
  84.         label4.center = label3.center;  
  85.         label3.center = a.center;  
  86.         [UIView commitAnimations];  
  87.         [button1 setTitle:@"顺时针" forState:UIControlStateNormal];  
  88.         [a release];  
  89.         bool1 = YES;  
  90.     }  
  91. }  
  92.   
  93. //逆时针  
  94. -(void)changele  
  95. {  
  96.     if (bool1 == YES) {  
  97.         [UIView beginAnimations:@"testA" context:NULL];  
  98.         [UIView setAnimationDuration:1];  
  99.         [UIView setAnimationCurve:UIViewAnimationCurveLinear];  
  100.         [UIView setAnimationDelegate:self];  
  101.         [UIView setAnimationRepeatAutoreverses:NO];  
  102.         [UIView setAnimationRepeatCount:99];  
  103.         a = [[UIView alloc]init];  
  104.         a.center = label3.center;  
  105.         label3.center = label4.center;  
  106.         label4.center = label2.center;  
  107.         label2.center = label1.center;  
  108.         label1.center = a.center;  
  109.         [UIView commitAnimations];  
  110.         [button2 setTitle:@"返回" forState:UIControlStateNormal];  
  111.         [a release];  
  112.         bool1 = NO;  
  113.     }  
  114.     else {  
  115.         [UIView beginAnimations:@"testA" context:NULL];  
  116.         [UIView setAnimationDuration:1];  
  117.         [UIView setAnimationCurve:UIViewAnimationCurveLinear];  
  118.         [UIView setAnimationDelegate:self];  
  119.         [UIView setAnimationRepeatAutoreverses:NO];  
  120.         [UIView setAnimationRepeatCount:0];  
  121.         a = [[UIView alloc]init];  
  122.         a.center = label3.center;  
  123.         label3.center = label4.center;  
  124.         label4.center = label2.center;  
  125.         label2.center = label1.center;  
  126.         label1.center = a.center;  
  127.         [UIView commitAnimations];  
  128.         [button2 setTitle:@"逆时针" forState:UIControlStateNormal];  
  129.         [a release];  
  130.         bool1 = YES;  
  131.     }  
  132.   
  133. }  
  134. -(void)dealloc  
  135. {  
  136.     [view1 release];  
  137.     [label1 release];  
  138.     [label2 release];  
  139.     [label3 release];  
  140.     [label4 release];  
  141.     [super dealloc];  
  142. }  
  143.   
  144.   
  145.   
  146. - (void)viewDidUnload  
  147. {  
  148.     [super viewDidUnload];  
  149.     // Release any retained subviews of the main view.  
  150. }  
  151.   
  152. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation  
  153. {  
  154.     return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);  
  155. }  
  156.   
  157. @end  
原创粉丝点击