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

ios使用UIScrollView和PageControl创建图片轮播

1.创建cocoa touch class

2.同时创建xib页面

3.SceneDelegate设置根视图控制器

//
//  SceneDelegate.m
//  iosstudy2024
//
//  Created by figo on 2024/8/5.
//#import "SceneDelegate.h"
#import "WidgetViewController.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).//默认是使用ViewController,这里改成WidgetViewController为根视图WidgetViewController * wvc = [[WidgetViewController alloc]init];//[self.window setRootViewController:wvc];self.window.rootViewController=wvc;
}- (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.NSLog(@"%s",__func__);
}- (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).NSLog(@"%s",__func__);
}- (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.NSLog(@"%s",__func__);
}- (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.NSLog(@"%s",__func__);
}@end

4.设计xib页面

5.编写WidgetViewController

//
//  WidgetViewController.m
//  iosstudy2024
//
//  Created by figo on 2024/12/5.
//#import "WidgetViewController.h"@interface WidgetViewController ()<UIScrollViewDelegate>
@property (weak, nonatomic) IBOutlet UIScrollView *scrollViewTest;
@property (weak, nonatomic) IBOutlet UIPageControl *pageControll;@end@implementation WidgetViewController- (void)viewDidLoad {[super viewDidLoad];//1.实现了UIScrollViewDelegate,代理设置成自己self.scrollViewTest.delegate=self;//2.获取屏幕宽度float width=[UIScreen mainScreen].bounds.size.width;//3.设置scrollView宽和高[self.scrollViewTest setFrame:CGRectMake(0, 200, width, 200)];self.scrollViewTest.contentSize=CGSizeMake(3*width, 200);//4.添加3张imgView,imgView相对scrollView来说,那么它的左上角坐标是0,0,不要设置错,否则会不显示self.pageControll.numberOfPages=3;//这里设置页数,同图片数量,这里简单的添加3张UIImageView *imgView=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, width, 200)];imgView.image=[UIImage imageNamed:@"star"];[self.scrollViewTest addSubview:imgView];UIImageView *imgView1=[[UIImageView alloc]initWithFrame:CGRectMake(0+width, 0, width, 200)];imgView1.image=[UIImage imageNamed:@"diamond"];[self.scrollViewTest addSubview:imgView1];UIImageView *imgView2=[[UIImageView alloc]initWithFrame:CGRectMake(0+2*width, 0, width, 200)];imgView2.image=[UIImage imageNamed:@"grape"];[self.scrollViewTest addSubview:imgView2];
}
//5.通过scrollView滑动偏移量计算pageIndex
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{CGFloat offset=self.scrollViewTest.contentOffset.x;NSInteger index=offset/[UIScreen mainScreen].bounds.size.width;self.pageControll.currentPage=index;
}
/*
#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

6.运行效果

http://www.lryc.cn/news/500424.html

相关文章:

  • 3D 生成重建024-LGM第一个开源的3D生成大模型!
  • linux目录权限
  • 语言模型使用心得
  • ChatGPT客户端安装教程(附下载链接)
  • Electron 基础+传值+引用+安全
  • 手机租赁系统全面解析与开发指南
  • mongoDb的读session和写session权限报错问题
  • Centos在2024年6月30日停止维护后如何换yum源安装组件
  • 阿里云ACP云计算模拟试题(附答案解析)
  • 简单的爬虫脚本编写
  • [MySQL基础](三)SQL--图形化界面+DML
  • 11.23[大数据]
  • C++ 游戏开发进阶:打造更精彩的游戏世界
  • 想在iPad上远程操作安卓手机的APP,怎样实现iPad远程控制安卓?
  • GPS北斗卫星授时服务器功能是什么?应用是什么?
  • 利用Java爬虫获取商品数据的完整指南
  • mysql 迁移达梦数据库出现的 sql 语法问题 以及迁移方案
  • 深入解析css-浮动-学习小结
  • 【机器学习】机器学习的基本分类-无监督学习-K-Means聚类
  • .NET for Android/iOS应用的如何在各自的系统运行
  • 访问django后台,提示CSRF验证失败. 请求被中断403
  • Scala的隐式转换(1)
  • 华为TaurusDB与GaussDB:信创改造的“降本提效”之路
  • Linux网络编程---本地套接字
  • 数据结构之四:堆和二叉树
  • 【论文阅读】国际开源发展经验及其对我国开源创新体系建设的启示
  • redis击穿,穿透,雪崩以及解决方案
  • 时频转换 | Matlab格拉姆角和场Gramian angular summation field一维数据转二维图像方法
  • qt QCryptographicHash详解
  • 亚马逊云科技大语言模型加速OCR应用场景发展