按鈕相互切換

#import "ChangeViewController.h"
#import "SuccessViewController.h"
#import "FailedViewController.h"
#import "DoingViewController.h"

#define Width [UIScreen mainScreen].bounds.size.width
@interface ChangeViewController ()
{
    SuccessViewController *successVC;
    FailedViewController *failedVC;
    DoingViewController *doVC;
}
@property (weak, nonatomic) IBOutlet UIButton *sucBtn;
@property (weak, nonatomic) IBOutlet UIButton *failedBtn;
@property (weak, nonatomic) IBOutlet UIButton *doBtn;
@property (weak, nonatomic) IBOutlet UIView *baseView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *leading;

@end

@implementation ChangeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    self.sucBtn.selected = YES;
    self.failedBtn.selected = NO;
    self.doBtn.selected = NO;
    
    successVC = [[SuccessViewController alloc] initWithNibName:@"SuccessViewController" bundle:nil];
    failedVC = [[FailedViewController alloc] initWithNibName:@"FailedViewController" bundle:nil];
    doVC = [[DoingViewController alloc] initWithNibName:@"DoingViewController" bundle:nil];
    
    successVC.view.frame = CGRectMake(0, 0, _baseView.frame.size.width, _baseView.frame.size.height);
    failedVC.view.frame = CGRectMake(0, 0, _baseView.frame.size.width, _baseView.frame.size.height);
    doVC.view.frame = CGRectMake(0, 0, _baseView.frame.size.width, _baseView.frame.size.height);
    
    //先加上的在最底部
    [_baseView addSubview:doVC.view];
    [_baseView addSubview:failedVC.view];
    [_baseView addSubview:successVC.view];
    
    //加入子控制器
    [self addChildViewController:successVC];
    [self addChildViewController:failedVC];
    [self addChildViewController:doVC];
    
    // Do any additional setup after loading the view from its nib.
}
- (IBAction)changeAction:(id)sender {
    UIButton *button = (UIButton *)sender;
    NSInteger index = button.tag - 100;
    if (index == 0) {
        self.sucBtn.selected = YES;
        self.failedBtn.selected = NO;
        self.doBtn.selected = NO;
        [_baseView bringSubviewToFront:successVC.view];
    }else if (index == 1) {
        self.sucBtn.selected = NO;
        self.failedBtn.selected = YES;
        self.doBtn.selected = NO;
        [_baseView bringSubviewToFront:failedVC.view];
    }else {
        self.sucBtn.selected = NO;
        self.failedBtn.selected = NO;
        self.doBtn.selected = YES;
        [_baseView bringSubviewToFront:doVC.view];
    }
    
    //滑動條的動畫
    CGFloat leadWidth = Width/3;
    [UIView animateWithDuration:0.33 animations:^{
        _leading.constant = index*leadWidth;
        [self.view layoutIfNeeded];
    }];
    
}

xib文件動畫

文件概覽atom

相關文章
相關標籤/搜索