当前位置: 首页 > news >正文

【iOS】暑期学习总结

暑期学习总结

  • 前言
  • 无限轮播图
  • 换头像
  • 简单的网络请求
  • UISearchController

前言

暑假在学校完成了五个项目,总的来说学习到了很多新的知识,这里对暑假中学习的内容进行一个小的总结,整理一些个人认为比较重点的内容。

无限轮播图

无限轮播图的原理是在轮播的图片两边加上两个假页,通过判断是否到了这两个假页的位置,而后将滚动视图展示位置移动,从而可以实现视觉效果上的无限轮播效果。
在这里插入图片描述
如图所示,当我插入三张图片时,两边的假页分别是第三、一张,这样一来,在展示时当移动到第五张时,函数检测后将滚动视图重置至第二张,就不会有间断效果,前面的移动也是如此。

self.scrollview = [[UIScrollView alloc] init];self.scrollview.frame = CGRectMake(0, [UIScreen mainScreen].bounds.size.height/2-200, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height-320);self.scrollview.pagingEnabled = YES;self.scrollview.scrollEnabled = YES;self.scrollview.contentSize = CGSizeMake([UIScreen mainScreen].bounds.size.width * 5 , [UIScreen mainScreen].bounds.size.height / 2);for (int i = 0; i <= 5; i++) {NSString *strName = [NSString stringWithFormat:@"%d.JPG", (i % 3) + 1];// 创建图片UIImage *image = [UIImage imageNamed:strName];// 创建视图UIImageView *iView = [[UIImageView alloc] initWithImage:image];if (i == 5) {iView.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height - 300);}else {iView.frame = CGRectMake([UIScreen mainScreen].bounds.size.width * (i + 1), 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height - 320);}// 将图片添加到滚动视图,不要添加到主视图[self.scrollview addSubview:iView];}[self.view addSubview:self.scrollview];// 设置初始偏移量到第二张图片[self.scrollview setContentOffset:CGPointMake([UIScreen mainScreen].bounds.size.width, 0) animated:NO];// 实现滚动到最后一张视图时,再次滚动能回到第一张视图self.scrollview.delegate = self;- (void)scrollViewDidScroll:(UIScrollView *)scrollView {//当前滚动到的x位置CGFloat X = scrollView.contentOffset.x;//滚动视图展示宽度,即已经滚动过的宽度CGFloat screenWidth = CGRectGetWidth(scrollView.frame);//滚动视图内容的宽度,在我的视图中,就是5个屏幕的宽度CGFloat contentWidth = scrollView.contentSize.width;// 滚动到最后一张视图之后,将滚动位置重置到第二张图片;这样做的话,滚动视图位置被提前,就可以继续对视图进行从左到右的滚动if (X >= contentWidth - screenWidth) {[scrollView setContentOffset:CGPointMake(screenWidth, 0) animated:NO];}// 滚动到第一张视图之前,将滚动位置重置到倒数第二张图片;这样做的话,滚动视图位置被滞后,就可以继续对视图进行从右向左的滚动else if (X < screenWidth - scrollView.frame.size.width) {[scrollView setContentOffset:CGPointMake(contentWidth - 2 * screenWidth, 0) animated:NO];}
}

换头像

这个功能是一个在自己创建的照片墙中选择图片并且传递到上一个页面,在头像框内显示的操作,这里我使用了协议传值的方法,通过将图片传递给上一个界面实现了该功能,这里通过代码展示。

if(self.selectedCount == 0) {self.elertView = [UIAlertController alertControllerWithTitle:@"警告" message:@"请选择一张图片更换" preferredStyle:UIAlertControllerStyleAlert];UIAlertAction* action = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction* action){}];[self.elertView addAction:action];[self presentViewController:self.elertView animated:YES completion:nil];} else if (self.selectedCount == 1) {for(UIView* subview in self.scrollview.subviews) {if([subview isKindOfClass:[UIButton class]]) {UIButton* button = (UIButton*) subview;if(button.selected) {UIImage* image = [button imageForState:UIControlStateNormal];[self.delegate ChangePhoto:image];button.selected = NO;[self.navigationController popViewControllerAnimated:YES];break;}}}} else {self.elertView = [UIAlertController alertControllerWithTitle:@"警告" message:@"禁止一次选用多张图片更换" preferredStyle:UIAlertControllerStyleAlert];UIAlertAction* action = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction* action){}];[self.elertView addAction: action];[self presentViewController:self.elertView animated:YES completion:nil];}

简单的网络请求

简单的网络请求主要有以下几个步骤:

  1. 创建请求地址
  2. 创建请求类
  3. 创建会话
  4. 根据会话创建任务
  5. 启动任务

这里就不详细讲解网络请求的内容了,如果需要了解内容,可以看看本人的这篇博客:【iOS】APP仿写——天气预报
主要需要注意网络请求异步的问题。

UISearchController

UISearchController提供了一个搜索栏和搜索结果视图的管理器,使得在应用中集成搜索功能变得简单。它可以用于在单个视图控制器中添加搜索功能,也可以与其他视图控制器(如UITableViewController)一起使用。 我们要实现UISearchControllerDelegate, UISearchResultsUpdatig两个协议来实现想要显示的效果,下面通过代码展示:

self.search = [[UISearchController alloc] initWithSearchResultsController:nil];self.search.delegate = self;self.search.searchResultsUpdater = self;//设置searchbar的尺寸[self.search.searchBar sizeToFit];//在搜索时将背景模糊化,因为模糊背景的目的是将焦点集中在搜索结果上,而不是与背景内容进行交互。self.search.obscuresBackgroundDuringPresentation = YES;//隐藏导航控制栏self.search.hidesNavigationBarDuringPresentation = YES;//设置未编辑状态的searchbar的位置self.search.searchBar.frame = CGRectMake(0, 150, [UIScreen mainScreen].bounds.size.width, 55);
//    _tableview.tableHeaderView = self.search.searchBar;[self.view addSubview:self.search.searchBar];self.search.searchBar.searchBarStyle = UISearchBarStyleDefault;UISearchBar* searchbar = self.search.searchBar;searchbar.placeholder = @"请输入搜索的城市";

在这里插入图片描述

上面是本人在完成项目中一些觉得比较有代表性的学习的内容,如果你还想了解更多的内容,去看看笔者之前写的博客吧。


http://www.mrgr.cn/news/19008.html

相关文章:

  • Spring 学习笔记
  • 探索 Nuxt Devtools:功能全面指南
  • Vue3 中的响应式系统:深入理解 Proxy API
  • LeetCode LCR088.使用最小花费爬楼梯
  • wget下载速度受到哪些因素影响?
  • 揭秘Java后端框架:从Spring到Netty,全方位解析!
  • 【归并分而治之】逆序对的应对之策
  • KTH5641 系列具有模拟输出的比例式线性霍尔效应传感器
  • 【Qt】消息对话框 QMessageBox
  • 7.Lab Six —— Cow Fork
  • 6. MyBatis中的@Mapper注解和XML映射文件的区别是什么?
  • 【重构获得模式 Refactoring to Patterns】
  • 屏幕像素初步认识
  • 【非常简单】 猿人学web第一届 第17题 天杀的 Http2.0
  • 最新2024年国际EI会议集合
  • C语言从头学55——学习头文件errno.h、float.h
  • c++引用和指针
  • 通过vscode连接linux服务器时terminal显示空白无法运行
  • VMware Workstation 17.6 Pro 发布下载,新增功能概览
  • SMB攻击利用之-设置远程mimikatz程序为定时任务流量数据包分析