zoukankan      html  css  js  c++  java
  • UIImageView 与 CABasicAnimation 实现多层视图,带动画效果objec iOS

    - (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 ;
    
    }
     
  • 相关阅读:
    PHP实现同array_column一样的功能
    PHP使用FTP上传文件到服务器(实战篇)
    PHP计算今天、昨天、本周、本月、上月开始时间和结束时间
    PHP计算两个日期相差的年月日时分秒
    mysql命令行复制数据库
    Mysql中反引号和单引号的区别
    Linux 使用 selenium 环境配置
    SVN 提交文件报错:svn: E155015: Aborting commit:
    Django:Nginx 启动,无法加载样式,无法加载静态文件
    Django:Could not find backend 'django_redis.cache.RedisCache': cannot import name 'six'
  • 原文地址:https://www.cnblogs.com/qingjoin/p/UIImageView_CABasicAnimation.html
Copyright © 2011-2022 走看看