UIImageView 与 CABasicAnimation 实现多层视图,带动画效果objec iOS

来源:互联网 发布:mac 网线转接头 编辑:程序博客网 时间:2024/05/18 15:53
- (void)viewDidLoad{    [super viewDidLoad];    int i;    int n = 0;        UIScrollView *newscrollview = [[UIScrollView alloc]init];    newscrollview.frame = CGRectMake(0, 0, 320, 400);    [self.view addSubview:newscrollview];            newscrollview.backgroundColor = [UIColor brownColor];        //button.backgroundColor = [UIColor redColor];    //button.tag = 0;        //关联事件。。butttest        for(i = 0;     i<20; i++)    {        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];        [newscrollview addSubview:button];        button.frame = CGRectMake(0, 1+n, 280, 110);        button.backgroundColor = [UIColor redColor];        //与事件关联  调用buttest方法        [button addTarget:self action:@selector(butttest:) forControlEvents:UIControlEventTouchUpInside];                button.tag = i;        //在视图里加载imageview视图        UIImageView *uiimageview = [[UIImageView alloc]init];        uiimageview.frame = CGRectMake(1, 1+n, 90, 110);                        UIImageView *uiimageview1 = [[UIImageView alloc]init];        UIImageView *uiimageview2 = [[UIImageView alloc]init];        uiimageview1.frame = CGRectMake(92, 1+n, 90, 110);        uiimageview2.frame = CGRectMake(185, 1+n, 90, 110);        [newscrollview addSubview:uiimageview1];        [newscrollview addSubview:uiimageview2];        uiimageview1.image = [UIImage imageNamed:@"three.png"];        uiimageview2.image = [UIImage imageNamed:@"four.png"];        [newscrollview addSubview:uiimageview];        //uiimageview.backgroundColor = [UIColor yellowColor];        uiimageview.image = [UIImage imageNamed:@"two.png"];               n = n + 113;    }    [newscrollview setContentSize:CGSizeMake(320, 2000)];    newscrollview.showsVerticalScrollIndicator = NO;     // Do any additional setup after loading the view from its nib.}-(IBAction)butttest:(id)sender{       NSLog(@"next add a big button ...\n");    static int n=0 ;    if(maximgbutton == nil)    {        maximgbutton = [UIButton buttonWithType:UIButtonTypeCustom];        maximgbutton.frame = CGRectMake(0, 0, 320, 400);        //maximgbutton.backgroundColor = [UIColor redColor];        [self.view addSubview:maximgbutton];            }        if( 2== ((UIButton*)sender).tag )    {        [maximgbutton addTarget:self action:@selector(removebutton:) forControlEvents:UIControlEventTouchUpInside];        NSLog(@"successful");        if(imageviewts ==nil)        {            n++;                                    CABasicAnimation *theAnimation;            theAnimation=[CABasicAnimation animationWithKeyPath:@"transform.translation.x"];            theAnimation.fromValue = [NSNumber numberWithFloat:0.09];            //移动的幅度            theAnimation.toValue = [NSNumber numberWithFloat:320];            //x,y轴缩小到0.1,Z 轴不变            //theAnimation.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeScale(320, 400, 1.0)];            //持续时间            theAnimation.duration = 2;            //重复次数            theAnimation.repeatCount = 1;            theAnimation.removedOnCompletion = NO;                        theAnimation.autoreverses = YES;                        imageviewts = [[UIImageView alloc]init];            imageviewts = [imageviewts autorelease];            imageviewts.frame = CGRectMake(0, 0, 320, 400);            imageviewts.image = [UIImage imageNamed:@"two.png"];            [self.view addSubview:imageviewts];                                     [imageviewts.layer addAnimation:theAnimation forKey:nil];            NSLog(@"nnnnnn%i",n);                                            }        else         {            imageviewts.frame = CGRectMake(0, 0, 320, 400);            imageviewts.image = [UIImage imageNamed:@"two.png"];            [self.view addSubview:imageviewts];        }         }}-(IBAction)removebutton:(id)sender{    NSLog(@"remove....button\n");    static int Calculator=0;    Calculator ++;    if(Calculator%2 !=0)    {        [imageviewts removeFromSuperview];        [maximgbutton removeFromSuperview];        maximgbutton =nil;           imageviewts = nil ;    }    NSLog(@"Calculator%i",Calculator);    }


-(IBAction)removebutton:(id)sender{    NSLog(@"remove....button\n");    static int Calculator=0;    Calculator ++;    if(Calculator%2 !=0)    {         CABasicAnimation *theAnimation;        theAnimation=[CABasicAnimation animationWithKeyPath:@"transform.translation.x"];                theAnimation.fromValue = [NSNumber numberWithFloat:0];        //移动的幅度        theAnimation.toValue = [NSNumber numberWithFloat:0];        //x,y轴缩小到0.1,Z 轴不变        //theAnimation.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeScale(320, 400, 1.0)];        //持续时间        theAnimation.duration = 2;        //重复次数        theAnimation.repeatCount = 1;        //theAnimation.delegate = self;        //把imageviews这个视图加上theAnimation这个动画效果            [imageviewts.layer addAnimation:theAnimation forKey:nil];        //开始加载动画效果        [UIView beginAnimations:nil context:nil];        [UIView setAnimationDuration:1];        [UIView setAnimationDelegate:self];        //view 大小 位置        imageviewts.frame = CGRectMake(-320, 0, 320, 400);         //当动画停止的时候,调用hideAnimationStopped事件        [UIView setAnimationDidStopSelector:@selector(hideAnimationStopped)];        [UIView commitAnimations];    }    NSLog(@"Calculator%i",Calculator);    }
-(void)hideAnimationStopped{    NSLog(@"imageViewStop");    [imageviewts removeFromSuperview];    [maximgbutton removeFromSuperview];     maximgbutton =nil;       imageviewts = nil ;}