aBigRoybot

  博客园 :: 首页 :: 新随笔 :: 联系 :: 订阅 :: 管理 ::

[转]UIScrollView+2个效果。

研究了切换效果。自己写了2个:一个是用UIView淡入。。。一个是用CATransition(加入须QuartzCore.framework);

UIView 是子视图逐个淡入,但是效果不太好。可能时间设计不好,还是有其他更佳方法,还请各为不惜赐教。

CATransition 是随机交换各个对象。其实就是随机分配背景图,在根据背景图名称,给按钮设计Tag值。

让各位见笑了:

UI- (void)viewDidAppear:(BOOL)animated

{

//不要scrollView 请注释此句。

[(UIScrollView*)self.view setContentSize:CGSizeMake(320450)];    NSMutableArray * arr =[[NSMutableArray alloc]init];

    float offset = 10;

    int cellPadingY=10;

    for (int i = 0; i < 9; i++) 

    {

        CGRect rect = CGRectMake(offset, cellPadingY, 9090);

        button = [[UIButton alloc]initWithFrame:rect];

        [button setTitle:[[NSString alloc]initWithFormat:@"btn%i",i] forState:UIControlStateNormal];

        [button setTitleColor:[UIColor redColorforState:UIControlStateNormal];

        [button addTarget:self action:@selector(buttonOnClick:) forControlEvents:UIControlEventTouchUpInside];

        //button.backgroundColor = [UIColor clearColor];

        if (i%2==0) {

            [button setBackgroundImage:[UIImage imageNamed:@"2.jpg"]forState:UIControlStateNormal];

        }

        else[button setBackgroundImage:[UIImage imageNamed:@"1.jpg"]forState:UIControlStateNormal];

        button.alpha=0;

        //为后面按钮的视图控制器做准备。。。。

        button.tag = i+ 100//加上100 以免跟其它tag一样 

        [self.view  addSubview:button];

        [arr addObject:button];

        offset += 90 + 15;

        if(offset>320){

            offset=10;

            cellPadingY+=100;

        }   

    }

    

    NSMutableArray *arrTime=[[NSMutableArray alloc]initWithObjects:@"1",@"2",@"2.5",@"3",@"3.5",@"4",@"4.5",@"5",@"5.5" ,nil];

    int m = [arrTime count];  

    while(--m > 0) {  

        int n = rand()%(m+1);  

        [arrTime exchangeObjectAtIndex:m withObjectAtIndex:n];

    }

    [UIView beginAnimations:@"moive" context:nil];

    for (int i=0; i<[arr count]; i++) {

        [UIView setAnimationDuration:[[arrTime objectAtIndex:i]  intValue]];

        [UIView setAnimationCurve:UIViewAnimationCurveEaseIn];

        //Make the animatable changes.

        [[arr objectAtIndex:i]setAlpha:1];

    }

    [UIView commitAnimations];

  [super viewDidAppear:animated];

}

//-------------------------分割线---------------------

//CATransition

// 请自行导入框架和头文件。

- (void)viewDidAppear:(BOOL)animated

{

 

    

    

    NSMutableArray * arr =[[NSMutableArray alloc]initWithObjects:@"2.jpg",@"1.jpg",@"4.jpg",@"3.jpg" ,nil];

    int k = [arr count];  

    while(--k > 0) {  

        int j = rand()%(k+1);  

        [arr exchangeObjectAtIndex:k withObjectAtIndex:j];

    }

    

    

    float offset = 10;

    int cellPadingY=10;

    for (int i = 0; i < [arr count]; i++) 

    {

        CGRect rect = CGRectMake(offset, cellPadingY, 9090);

        button = [[UIButton alloc]initWithFrame:rect];

        [button setTitle:[[NSString alloc]initWithFormat:@"btn%i",i] forState:UIControlStateNormal];

        [button setTitleColor:[UIColor redColorforState:UIControlStateNormal];

        [button addTarget:self action:@selector(buttonOnClick:) forControlEvents:UIControlEventTouchUpInside];

        //button.backgroundColor = [UIColor clearColor];

        [button setBackgroundImage:[UIImage imageNamed:[arr objectAtIndex:i] ]forState:UIControlStateNormal];//设置图片,取数组的第i张图片 [array objectAtIndex:i];

        

        //为后面按钮的视图控制器做准备。。。。

        NSString *objName=[[NSString alloc]initWithFormat:@"%@",[arr objectAtIndex:i]];

        int tagValue=0;

        if([objName isEqualToString:@"1.jpg"]){tagValue=1;};

        if([objName isEqualToString:@"2.jpg"]){tagValue=2;};

        if([objName isEqualToString:@"3.jpg"]){tagValue=3;};

        if([objName isEqualToString:@"4.jpg"]){tagValue=4;};

        button.tag = tagValue + 100//加上100 以免跟其它tag一样 

        [self.view  addSubview:button];

        

        //.................................................................

        offset += 90 + 15;

        if(offset>320){

            offset=10;

            cellPadingY+=100;

        }

        

        

        

    }

    CATransition *transition = [CATransition animation];

    transition.duration = 3;

    transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];

    transition.type = kCATransitionFade;

    transition.delegate = self;

    [self.view.layer  addAnimation:transition forKey:nil];

    

    [super viewDidAppear:animated];

}

posted on 2011-11-03 13:42  aBigRoybot  阅读(267)  评论(0)    收藏  举报