美文网首页
两个UIView点击动画切换

两个UIView点击动画切换

作者: 小猿猴 | 来源:发表于2017-01-05 16:29 被阅读398次
    效果图
    代码
    #import "ViewController.h"
    
    @interface ViewController (){
        UIButton *btn_Rock;
    }
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        //先把需要的视图初始化好
        self.oneView = [[OneView alloc] init];
        self.twoView = [[TwoView alloc] init];
        //添加的时候先只添加第一个视图
        [self.view addSubview:self.oneView];
        [self.view sendSubviewToBack:self.oneView];
        
        
        
        //先创建一个自定义类型(UIButtonTypeCustom)的按钮!
        btn_Rock = [UIButton buttonWithType:UIButtonTypeCustom];
        //设置自定义类型按钮的宽高!
        btn_Rock.frame = CGRectMake(0, 0, 32, 28);
        //设置自定义类型按钮的图片!
        [btn_Rock setBackgroundColor:[UIColor greenColor]];
        //添加按钮的点击事件!
        [btn_Rock addTarget:self action:@selector(btn_RockClick:) forControlEvents:UIControlEventTouchUpInside];
        UIBarButtonItem *item = [[UIBarButtonItem alloc]initWithCustomView:btn_Rock];
        [self.navigationItem setRightBarButtonItem:item];
        self.title = @"活动";
       
        
    }
    
    -(void)btn_RockClick:(id)sender{
        NSLog(@"点击翻转");
        //防止暴力点击切换,导致视图切换出错,所以需要加个小技巧,当点击一次切换后就把按钮的交互给关了!
        btn_Rock.userInteractionEnabled = NO;
        //执行切换视图的函数
        [self doTransitionWithType];
        //用定时器1秒(与切换视图的动画一致)之后才唤醒切换按钮的交互事件
        [self performSelector:@selector(isClick) withObject:nil afterDelay:1.0f];
    
    }
    
    -(void)isClick{
        //重新唤醒按钮的交互事件!
        btn_Rock.userInteractionEnabled = YES;
    }
    
    -(void)doTransitionWithType{
        if ([[self.view subviews] containsObject:self.twoView ]) {
            
            /**
             UIView切换动画
             @param FromView   从 twoView 视图开始
             @param toView     切换到 oneView 视图
             @param duration   1秒的动画时间
             @param options    动画方式----有很多总,这里选择了从右开始翻转
             @param completion 当执行完动画之后删除之前的 twoView
             @return UIView
             */
            [UIView transitionFromView:self.twoView
                                toView:self.oneView
                              duration:1
                               options:UIViewAnimationOptionTransitionFlipFromRight
                            completion:^(BOOL finished){
                                [self.twoView removeFromSuperview];
                            }];
            [self.view addSubview:self.oneView];
            [self.view sendSubviewToBack:self.oneView];
        }
        else{
            
            /**
            UIView切换动画
             @param FromView   从 oneView 视图开始
             @param toView     切换到 twoView 视图
             @param duration   1秒的动画时间
             @param options    动画方式----有很多总,这里选择了从左开始翻转
             @param completion 当执行完动画之后删除之前的 oneView
             @return UIView
             */
            [UIView transitionFromView:self.oneView
                                toView:self.twoView
                              duration:1
                               options:UIViewAnimationOptionTransitionFlipFromLeft
                            completion:^(BOOL finished){
                                [self.oneView removeFromSuperview];
                            }];
            [self.view addSubview:self.twoView];
            [self.view sendSubviewToBack:self.twoView];
            
        }
    }
    
    @end
    

    相关文章

      网友评论

          本文标题:两个UIView点击动画切换

          本文链接:https://www.haomeiwen.com/subject/noqzvttx.html