1 #import "AppDelegate.h" 2 #import "ViewController.h" 3 4 @interface AppDelegate () 5 6 @end 7 8 @implementation AppDelegate 9 10 //程序启动完成调用 11 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 12 //系统创建window 13 self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 14 //在window上创建视图控制器 15 self.window.rootViewController = [[ViewController alloc] init]; 16 //系统设置创建window的背景颜色 17 self.window.backgroundColor = [UIColor blackColor]; 18 //设置为主窗口并显示出来 19 [self.window makeKeyAndVisible]; 20 21 return YES; 22 } 23 24 25 26 #ifndef GlobalDefine_h 27 #define GlobalDefine_h 28 29 #define SCREENWIDTH [[UIScreen mainScreen] bounds].size.width 30 #define SCREENHEIGHT [[UIScreen mainScreen] bounds].size.height 31 32 #endif /* GlobalDefine_h */ 33 34 35 36 #import <UIKit/UIKit.h> 37 38 @interface ViewController : UIViewController <UITextFieldDelegate> 39 40 @property (nonatomic, strong) UIImageView *imgViewHomePage; 41 @property (nonatomic, strong) UIImageView *imgViewLine1; 42 @property (nonatomic, strong) UIImageView *imgViewLine2; 43 @property (nonatomic, strong) UIImageView *imgViewLogin; 44 @property (nonatomic, strong) UIImageView *imgViewCancel; 45 46 @property (nonatomic, strong) UITextField *textFieldUserName; 47 @property (nonatomic, strong) UITextField *textFieldPassword; 48 49 @property (nonatomic, strong) UIButton *buttonLogin; 50 @property (nonatomic, strong) UIButton *buttonEmpty; 51 52 @end 53 54 55 56 #import "ViewController.h" 57 #import "SecondViewController.h" 58 59 @interface ViewController () 60 61 @end 62 63 @implementation ViewController 64 65 - (void)viewDidLoad { 66 [super viewDidLoad]; 67 //设置主页面 68 _imgViewHomePage = [[UIImageView alloc] initWithFrame:CGRectMake(89, 92, 143, 48)]; 69 _imgViewHomePage.image = [UIImage imageNamed:@"img_首页Logo"]; 70 [self.view addSubview:_imgViewHomePage]; 71 72 //设置用户名线 73 _imgViewLine1 = [[UIImageView alloc] initWithFrame:CGRectMake(45, 262, 230, 1)]; 74 _imgViewLine1.image = [UIImage imageNamed:@"img_首页输入框底线"]; 75 [self.view addSubview:_imgViewLine1]; 76 77 //设置密码线 78 _imgViewLine2 = [[UIImageView alloc] initWithFrame:CGRectMake(45, 311, 230, 1)]; 79 _imgViewLine2.image = [UIImage imageNamed:@"img_首页输入框底线"]; 80 [self.view addSubview:_imgViewLine2]; 81 82 //设置登录imageView 83 _imgViewLogin = [[UIImageView alloc] initWithFrame:CGRectMake(45, 323, 230, 47)]; 84 _imgViewLogin.image = [UIImage imageNamed:@"btn_登录_n"]; 85 [self.view addSubview:_imgViewLogin]; 86 87 //设置取消imageView 88 _imgViewCancel = [[UIImageView alloc] initWithFrame:CGRectMake(252, 240, 22, 22)]; 89 _imgViewCancel.image = [UIImage imageNamed:@"btn_首页用户名取消"]; 90 [self.view addSubview:_imgViewCancel]; 91 92 //设置用户名TextField 93 _textFieldUserName = [[UITextField alloc] initWithFrame:CGRectMake(45, 232, 230, 30)]; 94 //占位符 95 // _textFieldUserName.placeholder = @"用户名"; 96 //设置占位符上字体的颜色!!! 97 _textFieldUserName.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"用户名" attributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor grayColor], NSForegroundColorAttributeName,nil]];//设置占位符上字体的颜色 98 _textFieldUserName.textColor = [UIColor whiteColor]; 99 _textFieldUserName.delegate = self;//调用协议 100 [self.view addSubview:_textFieldUserName]; 101 102 //设置密码TextField 103 _textFieldPassword = [[UITextField alloc] initWithFrame:CGRectMake(45, 281, 230, 30)]; 104 NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIColor grayColor], NSForegroundColorAttributeName,nil]; 105 _textFieldPassword.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"密码" attributes:dic]; 106 _textFieldPassword.textColor = [UIColor whiteColor]; 107 //使用密文 108 _textFieldPassword.secureTextEntry = YES; 109 _textFieldPassword.delegate = self; 110 [self.view addSubview:_textFieldPassword]; 111 112 //设置登录按钮 113 _buttonLogin = [UIButton buttonWithType:UIButtonTypeCustom]; 114 _buttonLogin.frame = CGRectMake(45, 323, 230, 47); 115 [self.view addSubview:_buttonLogin]; 116 //登录跳转 117 [_buttonLogin addTarget:self action:@selector(next:) forControlEvents:UIControlEventTouchUpInside]; 118 119 //设置清空按钮 120 _buttonEmpty = [UIButton buttonWithType:UIButtonTypeCustom]; 121 _buttonEmpty.frame = CGRectMake(252, 240, 22, 22); 122 [self.view addSubview:_buttonEmpty]; 123 //清空用户名和密码:跳转 124 [_buttonEmpty addTarget:self action:@selector(empty:) forControlEvents:UIControlEventTouchUpInside]; 125 126 } 127 128 //清空用户名的方法 129 -(void)empty:(id)sender 130 { 131 [_textFieldUserName setText:@""]; 132 [_textFieldPassword setText:@""]; 133 [_textFieldUserName isFirstResponder]; 134 } 135 136 //登录跳转方法 137 - (void)next:(id)sender 138 { 139 NSString *strUserName = @"Huashan"; 140 NSString *strPassWord = @"123456"; 141 if ([_textFieldUserName.text isEqualToString:strUserName] && [_textFieldPassword.text isEqualToString:strPassWord]) 142 { 143 //跳到下一个界面 144 SecondViewController *tempVC = [[SecondViewController alloc] init]; 145 [self presentViewController:tempVC animated:YES completion:nil]; 146 } 147 else 148 { 149 //跳出输入错误提示框 150 UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"警告" message:@"用户名或密码错误,请重新输入" preferredStyle:UIAlertControllerStyleAlert]; 151 UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil]; 152 [alertController addAction:okAction]; 153 [self presentViewController:alertController animated:YES completion:nil]; 154 } 155 } 156 157 - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event 158 { 159 [_textFieldUserName resignFirstResponder]; 160 [_textFieldPassword resignFirstResponder]; 161 } 162 163 //点击文本框,界面随键盘出现,整体往上移动(关键词:UIView animate) 164 - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField 165 { 166 if (self.view.frame.origin.y == 0) 167 { 168 [UIView animateWithDuration:0.3 animations:^{ 169 self.view.frame = CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y - 100, self.view.frame.size.width, self.view.frame.size.height); 170 }]; 171 } 172 return YES; 173 } 174 //随着键盘收缩,界面随着往下移动 175 - (BOOL)textFieldShouldEndEditing:(UITextField *)textField 176 { 177 178 if (self.view.frame.origin.y == -100) 179 { 180 [UIView animateWithDuration:0.3 animations:^{ 181 self.view.frame = CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y + 100, self.view.frame.size.width, self.view.frame.size.height); 182 }]; 183 } 184 return YES; 185 } 186 187 188 - (BOOL)textFieldShouldReturn:(UITextField *)textField//按下return键盘收缩 189 { 190 [textField resignFirstResponder]; 191 return YES; 192 } 193 194 - (void)didReceiveMemoryWarning { 195 [super didReceiveMemoryWarning]; 196 // Dispose of any resources that can be recreated. 197 } 198 199 @end 200 201 202 203 #import <UIKit/UIKit.h> 204 205 @interface SecondViewController : UIViewController 206 207 @property (nonatomic, strong) UIScrollView *scrollView; 208 @property (nonatomic, strong) UIImageView *imgView; 209 @property (nonatomic, strong) UILabel *sortLabel; 210 @property (nonatomic, strong) UIButton *backButton; 211 212 @end 213 214 215 216 #import "SecondViewController.h" 217 #import "GlobalDefine.h" 218 219 @interface SecondViewController () 220 221 @end 222 223 @implementation SecondViewController 224 225 - (void)viewDidLoad 226 { 227 [super viewDidLoad]; 228 //设置滚动视图 229 _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, SCREENWIDTH, SCREENHEIGHT)]; 230 //滚动视图内容的宽高 231 _scrollView.contentSize = CGSizeMake(SCREENWIDTH, SCREENHEIGHT * 4); 232 //启用分页 233 _scrollView.pagingEnabled = YES; 234 //禁止弹动 235 _scrollView.bounces = NO; 236 [self.view addSubview:_scrollView]; 237 238 //添加4张图片,顺序上下 239 for (int i = 1; i < 5; ++i) 240 { 241 _sortLabel = [[UILabel alloc] initWithFrame:CGRectMake(SCREENWIDTH / 2 - 100, SCREENHEIGHT - 50, 200, 30)]; 242 _sortLabel.textAlignment = NSTextAlignmentCenter; 243 _sortLabel.textColor = [UIColor whiteColor]; 244 _sortLabel.font = [UIFont systemFontOfSize:15]; 245 246 _imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, SCREENHEIGHT * (i - 1), SCREENWIDTH, SCREENHEIGHT)]; 247 _imgView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%d.jpg", i]]; 248 _sortLabel.text = [NSString stringWithFormat:@"这是第%d张图片", i]; 249 [_imgView addSubview:_sortLabel]; 250 [_scrollView addSubview:_imgView]; 251 } 252 253 //退出(返回) 254 _backButton = [UIButton buttonWithType:UIButtonTypeCustom]; 255 _backButton.frame = CGRectMake(30, 50, 60, 60); 256 [_backButton setTitle:@"退出" forState:UIControlStateNormal]; 257 [_backButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; 258 // _backButton.tintColor = [UIColor whiteColor]; 259 _backButton.titleLabel.font = [UIFont systemFontOfSize:14]; 260 [self.view addSubview:_backButton]; 261 262 //退出跳转 263 [_backButton addTarget:self action:@selector(back:) forControlEvents:UIControlEventTouchUpInside]; 264 265 } 266 267 - (void)back:(id)sender 268 { 269 [self dismissViewControllerAnimated:YES completion:nil]; 270 } 271 272 - (void)didReceiveMemoryWarning { 273 [super didReceiveMemoryWarning]; 274 // Dispose of any resources that can be recreated. 275 } 276 277 /* 278 #pragma mark - Navigation 279 280 // In a storyboard-based application, you will often want to do a little preparation before navigation 281 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { 282 // Get the new view controller using [segue destinationViewController]. 283 // Pass the selected object to the new view controller. 284 } 285 */ 286 287 @end
时间: 2024-09-29 11:23:55