RootViewController
//视图控制器(UIViewController):它不是视图,用来管理视图,所以屏幕上看不到,但是自身携带一个视图(根视图) #import "RootViewController.h" #import "LoginView.h" //视图控制器的延展 @interface RootViewController () @end //视图控制器的实现部分 @implementation RootViewController //用来加载视图控制器的根视图,此方法源于父类 - (void)loadView { LoginView *logview = [[LoginView alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; //指定刚才创建的 自定义视图 对象为当前视图控制器的根视图 self.view = logview; [logview release]; } //该方法是在loadView执行完之后立即执行的方法 //当访问视图控制器的View为空,一但发现View为空,立即调用loadView来加载根视图 //如果要自定义视图控制器的根视图,只需要重写loadView方法,在loadView方法中完成创建,并指定当前试图控制器的根视图即可 - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. NSLog(@"%s,%d",__FUNCTION__,__LINE__); // self.view.backgroundColor = [UIColor redColor]; } #pragma mark -- 检测和处理屏幕的旋转 //1.设置屏幕支持的旋转方向 - (NSUInteger)supportedInterfaceOrientations{ return UIInterfaceOrientationMaskAll; } //开始旋转时会触发的方法 //经常用来暂停播放器播放,暂停视屏播放,以及关闭用户交互 - (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{ [super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration]; } //当旋转结束是时触发 //继续音乐,视频播放,以及打开的用户交互 - (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation{ [super didRotateFromInterfaceOrientation:fromInterfaceOrientation]; } //当将要开始旋转做动画时触发,经常用来在旋转时添加自定义动画 - (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{ [super willAnimateRotationToInterfaceOrientation:toInterfaceOrientation duration:duration]; } #pragma ---视图控制器控制布局视图的方法 //当视图旋转时,视图控制器触发的方法,用于重新布局视图控制器根据视图上得子视图 - (void)viewWillLayoutSubviews{ [super viewWillLayoutSubviews]; } //当视图控制器收到内存警告时触发 //释放之前未使用的空间,以及可以重建的对象. - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. //1.视图控制器的根视图是够已经成功加载 //2.当前根视图是否正在显示 //[self isViewLoaded]判断视图是否成功加载 //self.view.window,判读视图是否在当前界面 //内存加载成功,且不在当前界面 if ([self isViewLoaded] && !self.view.window) { [self.view release]; } } @end
LoginView.h
#import <UIKit/UIKit.h> @interface LoginView : UIView //最简单的自定义视图封装:直接将控件声明为属性,在.h中声明外界访问的接口. @property (nonatomic,retain) UILabel *aLabel; @property (nonatomic, retain)UITextField *textField; @property (nonatomic, retain)UIButton *loginBtn; @end
LoginView.m
#import "LoginView.h" #define kMargin_Left_Label 30 #define kMargin_Top_Label 100 #define kWidth_Label 60 #define kHeight_Label 30 #define kMargin_Left_TextField (kMargin_Left_Label + kWidth_Label + 20) #define kMargin_Top_TextField 100 #define kWidth_TextField 150 #define kHeight_TextField 30 #define KMargin_Left_Button 100 #define kmargin_Top_Button (kMargin_Top_Label + kHeight_Label + 30) #define kWidth_Button 80 #define kHeight_Button 40 @implementation LoginView //原则:如果重写父类继承来方法是,如果不知道父类对该方法的事项,则调用父类对该方法的实现(super). - (instancetype)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if(self) { [self setupLabel]; [self setupButton]; [self setupTextField]; } return self; } //用户名Label - (void)setupLabel { self.aLabel = [[UILabel alloc] init]; self.aLabel.text = @"用户名"; self.aLabel.frame = CGRectMake(kMargin_Left_Label,kMargin_Top_Label , kWidth_Label, kHeight_Label); [self addSubview:self.aLabel]; [self.aLabel release]; } //输入框 - (void)setupTextField{ self.textField = [[UITextField alloc] init]; self.textField.placeholder = @"请输入用户名"; self.textField.borderStyle = UITextBorderStyleRoundedRect; self.textField.frame = CGRectMake(kMargin_Left_TextField, kMargin_Top_TextField, kWidth_TextField, kHeight_TextField); [self addSubview:self.textField]; [self.textField release]; } //登陆按钮 - (void)setupButton { self.loginBtn = [UIButton buttonWithType:UIButtonTypeSystem]; [self.loginBtn setTitle:@"登陆" forState:UIControlStateNormal]; self.loginBtn.frame = CGRectMake(KMargin_Left_Button, kmargin_Top_Button, kWidth_Button, kHeight_Button); [self addSubview:self.loginBtn]; } //该方法为视图的方法,当旋转时,视图会自动的调用该方法用来重新布局子视图. - (void)layoutSubviews{ [super layoutSubviews]; //1.获取当前屏幕所处的状态(旋转方向) UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation; //2.判断屏幕方向,调整子视图 switch (orientation) { case UIInterfaceOrientationPortrait: // NSLog(@"竖屏"); //break; case UIInterfaceOrientationPortraitUpsideDown: // NSLog(@"倒置"); self.loginBtn.frame = CGRectMake(KMargin_Left_Button, kmargin_Top_Button, kWidth_Button, kHeight_Button); break; case UIInterfaceOrientationLandscapeLeft: // NSLog(@"右横屏"); //break; case UIInterfaceOrientationLandscapeRight: // NSLog(@"左横屏"); self.loginBtn.frame = CGRectMake(kMargin_Left_TextField + kWidth_TextField + 30, kMargin_Top_Label, kWidth_Button, kHeight_Button); break; default: break; } } - (void)dealloc { [_aLabel release]; [_textField release]; [_loginBtn release]; [super dealloc]; } @end
时间: 2024-11-25 05:17:52