您的位置:首页 > 产品设计 > UI/UE

移动开发iOS之如何在UI界面实现视图的交替

2015-08-23 12:53 447 查看
首先,在IOS中建立一个Single View Application;

在创建一个类Cocoa Touch Class,继承于ViewController类;

在ViewController写:

#import “ViewController.h”

#import “NextViewController.h”

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {

[super viewDidLoad];

self.view.backgroundColor = [UIColor blackColor];

//模态视图推送

UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];

button.translatesAutoresizingMaskIntoConstraints = NO;

[button setTitle:@”推送” forState:0];

[button addTarget:self action:@selector(pushAction:) forControlEvents:UIControlEventTouchUpInside];

[self.view addSubview:button];

//给按钮添加约束来布局,即相当于frame的功能:

NSLayoutConstraint *constraint = [NSLayoutConstraint constraintWithItem:button attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:1 constant:0];

[self.view addConstraint:constraint];

constraint = [NSLayoutConstraint constraintWithItem:button attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterY multiplier:1 constant:0];
[self.view addConstraint:constraint];

constraint = [NSLayoutConstraint constraintWithItem:button attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:60];
[self.view addConstraint:constraint];

constraint = [NSLayoutConstraint constraintWithItem:button attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:30];
[self.view addConstraint:constraint];


-(void)pushAction:(UIButton *)sender{

NextViewController *nextViewController = [[NextViewController alloc] init];


//模态视图过度动画

nextViewController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;

[self presentViewController:nextViewController animated:YES completion:^{

}];

}

- (void)didReceiveMemoryWarning {

[super didReceiveMemoryWarning];

}

@end

在新建的类中这么写:

#import “ViewController.h”

@interface NextViewController : ViewController

@end

#import “NextViewController.h”

@interface NextViewController ()

@end

@implementation NextViewController

- (void)viewDidLoad {

self.view.backgroundColor = [UIColor redColor];

UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
button.translatesAutoresizingMaskIntoConstraints = NO;
[button setTitle:@"返回" forState:0];
[button addTarget:self action:@selector(popAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];

NSLayoutConstraint *constraint = [NSLayoutConstraint constraintWithItem:button attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:1 constant:0];
[self.view addConstraint:constraint];

constraint = [NSLayoutConstraint constraintWithItem:button attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterY multiplier:1 constant:0];
[self.view addConstraint:constraint];

constraint = [NSLayoutConstraint constraintWithItem:button attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:60];
[self.view addConstraint:constraint];
constraint = [NSLayoutConstraint constraintWithItem:button attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:30];
[self.view addConstraint:constraint];


}

-(void)popAction:(UIButton *)sender{

[self dismissViewControllerAnimated:YES completion:nil];

}

- (void)didReceiveMemoryWarning {

[super didReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}

@end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: