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

#import "ViewController.h"

@interface ViewController ()

@end
static 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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值