利用addviewcontroller实现类似头条不同view的切换

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#import "ViewController.h"
#import "SecViewController.h"
#import "FirstViewController.h"
#import "ThirdViewController.h"
@interface ViewController ()
@property(nonatomic,weak)UIScrollView *scrollView;
@property(nonatomic,strong)FirstViewController* firstViewController;
@property(nonatomic,strong)SecViewController* secViewController;
@property(nonatomic,strong)ThirdViewController *thirdViewController;
@property(nonatomic,strong)UIViewController*tempVc;
@end
 
@implementation ViewController
 
- (void)viewDidLoad {
    [super viewDidLoad];
    UIScrollView *scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 30, self.view.frame.size.width, 40)];
    self.scrollView = scrollView;
    self.scrollView.backgroundColor = [UIColor purpleColor];
    self.scrollView.contentSize = CGSizeMake(560, 40);
    self.scrollView.bounces = NO;
    self.scrollView.pagingEnabled = YES;
    [self.view addSubview:self.scrollView];
    for (int i = 0; i<6; i++) {
        UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(i*70, 0, 70, 40)];
        [self.scrollView addSubview:button];
        [button setTitle:[NSString stringWithFormat:@"按钮%d",i] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        button.tag = i;
        [button addTarget:self action:@selector(didClick:) forControlEvents:UIControlEventTouchUpInside];
         }
    FirstViewController *firstVc = [[FirstViewController alloc]init];
    self.firstViewController = firstVc;
    [self.firstViewController.view setFrame:CGRectMake(0, 104, 320, 464)];
    SecViewController *secVc = [[SecViewController alloc]init];
    self.secViewController = secVc;
    [self.secViewController.view setFrame:CGRectMake(0, 104, 320, 464)];
    ThirdViewController *thirdVc = [[ThirdViewController alloc]init];
    self.thirdViewController =thirdVc;
    [self.thirdViewController.view setFrame:CGRectMake(0, 104, 320, 464)];
    [self addChildViewController:firstVc];
    [self addChildViewController:secVc];
    [self addChildViewController:thirdVc];
    self.tempVc = firstVc;
    [self.view addSubview:self.tempVc.view];
    }
-(void)didClick:(UIButton *)button{
    if ((self.tempVc == self.firstViewController && button.tag == 0)||(self.tempVc == self.secViewController && button.tag == 1)||(self.tempVc == self.thirdViewController && button.tag ==2)) {
        return;
    }else{
    switch (button.tag) {
        case 0:
            [self replaceController:self.tempVc newController:self.firstViewController];
            break;
           case 1:
            [self replaceController:self.tempVc newController:self.secViewController];
            break;
            case 2:
            [self replaceController:self.tempVc newController:self.thirdViewController];
        default:
            break;
      }
    }
}
 
- (void)replaceController:(UIViewController *)oldController newController:(UIViewController *)newController
{
    /**
     *            着重介绍一下它
     *  transitionFromViewController:toViewController:duration:options:animations:completion:
     *  fromViewController      当前显示在父视图控制器中的子视图控制器
     *  toViewController        将要显示的姿势图控制器
     *  duration                动画时间(这个属性,old friend 了 O(∩_∩)O)
     *  options                 动画效果(渐变,从下往上等等,具体查看API)
     *  animations              转换过程中得动画
     *  completion              转换完成
     */
 
    [self addChildViewController:newController];
    [self transitionFromViewController:oldController toViewController:newController duration:0 options:UIViewAnimationOptionTransitionCrossDissolve animations:nil completion:^(BOOL finished) {
 
        if (finished) {
 
            [newController didMoveToParentViewController:self];
            [oldController willMoveToParentViewController:nil];
            [oldController removeFromParentViewController];
            self.tempVc = newController;
 
        }else{
 
            self.tempVc = oldController;
 
        }
    }];
}

Add a Comment

电子邮件地址不会被公开。 必填项已用*标注