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 ;
    
    }
     
  • 相关阅读:
    双飞翼布局和圣杯布局的对比
    阿里云centos+java环境搭建
    Android零散知识点积累
    [转]linux shell 流程控制(条件if,循环【for,while】,选择【case】语句实例
    [转]linux shell 获取当前正在执行脚本的绝对路径
    [转+整理]linux shell 将字符串分割成数组
    [转]linux shell 数组建立及使用技巧
    linux shell 入门
    jquery 字符串转为json
    JQuery插件开发入门
  • 原文地址:https://www.cnblogs.com/qingjoin/p/UIImageView_CABasicAnimation.html
Copyright © 2011-2022 走看看