UIScrollView+2个效果。

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

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

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

让各位见笑了:

 1 UI- (void)viewDidAppear:(BOOL)animated

 2 

 3 {

 4 //不要scrollView 请注释此句。

 5 [(UIScrollView*)self.viewsetContentSize:CGSizeMake(320, 450)];    NSMutableArray * arr =[[NSMutableArrayalloc]init];

 6     float offset = 10;

 7     int cellPadingY=10;

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

 9     {

10         CGRect rect = CGRectMake(offset, cellPadingY, 90, 90);

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

12         [buttonsetTitle:[[NSStringalloc]initWithFormat:@"btn%i",i] forState:UIControlStateNormal];

13         [buttonsetTitleColor:[UIColorredColor] forState:UIControlStateNormal];

14         [buttonaddTarget:selfaction:@selector(buttonOnClick:) forControlEvents:UIControlEventTouchUpInside];

15         //button.backgroundColor = [UIColor clearColor];

16         if (i%2==0) {

17             [buttonsetBackgroundImage:[UIImageimageNamed:@"2.jpg"]forState:UIControlStateNormal];

18         }

19         else[buttonsetBackgroundImage:[UIImageimageNamed:@"1.jpg"]forState:UIControlStateNormal];

20         button.alpha=0;

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

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

23         [self.view  addSubview:button];

24         [arr addObject:button];

25         offset += 90 + 15;

26         if(offset>320){

27             offset=10;

28             cellPadingY+=100;

29         }   

30     }

31     

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

33     int m = [arrTime count];  

34     while(--m > 0) {  

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

36         [arrTime exchangeObjectAtIndex:m withObjectAtIndex:n];

37     }

38     [UIViewbeginAnimations:@"moive"context:nil];

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

40         [UIViewsetAnimationDuration:[[arrTime objectAtIndex:i]  intValue]];

41         [UIViewsetAnimationCurve:UIViewAnimationCurveEaseIn];

42         //Make the animatable changes.

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

44     }

45     [UIViewcommitAnimations];

46   [super viewDidAppear:animated];

47 }

 

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

//CATransition

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

- (void)viewDidAppear:(BOOL)animated

{



    

    

    NSMutableArray * arr =[[NSMutableArrayalloc]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, 90, 90);

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

        [buttonsetTitle:[[NSStringalloc]initWithFormat:@"btn%i",i] forState:UIControlStateNormal];

        [buttonsetTitleColor:[UIColorredColor] forState:UIControlStateNormal];

        [buttonaddTarget:selfaction:@selector(buttonOnClick:) forControlEvents:UIControlEventTouchUpInside];

        //button.backgroundColor = [UIColor clearColor];

        [buttonsetBackgroundImage:[UIImageimageNamed:[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 = [CATransitionanimation];

    transition.duration = 3;

    transition.timingFunction = [CAMediaTimingFunctionfunctionWithName:kCAMediaTimingFunctionEaseOut];

    transition.type = kCATransitionFade;

    transition.delegate = self;

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

    

    [super viewDidAppear:animated];

}

你可能感兴趣的:(uiscrollview)