IOS 霓虹灯效果


//初始化视图
- (void) initViews{
    //红色视图
    UIView *redView=[[UIView alloc] initWithFrame:CGRectMake(20, 20, 280, 280)];
    redView.backgroundColor=[UIColor redColor];

//视图的标签,标示一个视图对象

    redView.tag=1;
    [self.view addSubview:redView];
    [redView release];
    
    
    //橙色视图
    UIView *oriangeView=[[UIView alloc] initWithFrame:CGRectMake(20, 20, 240, 240)];
    oriangeView.backgroundColor=[UIColor orangeColor];
    oriangeView.tag=2;
    [redView addSubview:oriangeView];
    [oriangeView release];
    
    //黄色视图
    UIView *yellowView=[[UIView alloc] initWithFrame:CGRectMake(20, 20, 200, 200)];
    yellowView.backgroundColor=[UIColor yellowColor];
    yellowView.tag=3;
    [oriangeView addSubview:yellowView];
    [yellowView release];
    
    
    //绿色视图
    UIView *greenView=[[UIView alloc] initWithFrame:CGRectMake(20,20, 160, 160)];
    greenView.backgroundColor=[UIColor greenColor];
    greenView.tag=4;
    [yellowView addSubview:greenView];
    [greenView release];
    
    //青色视图
    UIView *cyanView=[[UIView alloc] initWithFrame:CGRectMake(20, 20, 120, 120)];
    cyanView.backgroundColor=[UIColor cyanColor];
    cyanView.tag=5;
    [greenView addSubview:cyanView];
    [cyanView release];
    
    //蓝色视图
    UIView *blueView=[[UIView alloc] initWithFrame:CGRectMake(20, 20, 80, 80)];
    blueView.backgroundColor=[UIColor blueColor];
    blueView.tag=6;
    [cyanView addSubview:blueView];
    [blueView release];
    
    //紫色视图
    
    UIView *purpleView=[[UIView alloc] initWithFrame:CGRectMake(20, 20, 40, 40)];
    purpleView.backgroundColor=[UIColor purpleColor];
    purpleView.tag=7;
    [blueView addSubview:purpleView];
    [purpleView release];
    
    
}

//实现翻页效果

- (void)xuanzhuang:(id) sender{
    
    //获取当前画图的设备上下文
    CGContextRef context=UIGraphicsGetCurrentContext();
    //开始准备动画
    [UIView beginAnimations:@"*******" context:context];
    
    
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    
    //设置动画持续时间
    
    [UIView setAnimationDuration:0.5];
    
    //因为没有给该Controller添加实例变量,所以用以下方法获得View
    
    UIView *parentView=[self.view viewWithTag:1];
    
    //设置动画效果
    
    [UIView setAnimationTransition:UIViewAnimationTransitionCurlDown forView:parentView cache:YES]; //从上到下
    [UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:parentView cache:YES]; //从下往上
    
//   [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:parentView cache:YES]; //从左到右
//   [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:parentView cache:YES]; //从右到左
    
    //设置动画委托
    [UIView setAnimationDelegate:self];
    
    //当动画啊结束时 调用animationFinished方法
    [UIView setAnimationDidStopSelector:@selector(animationFinished:)];
    
    
    //提交动画
    
    [ UIView commitAnimations];
    
    
}

//处理颜色的改变
- (void) showChangeView:(id) sender{
    [self xuanzhuang:nil];
    NSLog(@"调用showChange函数!!");

//每次调用,当前颜色加1

    currentColor++;
    if (currentColor>6) {
        currentColor=0;
    }
    colorArray=[NSArray arrayWithObjects:[UIColor redColor],[UIColor orangeColor],[UIColor yellowColor],[UIColor greenColor],[UIColor cyanColor],[UIColor blueColor],[UIColor purpleColor], nil];
    
    //初始化视图数组
    for (int i=1; i<=7; i++) {

//存的是7个视图的对象

        [viewArray addObject:[self.view viewWithTag:i]];
        
    }

 
    
//    NSLog(@"颜色数组大小为:%d",[ colorArray count]);
//    NSLog(@"视图大小为:%d",[viewArray count]);
    

    //关键代码,循环设置背景色
    for (int i=0; i<7-currentColor; i++) {
        [[viewArray objectAtIndex:i] setBackgroundColor:[colorArray objectAtIndex:i+currentColor]];
    }
    
    for (int i=6-currentColor,j=0; i<7;j++,i++) {
        [[viewArray objectAtIndex:i] setBackgroundColor:[colorArray objectAtIndex:j]];
    }
        
 }


- (void)viewDidLoad
{
    [super viewDidLoad];
    NSLog(@"调用viewDidLoad方法!!!");
    
    self.view=[[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 460 )];
    self.view.backgroundColor=[UIColor clearColor];
    //初始化视图
    [self initViews];
        
           //定义一个定时器每隔0.5秒循环调用showChangeView:方法,这个定时器的用法如果还不是很熟悉的话,可以看我的 其他文章,有对他的详细讲解
    
    NSTimer *timer=[NSTimer  timerWithTimeInterval:0.5 target:self selector:@selector(showChangeView:) userInfo:nil repeats:YES];
    NSRunLoop *runloop=[NSRunLoop currentRunLoop];
   [runloop addTimer:timer forMode:NSDefaultRunLoopMode];


}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值