【iOS】UI学习——登陆界面案例、照片墙案例

文章目录

登陆界面案例

这里通过一个登陆界面来复习一下前面学习的内容。

先在接口部分定义两个UILabel、两个UITextField、两个UIButton按键

objectivec 复制代码
#import <UIKit/UIKit.h>

@interface ViewController : UIViewController
{
    UILabel* _lbUser;
    UILabel* _lbPassword;
    UITextField* _textUser;
    UITextField* _textPassword;
    UIButton* _btn1;
    UIButton* _btn2;
}
@property (retain, nonatomic) UILabel* lbUser;
@property (retain, nonatomic) UILabel* lbPassword;
@property (retain, nonatomic) UITextField* textUser;
@property (retain, nonatomic) UITextField* textPassword;
@property (retain, nonatomic) UIButton* btn1;
@property (retain, nonatomic) UIButton* btn2;

@end

实现部分

objectivec 复制代码
#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController
@synthesize lbUser = _lbUser;
@synthesize lbPassword = _lbPassword;
@synthesize textUser = _textUser;
@synthesize textPassword = _textPassword;
@synthesize btn1 = _btn1;
@synthesize btn2 = _btn2;


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    //设置两个UILabel
    self.lbUser = [[UILabel alloc] initWithFrame:CGRectMake(50, 160, 100, 40)];
    self.lbUser.text = @"用户名:";
    self.lbUser.font = [UIFont systemFontOfSize:24];
    self.lbPassword = [[UILabel alloc] initWithFrame:CGRectMake(50, 230, 100, 40)];
    self.lbPassword.text = @"密码:";
    self.lbPassword.font = [UIFont systemFontOfSize:24];
    [self.view addSubview:self.lbUser];
    [self.view addSubview:self.lbPassword];
    //设置两个输入框
    self.textUser = [[UITextField alloc] initWithFrame:CGRectMake(150, 160, 200, 40)];
    self.textUser.borderStyle = UITextBorderStyleRoundedRect;
    self.textUser.keyboardType = UIKeyboardTypeDefault;
    self.textUser.font = [UIFont systemFontOfSize:24];
    self.textUser.text = @"";
    [self.view addSubview:self.textUser];
    _textPassword = [[UITextField alloc] initWithFrame:CGRectMake(150, 230, 200, 40)];
    self.textPassword.borderStyle = UITextBorderStyleRoundedRect;
    self.textPassword.keyboardType = UIKeyboardTypeDefault;
    self.textPassword.font = [UIFont systemFontOfSize:24];
    self.textPassword.text = @"";
    [self.view addSubview:self.textPassword];
    //设置两个按钮
    self.btn1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    self.btn1.frame = CGRectMake(150, 350, 100, 40);
    [self.btn1 setTitle:@"登陆" forState:UIControlStateNormal];
    self.btn1.titleLabel.font = [UIFont systemFontOfSize:32];
    [self.btn1 addTarget:self action:@selector(pressLogin) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.btn1];
    self.btn2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    self.btn2.frame = CGRectMake(150, 450, 100, 40);
    [self.btn2 setTitle:@"注册" forState:UIControlStateNormal];
    self.btn2.titleLabel.font = [UIFont systemFontOfSize:32];
    [self.btn2 addTarget:self action:@selector(pressRegister) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.btn2];
}

-(void) touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.textUser resignFirstResponder];
    [self.textPassword resignFirstResponder];
}

-(void) pressLogin
{
    NSString* Username = @"Reus";
    NSString* Password = @"123098";
    NSString* strUser = self.textUser.text;
    NSString* strPass = self.textPassword.text;
    if([Username isEqual:strUser] && [Password isEqual:strPass]) {
        NSLog(@"对对对");
        UIAlertController* ele = [UIAlertController alertControllerWithTitle:@"提示" message:@"您的账号和密码输入正确,即将进入主页面" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction* act = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){
            //设置密码输入正确将要进入的界面
            UIView* view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10000, 10000)];
            view.backgroundColor = [UIColor grayColor];
            UILabel* lb = [[UILabel alloc] initWithFrame:CGRectMake(50, 50, 300, 100)];
            lb.text = @"哈哈哈哈哈哈哈哈";
            lb.textColor = [UIColor redColor];
            lb.font = [UIFont systemFontOfSize:34];
            [view addSubview:lb];
            [self.view addSubview:view];
        }];
        [ele addAction:act];
        [self presentViewController:ele animated:YES completion:nil];
    } else {
        UIAlertController* ele = [UIAlertController alertControllerWithTitle:@"提示" message:@"您的账号或密码输入错误,请重新输入" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction* act = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action){
            self.textUser.text = @"";
            self.textPassword.text = @"";
        }];
        [ele addAction:act];
        [self presentViewController:ele animated:YES completion:nil];
    }
}

-(void) pressRegister
{
    NSLog(@"好好好");
}


@end

效果图

照片墙案例

UITapGestureRecognizer:iOS 中一种手势识别器,用于检测用户在屏幕上的单击或双击操作。

在 Objective-C 中,- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated; 是 UINavigationController 类的一个实例方法,用于将一个新的视图控制器添加到导航栈中,并将其推送到当前视图控制器上。

该方法的主要作用如下:

添加新的视图控制器到导航栈中

该方法会将传入的 viewController 参数添加到导航栈的顶部,成为当前显示的视图控制器。

切换到新的视图控制器

调用该方法后,导航控制器会切换到新推送的视图控制器,用户将看到新的视图控制器的内容。

动画效果

animated 参数决定了视图切换时是否使用动画效果。如果设置为 YES,则视图切换时会有一个平滑的动画效果;如果设置为 NO,则视图会立即切换到新的视图控制器。

导航栈管理

每次调用该方法后,新的视图控制器都会被添加到导航栈的顶部。用户可以通过导航栏上的返回按钮,返回到之前的视图控制器。

SceneDelegate.m:

objectivec 复制代码
#import "SceneDelegate.h"
#import "VCRoot.h"
@interface SceneDelegate ()

@end

@implementation SceneDelegate


- (void)scene:(UIScene *)scene willConnectToSession:(UISceneSession *)session options:(UISceneConnectionOptions *)connectionOptions {
    //导航控制器框架结构
    UINavigationController* nav = [[UINavigationController alloc] initWithRootViewController:[[VCRoot alloc] init]];
    
    self.window.rootViewController = nav;
    [self.window makeKeyAndVisible];
}


- (void)sceneDidDisconnect:(UIScene *)scene {
    // Called as the scene is being released by the system.
    // This occurs shortly after the scene enters the background, or when its session is discarded.
    // Release any resources associated with this scene that can be re-created the next time the scene connects.
    // The scene may re-connect later, as its session was not necessarily discarded (see `application:didDiscardSceneSessions` instead).
}


- (void)sceneDidBecomeActive:(UIScene *)scene {
    // Called when the scene has moved from an inactive state to an active state.
    // Use this method to restart any tasks that were paused (or not yet started) when the scene was inactive.
}


- (void)sceneWillResignActive:(UIScene *)scene {
    // Called when the scene will move from an active state to an inactive state.
    // This may occur due to temporary interruptions (ex. an incoming phone call).
}


- (void)sceneWillEnterForeground:(UIScene *)scene {
    // Called as the scene transitions from the background to the foreground.
    // Use this method to undo the changes made on entering the background.
}


- (void)sceneDidEnterBackground:(UIScene *)scene {
    // Called as the scene transitions from the foreground to the background.
    // Use this method to save data, release shared resources, and store enough scene-specific state information
    // to restore the scene back to its current state.
}


@end

VCRoot.m:

objectivec 复制代码
#import "VCRoot.h"
#import "VCImageShow.h"
@interface VCRoot ()

@end

@implementation VCRoot

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.title = @"照片墙";
    
    self.navigationController.navigationBar.translucent = YES;
    self.view.backgroundColor = [UIColor whiteColor];
    UIScrollView* sv = [[UIScrollView alloc] init];
    
    sv.frame = self.view.bounds;//CGRectMake(5, 10, 394, 852);
    
    sv.contentSize = CGSizeMake(self.view.bounds.size.width, self.view.bounds.size.height);
    //  打开交互事件,关闭会导致无法使用点击等手势操作成功运行
    sv.userInteractionEnabled = YES;
    
    for(int i = 0; i < 10; i++) {
        NSString* strName = [NSString stringWithFormat:@"%d.JPG",i+1];
        UIImage* image = [UIImage imageNamed:strName];
        UIImageView* iview = [[UIImageView alloc] initWithImage:image];
        iview.frame = CGRectMake(10 + (i % 3) * self.view.bounds.size.width / 3, (i / 3) * self.view.bounds.size.height / 4, 110, 200);
        [sv addSubview:iview];
        iview.userInteractionEnabled = YES;
        iview.tag = 101 + i;
        UITapGestureRecognizer* tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(press:)];
        //表示我们需要检测单次点击事件
        tap.numberOfTapsRequired = 2;
        //表示我们需要检测单指点击事件
        tap.numberOfTouchesRequired = 1;
        //将手势识别器添加到视图上去
        [iview addGestureRecognizer:tap];
        
    }
    [self.view addSubview:sv];
}

-(void) press:(UITapGestureRecognizer*) tap {
    UIImageView* imageView = (UIImageView*) tap.view;
    //创建显示视图控制器
    VCImageShow* imageShow = [[VCImageShow alloc] init];
    imageShow.imageTag = imageView.tag;
    [self.navigationController pushViewController:imageShow animated:YES];
}

/*
#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

VCIamgeShow.h:

objectivec 复制代码
#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

@interface VCImageShow : UIViewController
@property (assign, nonatomic) NSUInteger imageTag;
@property (nonatomic, retain) UIImage* image;
@property (nonatomic, retain) UIImageView* imageView;
@end

VCImageShow.m:

objectivec 复制代码
#import "VCImageShow.h"
#import "VCRoot.h"
@interface VCImageShow ()

@end

@implementation VCImageShow
@synthesize imageView = _imageView;
@synthesize image = _image;
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"图片展示";
    UIImageView* _imageView = [[UIImageView alloc] init];
    _imageView.frame = self.view.bounds;
    
    _imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%lu.JPG", (unsigned long)(_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

效果图 :

相关推荐
量子-Alex几秒前
【多模态聚类】用于无标记视频自监督学习的多模态聚类网络
学习·音视频·聚类
吉大一菜鸡5 分钟前
FPGA学习(基于小梅哥Xilinx FPGA)学习笔记
笔记·学习·fpga开发
爱吃西瓜的小菜鸡3 小时前
【C语言】判断回文
c语言·学习·算法
小A1593 小时前
STM32完全学习——SPI接口的FLASH(DMA模式)
stm32·嵌入式硬件·学习
岁岁岁平安3 小时前
spring学习(spring-DI(字符串或对象引用注入、集合注入)(XML配置))
java·学习·spring·依赖注入·集合注入·基本数据类型注入·引用数据类型注入
武昌库里写JAVA4 小时前
Java成长之路(一)--SpringBoot基础学习--SpringBoot代码测试
java·开发语言·spring boot·学习·课程设计
qq_589568104 小时前
数据可视化echarts学习笔记
学习·信息可视化·echarts
兔C4 小时前
微信小程序的轮播图学习报告
学习·微信小程序·小程序
海海不掉头发5 小时前
苍穹外卖-day05redis 缓存的学习
学习·缓存
小木_.6 小时前
【Python 图片下载器】一款专门为爬虫制作的图片下载器,多线程下载,速度快,支持续传/图片缩放/图片压缩/图片转换
爬虫·python·学习·分享·批量下载·图片下载器