首页 > 其他 > 详细

进击的UI-----------------自定义视图&检测屏幕旋转&处理内存警告

时间:2015-11-21 18:21:51      阅读:330      评论:0      收藏:0      [点我收藏+]
 
1.自定义视图
流程:
1?? 新建一个类 继承于UIView
2?? 将控件写成view的属性
3?? .m初始化
4?? 直接在类外引入头文件 调用
5?? 注意: 建议不要把控件的初始化写在系统的初始化方法里直接用self调方法 自己封装方法
2.视图控制器指定自定义View
新建ViewController就是MVC(Mode(模型) View(视图) Controller(模型和视图两者的联系))设计模式的体现
3.检测屏幕旋转
1??:ViewController.m
- (NSUInteger)supportedInterfaceOrientations
{
    // portrait 竖着
    // portraitupsidedown上下颠倒竖着
    // landscape left 横着 左
    // landscape right 横着 右
    return  UIInterfaceOrientationMaskAll;
}
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{
    NSLog(@"将要旋转");
}
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation{
    NSLog(@"已经完成旋转");
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}
2??:View.m
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self p_setupView];
    }
    return self;
}
- (void)p_setupView{
    self.backgroundColor = [UIColor blackColor];
    self.nameLabel =  [[UILabel alloc]initWithFrame:CGRectMake(10, 100, 200, 300)];
    //  [[UIScreen mainScreen]bounds].size.width
    _nameLabel.backgroundColor = [UIColor yellowColor]; 
    [self addSubview:_nameLabel];}
// 当bounds改变时,需要重写layoutSubview的方法
- (void)layoutSubviews{
    if ([UIApplication sharedApplication].statusBarOrientation ==UIInterfaceOrientationMaskPortrait || [UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationPortraitUpsideDown) {
        _nameLabel.frame = CGRectMake(10, 100, 355, 200);
    }else{
        _nameLabel.frame = CGRectMake(50, 50, 300, 100);
    }
}
4.处理内存警告
ViewController.m
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    //处理内存警告的方法
    if ([self isViewLoaded] == YES && self.view.window == nil) {
        self.view = nil;
    }
}
5.容器视图控制器
ViewController.m
#import "RootViewController.h"
#import "RedViewController.h"
#import "BlueViewController.h"
@interface RootViewController ()
@property (nonatomic,retain)RedViewController *RedVC;
@property (nonatomic,retain)BlueViewController *BlueVC;
@end
@implementation RootViewController
- (void)viewWillAppear:(BOOL)animated{
    NSLog(@"将要出现");
}
- (void)viewDidAppear:(BOOL)animated{
    NSLog(@"已经出现");
}
- (void)viewWillDisappear:(BOOL)animated{
    NSLog(@"将要消失");
}
- (void)viewDidDisappear:(BOOL)animated{
    NSLog(@"已经消失");
}
- (void)viewDidLoad {
    [super viewDidLoad];
    NSLog(@"已经加载了");
    _BlueVC = [[BlueViewController alloc]init];
    _RedVC = [[RedViewController alloc]init];
    [self addChildViewController:_BlueVC];
    [self addChildViewController:_RedVC];
    [self.view addSubview:_RedVC.view];
    [self.view addSubview:_BlueVC.view];
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    button.frame = CGRectMake(100, 100, 100, 100);
    button.backgroundColor = [UIColor purpleColor];
    [button addTarget:self action:@selector(buttonAction) forControlEvents:UIControlEventTouchUpInside];
    [self.BlueVC.view addSubview:button];
}
- (void)buttonAction{
    [self transitionFromViewController:self.BlueVC toViewController:self.RedVC duration:20 options:UIViewAnimationOptionTransitionFlipFromBottom animations:^{
       
    } completion:^(BOOL finished) {
       
    }];}
重点:
技术分享
1??:AppDelegate.m
#import "AppDelegate.h"
#import "ZCHViewController.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    ZCHViewController *zchVC = [[ZCHViewController alloc]init];
    self.window.rootViewController = zchVC;
    return YES;
}
2??:ZCHViewController.m
#import "ZCHViewController.h"
#import "MyView.h"
@interface ZCHViewController ()
@property (nonatomic,retain)MyView *mv;
@end
@implementation ZCHViewController
// 必须把Controller 自带的view给替换掉
// 自己写的view,需要在Controller.m里区重写加载视图的方法(loadView)
// 加载视图
- (void)loadView{
    self.mv = [[MyView alloc]initWithFrame:[[UIScreen alloc]bounds]];
    self.view = _mv;
                
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor yellowColor];
}
3??:MyView.h
#import <UIKit/UIKit.h>
@interface MyView : UIView
@property (nonatomic,retain)UILabel *nameLabel;
@end
4??:MyView.m
#import "MyView.h"
@implementation MyView
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self z_setupView];
    }
    return self;
}
- (void)z_setupView{
     _nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(100, 100, 100, 40)];
        _nameLabel.backgroundColor = [UIColor grayColor];
    [self addSubview:_nameLabel];
}
@end

进击的UI-----------------自定义视图&检测屏幕旋转&处理内存警告

原文:http://www.cnblogs.com/sharkHZ/p/4984137.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!