按钮翻转并控制视图的翻转(带动画)Demo

实现原理:
1.定义两个UIView:view1,view2(大小跟self.view一样),添加到self.view,并设置它们的tag值标识和隐藏的属性.
2.自定义右侧导航栏按钮.定义一个视图customView,在创建两个按钮添加在customView,然后在添加在自带右侧导航栏.
3, 两个按钮的响应方法是同一个,在响应方法中通过获取tag值得到相应的控件,再根据按钮的隐藏值来判断左翻还是又翻的动画效果,最后在配置动画,在动画的动作中将两个按钮和视图的隐藏值相互取反,来达到点击按钮视图和按钮也翻转效果.

//
//  ViewController.m
//  ViewFipDemo
//
//  Created by mac on 16/7/28.
//  Copyright © 2016年 huang. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self _createNavRightItem];
    [self _createView];
}

- (void) _createView{
    UIView *view1 = [[UIView alloc] initWithFrame:self.view.frame];
    view1.backgroundColor = [UIColor redColor];
    view1.tag = 2000;
    view1.hidden = YES;
    [self.view addSubview:view1];

    UIView *view2 = [[UIView alloc] initWithFrame:self.view.frame];
    view2.backgroundColor = [UIColor greenColor];
    view2.tag = 2001;
    view2.hidden = NO;
    [self.view addSubview:view2];
}

- (void) _createNavRightItem {

    UIView *customView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 49, 32)];
    customView.backgroundColor = [UIColor clearColor];
    customView.tag = 1000;
    //创建button
    UIButton *postButton = [UIButton buttonWithType:UIButtonTypeCustom];
    //设置button的frame
    postButton.frame = customView.bounds;
    //设置图片
    [postButton setImage:[UIImage imageNamed:@"poster_home"]forState:UIControlStateNormal];
     [postButton setBackgroundImage:[UIImage imageNamed:@"exchange_bg_home"] forState:UIControlStateNormal];
    postButton.tag = 1001;
    postButton.hidden = YES;
    //设置响应事件
    [postButton addTarget:self
            action:@selector(button1Action:)
  forControlEvents:UIControlEventTouchUpInside];
    //添加到视图上
    [customView addSubview:postButton];

    //创建button
    UIButton *listButton = [UIButton buttonWithType:UIButtonTypeCustom];
    //设置button的frame
    listButton.frame = customView.bounds;
    //设置图片
    [listButton setImage:[UIImage imageNamed:@"list_home"]forState:UIControlStateNormal];
    [listButton setBackgroundImage:[UIImage imageNamed:@"exchange_bg_home"] forState:UIControlStateNormal];
    listButton.tag = 1002;
    listButton.hidden = NO;
    //设置响应事件
    [listButton addTarget:self
                   action:@selector(button1Action:)
         forControlEvents:UIControlEventTouchUpInside];
    //添加到视图上
    [customView addSubview:listButton];

    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:customView];

}

- (void) button1Action:(UIButton *)button {
    UIView *customView = [self.navigationController.navigationBar viewWithTag:1000];
     UIView *view1 = [self.view viewWithTag:2000];
     UIView *view2 = [self.view viewWithTag:2001];
    UIButton *postBtn = [self.navigationController.navigationBar  viewWithTag:1001];
    UIButton *listBtn = [self.navigationController.navigationBar  viewWithTag:1002];
    BOOL flip = postBtn.hidden;
    UIViewAnimationOptions option;

    if (flip) {
        option = UIViewAnimationOptionTransitionFlipFromLeft;
    }else{
        option = UIViewAnimationOptionTransitionFlipFromRight;
    }
    [UIView transitionWithView:customView duration:0.35 options:option animations:^{
        postBtn.hidden = !postBtn.hidden;
        listBtn.hidden = !listBtn.hidden;
    } completion:nil];

    [UIView transitionWithView:self.view duration:0.3 options:option animations:^{
        view1.hidden = !view1.hidden;
        view2.hidden = !view2.hidden;
    } completion:nil];    
    }
@end

运行效果:
这里写图片描述

这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值