移动开发iOS之如何在UI界面实现视图的交替
2015-08-23 12:53
483 查看
首先,在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];
-(void)pushAction:(UIButton *)sender{
//模态视图过度动画
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];
}
-(void)popAction:(UIButton *)sender{
[self dismissViewControllerAnimated:YES completion:nil];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
在创建一个类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
相关文章推荐
- UIButton2
- [leetcode-128]Longest Consecutive Sequence(java)
- 移动开发iOS之如何在UI相框里实现动画
- 如何在ios中的UI界面利用相框制作gif动画
- Java中的break,continue,return
- Android - 警告:it is always overridden by the value specified in the Gradle build script
- 大数据时代的 9 大Key-Value存储数据库
- Implement Queue using Stacks
- UIPickerView 多级联动
- Web.Config中的<compilation debug="true"/>
- requests模块post/get基本用法
- 终结者单身——setAccessible(true)
- 暑期留校之iOS学习笔记之视图控制器(UI)
- 暑期留校之iOS学习笔记_ UiButton
- UEditor-分布式部署解决方案
- iOS学习UI之UIButton
- UILable
- This Virtual Machine Is Configured For 64-bit Guest Operating Systems
- ListBuffer、ArrayBuffer、Queue、Stack操作代码实战之Scala学习笔记-30
- 简易相册UIScrollView