目录
登陆界面案例
这里实现一个登陆界面的案例,只实现最基础的界面。
用户名和密码登录需要UILabel和文本输入框
登陆和注册通过两个按钮和警告对话框实现
接口部分:
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
{
//登陆界面的组成
//用户名提示:输入框
//密码提示:密码输入框
//登陆按钮
//注册按钮
UILabel* _lbUserName;
UILabel* _lbPassword;
UITextField* _tfUserName;
UITextField* _tfPassword;
UIButton* _btnLogin;
UIButton* _btnRegister;
}
@end
实现部分:
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//用户名提示标签
_lbUserName = [[UILabel alloc] init];
_lbUserName.frame = CGRectMake(100, 100, 200, 40);
_lbUserName.text = @"用户名";
_lbUserName.font = [UIFont systemFontOfSize:20];
_lbUserName.textColor = [UIColor orangeColor];
_lbUserName.textAlignment = NSTextAlignmentCenter;
_lbUserName.backgroundColor = [UIColor cyanColor];
[self.view addSubview:_lbUserName];
//密码提示标签
_lbPassword = [[UILabel alloc] init];
_lbPassword.frame = CGRectMake(100, 225, 200, 40);
_lbPassword.text = @"密码";
_lbPassword.font = [UIFont systemFontOfSize:20];
_lbPassword.textColor = [UIColor orangeColor];
_lbPassword.textAlignment = NSTextAlignmentCenter;
_lbPassword.backgroundColor = [UIColor cyanColor];
[self.view addSubview:_lbPassword];
//用户名输入框
_tfUserName = [[UITextField alloc] init];
_tfUserName.frame = CGRectMake(100, 150, 200, 40);
_tfUserName.placeholder = @"请在此处输入用户名";
_tfUserName.backgroundColor = [UIColor cyanColor];
_tfUserName.textColor = [UIColor orangeColor];
_tfUserName.borderStyle = UITextBorderStyleRoundedRect;
[self.view addSubview:_tfUserName];
//密码输入框
_tfPassword = [[UITextField alloc] init];
_tfPassword.frame = CGRectMake(100, 275, 200, 40);
_tfPassword.placeholder = @"请在此处输入密码";
_tfPassword.backgroundColor = [UIColor cyanColor];
_tfPassword.textColor = [UIColor orangeColor];
_tfPassword.borderStyle = UITextBorderStyleRoundedRect;
_tfPassword.secureTextEntry = YES;
[self.view addSubview:_tfPassword];
//登陆按钮
_btnLogin = [UIButton buttonWithType:UIButtonTypeRoundedRect];
_btnLogin.frame = CGRectMake(150, 550, 70, 70);
[_btnLogin setTitle:@"登陆" forState:UIControlStateNormal];
_btnLogin.titleLabel.font = [UIFont systemFontOfSize:24];
[_btnLogin addTarget:self action:@selector(pressLogin) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:_btnLogin];
//注册按钮
_btnRegister = [UIButton buttonWithType:UIButtonTypeRoundedRect];
_btnRegister.frame = CGRectMake(150, 600, 70, 70);
[_btnRegister setTitle:@"注册" forState:UIControlStateNormal];
_btnRegister.titleLabel.font = [UIFont systemFontOfSize:24];
[_btnRegister addTarget:self action:@selector(pressRegister) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:_btnRegister];
}
//登陆事件函数
- (void)pressLogin {
NSString* username = @"Hugo";
NSString* password = @"123456";
if([username isEqualToString:_tfUserName.text] && [password isEqualToString:_tfPassword.text]) {
UIAlertController* win = [UIAlertController alertControllerWithTitle:@"提示" message:@"登录成功!" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* confirm = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"已确认");
}];
[win addAction:confirm];
[self presentViewController:win animated:NO completion:nil];
} else {
UIAlertController* lose = [UIAlertController alertControllerWithTitle:@"提示" message:@"登陆失败!" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* confirm = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"已确认");
}];
[lose addAction:confirm];
[self presentViewController:lose animated:NO completion:nil];
}
}
//注册事件函数
- (void)pressRegister {
NSLog(@"注册");
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
//回收键盘对象
[_tfPassword resignFirstResponder];
[_tfUserName resignFirstResponder];
}
@end
效果图:
照片墙案例
照片墙案例需要用到UIView的userInteractionEnabled属性
这个属性设置为YES后,用户才可以与UIView进行交互
SceneDelegate.m:
#import "SceneDelegate.h"
#import "RootViewController.h"
@interface SceneDelegate ()
@end
@implementation SceneDelegate
- (void)scene:(UIScene *)scene willConnectToSession:(UISceneSession *)session options:(UISceneConnectionOptions *)connectionOptions {
// Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
// If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
// This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).
//获取当前屏幕参数
self.window.frame = [UIScreen mainScreen].bounds;
//创建导航控制器
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:[[RootViewController alloc] init]];
//将该导航控制器赋为根视图控制器
self.window.rootViewController = nav;
[self.window makeKeyAndVisible];
}
然后是根视图控制器RootViewController.m:
#import "RootViewController.h"
#import "ImageViewController.h"
@interface RootViewController ()
@end
@implementation RootViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
//设置视图标题和视图背景颜色
self.title = @"照片墙";
self.view.backgroundColor = [UIColor cyanColor];
//设置导航栏是否透明
self.navigationController.navigationBar.translucent = YES;
//创建一个滚动视图并设置位置和画布大小
UIScrollView* sv = [[UIScrollView alloc] init];
sv.frame = CGRectMake(5, 10, 394, 852);
sv.contentSize = CGSizeMake(394, 852 * 1.5);
//打开交互事件
sv.userInteractionEnabled = YES;
//使用for循环给根视图上添加图片对象
for (int i = 0; i < 9; i++) {
//创建一个代表所传图片文件名字的字符串对象
NSString* strName = [NSString stringWithFormat:@"images (%d).jpg", i + 1];
UIImage* image = [UIImage imageNamed:strName];
UIImageView* iView = [[UIImageView alloc] initWithImage:image];
iView.frame = CGRectMake(10 + (i % 3) * 125, (i / 3) * 165, 110, 150);
[sv addSubview:iView];
iView.userInteractionEnabled = YES;
UITapGestureRecognizer* tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(pressTap:)];
tap.numberOfTapsRequired = 1;
tap.numberOfTouchesRequired = 1;
[iView addGestureRecognizer:tap];
iView.tag = 101 + i;
}
[self.view addSubview:sv];
}
- (void)pressTap: (UIGestureRecognizer*)tap {
UIImageView* imageView = (UIImageView*)tap.view;
ImageViewController* imageShow = [[ImageViewController alloc] init];
imageShow.imageTag = imageView.tag;
[self.navigationController pushViewController:imageShow animated:YES];
}
//使用直接setter图片属性的方法
//- (void)pressTap: (UITapGestureRecognizer*) tap {
// //这里是用了一个强制类型转换,指将传入的tap对象转换为UIImageView类的对象然后赋值给imageView
// UIImageView *imageView = (UIImageView*)tap.view;
//
// ImageViewController *imageShow = [[ImageViewController alloc] init];
// imageShow.image = imageView.image;
// [self.navigationController pushViewController: imageShow animated: YES];
//}
需要创建一个ImageViewController
#import "ImageViewController.h"
@interface ImageViewController ()
@end
@implementation ImageViewController
@synthesize imageView = _imageView;
@synthesize image = _image;
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.title = @"图片展示";
UIImageView* _imageView = [[UIImageView alloc] init];
_imageView.frame = CGRectMake(0, 100, 394, 650);
//通过图片属性image传图片
//_imageView.image = _image;
//通过imageTag属性传图片
_imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"images (%lu).jpg", _imageTag - 100]];
[self.view addSubview:_imageView];
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end
效果如下: