开发者社区> 文顶顶> 正文
阿里云
为了无法计算的价值
打开APP
阿里云APP内打开

iOS开发实用技巧—项目新特性页面的处理

简介:
+关注继续查看

iOS开发实用技巧篇—项目新特性页面的处理

说明:本文主要说明在项目开发中会涉及到的最最简单的新特性界面(实用UIScrollView展示多张图片的轮播)的处理。

代码示例:

新建一个专门的处理新特性界面的控制器,可以实用代码也可以用xib,在这里实用纯代码方式,创建一个控制器NewfeatureViewController。

头文件代码:

复制代码
 1 //  2 // JMNewfeatureViewController.h
 3 //
 4  5 #import <UIKit/UIKit.h>
 6  7 typedef enum:NSInteger
 8 {
 9 NewfeatureTypeFromeSetting, //从设置界面进入该页 10 NewfeatureTypeFromeWelcom, //第一次安装的时候进入 11 } NewfeatureType;
12 13 @interface JMNewfeatureViewController : UIViewController
14 15 @property(nonatomic,assign)NewfeatureType newfeatureType;
16 17 @end
复制代码

.m文件代码:

复制代码
 1 //  2 // JMNewfeatureViewController.m
 3 //
 4  5 #import "JMNewfeatureViewController.h"  6 #import "JMTabBarViewController.h"  7 #import "JMAboutTableViewController.h"  8  9 #define JMNewfeatureImageCount 4
 10  11 @interface JMNewfeatureViewController () <UIScrollViewDelegate>
 12  13 @property (nonatomic, weak) UIPageControl *pageControl;
 14  15  16 - (void)setupScrollView;
 17 - (void)setupPageControl;
 18 - (void)setupLastImageView:(UIImageView *)imageView;
 19 - (void)setupStartButton:(UIImageView *)imageView;
 20  21 @end  22  23 @implementation JMNewfeatureViewController
 24  25 #pragma mark ---------------------
 26 #pragma mark - CycLife
 27  28 - (void)viewDidLoad
 29 {
 30  [super viewDidLoad];
 31  32 [UIApplication sharedApplication].statusBarHidden = YES;
 33  34 [self setupScrollView]; // 添加UISrollView  35 [self setupPageControl]; // 添加pageControl  36 }
 37  38 #pragma mark ---------------------
 39 #pragma mark - Methods
 40  41 //添加UISrollView  42 - (void)setupScrollView
 43 {
 44 // 添加UISrollView  45 UIScrollView *scrollView = [[UIScrollView alloc] init];
 46 scrollView.frame = self.view.bounds;
 47 scrollView.bounces = NO;
 48 scrollView.delegate = self;
 49  [self.view addSubview:scrollView];
 50  51 // 添加图片  52 CGFloat imageW = scrollView.width;
 53 CGFloat imageH = scrollView.height;
 54 for (int i = 0; i<JMNewfeatureImageCount; i++) {
 55 // 创建UIImageView  56 UIImageView *imageView = [[UIImageView alloc] init];
 57 NSString *name = [NSString stringWithFormat:@"banner%d.jpg", i + 1];
 58 imageView.image = [UIImage imageNamed:name];
 59  [scrollView addSubview:imageView];
 60  61 // 设置frame  62 imageView.y = 0;
 63 imageView.width = imageW;
 64 imageView.height = imageH;
 65 imageView.x = i * imageW;
 66  67 // 给最后一个imageView添加按钮  68 if (i == JMNewfeatureImageCount - 1) {
 69  [self setupLastImageView:imageView];
 70  }
 71  }
 72  73 // 3.设置其他属性  74 scrollView.contentSize = CGSizeMake(JMNewfeatureImageCount * imageW, 0);
 75 scrollView.pagingEnabled = YES;
 76 scrollView.showsHorizontalScrollIndicator = NO;
 77 scrollView.backgroundColor = YYColor(246, 246, 246);
 78 }
 79  80 //添加pageControl  81 - (void)setupPageControl
 82 {
 83 // 添加PageControl  84 UIPageControl *pageControl = [[UIPageControl alloc] init];
 85 pageControl.numberOfPages = JMNewfeatureImageCount;
 86 pageControl.centerX = self.view.width * 0.5;
 87 pageControl.centerY = self.view.height - 20;
 88  [self.view addSubview:pageControl];
 89  90 // 设置圆点的颜色  91 self.pageControl = pageControl;
 92  [self changePageControlImage:self.pageControl];
 93 }
 94  95  96 //设置最后一个UIImageView中的内容  97 - (void)setupLastImageView:(UIImageView *)imageView
 98 {
 99 imageView.userInteractionEnabled = YES;
100 101 // 添加开始按钮 102  [self setupStartButton:imageView];
103 }
104 105 //添加开始按钮 106 - (void)setupStartButton:(UIImageView *)imageView
107 {
108 // 1.添加开始按钮 109 UIButton *startButton = [[UIButton alloc] init];
110 imageView.userInteractionEnabled = YES;
111  [imageView addSubview:startButton];
112 113 // 2.设置背景图片 114 [startButton setBackgroundImage:[UIImage imageNamed:@"banner_button_moren.jpg"] forState:UIControlStateNormal];
115 [startButton setBackgroundImage:[UIImage imageNamed:@"banner_button_dianji.jpg"] forState:UIControlStateHighlighted];
116 117 // 3.设置frame 118 startButton.size = startButton.currentBackgroundImage.size;
119 startButton.centerX = self.view.width * 0.5;
120 startButton.centerY = self.view.height * 0.8;
121 122 // 4.设置文字 123 [startButton setTitle:@"立即体验" forState:UIControlStateNormal];
124 [startButton setTitle:@"" forState:UIControlStateHighlighted];
125  [startButton addTarget:self action:@selector(start) forControlEvents:UIControlEventTouchUpInside];
126 }
127 128 129 //改变pagecontrol中圆点样式 130 - (void)changePageControlImage:(UIPageControl *)pageControl
131 {
132 static UIImage *imgCurrent = nil;
133 static UIImage *imgOther = nil;
134 static dispatch_once_t onceToken;
135 136 dispatch_once(&onceToken, ^{
137 imgCurrent = [UIImage imageNamed:@"yuan_01"];
138 imgOther = [UIImage imageNamed:@"yuan1"];
139  });
140 141 142 if (kSystemVersionMoreThan7) {
143 [pageControl setValue:imgCurrent forKey:@"_currentPageImage"];
144 [pageControl setValue:imgOther forKey:@"_pageImage"];
145 } else {
146 for (int i = 0;i < pageControl.numberOfPages; i++) {
147 UIImageView *imgv = [pageControl.subviews objectAtIndex:i];
148 imgv.frame = CGRectMake(imgv.frame.origin.x, imgv.frame.origin.y, 20, 20);
149 imgv.image = pageControl.currentPage == i ? imgCurrent : imgOther;
150  }
151  }
152 }
153 154 #pragma mark ---------------------
155 #pragma mark - Events
156 157 //立即体验 158 - (void)start
159 {
160 [UIApplication sharedApplication].statusBarHidden = NO;
161 162 //判断类型 163 if (self.newfeatureType == NewfeatureTypeFromeWelcom) {
164 JMTabBarViewController *tabVC = [[JMTabBarViewController alloc]init];
165 // 切换控制器 166 UIWindow *window = [UIApplication sharedApplication].keyWindow;
167 window.rootViewController = tabVC;
168 }else 169  {
170 171  [self.navigationController popViewControllerAnimated:YES];
172  [self.navigationController setNavigationBarHidden:NO animated:NO];
173  }
174 175 }
176 177 #pragma mark - UIScrollViewDelegate
178 - (void)scrollViewDidScroll:(UIScrollView *)scrollView
179 {
180 // 获得页码 181 CGFloat doublePage = scrollView.contentOffset.x / scrollView.width;
182 int intPage = (int)(doublePage + 0.5);
183 184 // 设置页码 185 self.pageControl.currentPage = intPage;
186  [self changePageControlImage:self.pageControl];
187 }
188 189 @end
复制代码

注意点:

  下面的方法可以为pageControl提供当前状态和默认状态下的图片设置。

复制代码
 1 //改变pagecontrol中圆点样式  2 - (void)changePageControlImage:(UIPageControl *)pageControl
 3 {
 4 static UIImage *imgCurrent = nil;
 5 static UIImage *imgOther = nil;
 6 static dispatch_once_t onceToken;
 7  8 dispatch_once(&onceToken, ^{
 9 imgCurrent = [UIImage imageNamed:@"yuan_01"];
10 imgOther = [UIImage imageNamed:@"yuan1"];
11  });
12 13 14 if (kSystemVersionMoreThan7) {
15 [pageControl setValue:imgCurrent forKey:@"_currentPageImage"];
16 [pageControl setValue:imgOther forKey:@"_pageImage"];
17 } else {
18 for (int i = 0;i < pageControl.numberOfPages; i++) {
19 UIImageView *imgv = [pageControl.subviews objectAtIndex:i];
20 imgv.frame = CGRectMake(imgv.frame.origin.x, imgv.frame.origin.y, 20, 20);
21 imgv.image = pageControl.currentPage == i ? imgCurrent : imgOther;
22  }
23  }
24 }
复制代码

本例中,新特性部分的业务逻辑非常简单,可以直接套用。

实用图片替换pageControl的效果如下:

版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

相关文章
iOS11新特性:新增拖拽交互体验(二)
iOS11新特性:新增拖拽交互体验
127 0
iOS11新特性:新增拖拽交互体验(一)
iOS11新特性:新增拖拽交互体验
115 0
iOS11新特性:新增拖拽交互体验(三)
iOS11新特性:新增拖拽交互体验
139 0
iOS NSInvocation应用与理解
iOS NSInvocation应用与理解
51 0
iOS7应用开发3、Objective-C
【跟随教授的讲解和演示,并重做了课上的demo之后,惊奇地发现自己写的程序有bug,界面上12张卡牌出现后,点击任何一个,其他所有卡牌都会变成一块白板……在经历了长时间的调试之后悲催地发现,在最后的updateUI这个函数中改变背景的函数写错了,setBackgroundImage:写成了setImage:,被这一无脑的失误纠结了这么久,丢人啊……各种bug改完后,前几次课的代码都已经传到博客的资源中了,可到我的资源页下载。
756 0
+关注
172
文章
0
问答
文章排行榜
最热
最新
相关电子书
更多
低代码开发师(初级)实战教程
立即下载
阿里巴巴DevOps 最佳实践手册
立即下载
冬季实战营第三期:MySQL数据库进阶实战
立即下载