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

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

代码示例:

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

头文件代码:

 //
// JMNewfeatureViewController.h
// #import <UIKit/UIKit.h> typedef enum:NSInteger
{
NewfeatureTypeFromeSetting, //从设置界面进入该页
NewfeatureTypeFromeWelcom, //第一次安装的时候进入
} NewfeatureType; @interface JMNewfeatureViewController : UIViewController @property(nonatomic,assign)NewfeatureType newfeatureType; @end

.m文件代码:

 //
// JMNewfeatureViewController.m
// #import "JMNewfeatureViewController.h"
#import "JMTabBarViewController.h"
#import "JMAboutTableViewController.h" #define JMNewfeatureImageCount 4 @interface JMNewfeatureViewController () <UIScrollViewDelegate> @property (nonatomic, weak) UIPageControl *pageControl; - (void)setupScrollView;
- (void)setupPageControl;
- (void)setupLastImageView:(UIImageView *)imageView;
- (void)setupStartButton:(UIImageView *)imageView; @end @implementation JMNewfeatureViewController #pragma mark ---------------------
#pragma mark - CycLife - (void)viewDidLoad
{
[super viewDidLoad]; [UIApplication sharedApplication].statusBarHidden = YES; [self setupScrollView]; // 添加UISrollView
[self setupPageControl]; // 添加pageControl
} #pragma mark ---------------------
#pragma mark - Methods //添加UISrollView
- (void)setupScrollView
{
// 添加UISrollView
UIScrollView *scrollView = [[UIScrollView alloc] init];
scrollView.frame = self.view.bounds;
scrollView.bounces = NO;
scrollView.delegate = self;
[self.view addSubview:scrollView]; // 添加图片
CGFloat imageW = scrollView.width;
CGFloat imageH = scrollView.height;
for (int i = ; i<JMNewfeatureImageCount; i++) {
// 创建UIImageView
UIImageView *imageView = [[UIImageView alloc] init];
NSString *name = [NSString stringWithFormat:@"banner%d.jpg", i + ];
imageView.image = [UIImage imageNamed:name];
[scrollView addSubview:imageView]; // 设置frame
imageView.y = ;
imageView.width = imageW;
imageView.height = imageH;
imageView.x = i * imageW; // 给最后一个imageView添加按钮
if (i == JMNewfeatureImageCount - ) {
[self setupLastImageView:imageView];
}
} // 3.设置其他属性
scrollView.contentSize = CGSizeMake(JMNewfeatureImageCount * imageW, );
scrollView.pagingEnabled = YES;
scrollView.showsHorizontalScrollIndicator = NO;
scrollView.backgroundColor = YYColor(, , );
} //添加pageControl
- (void)setupPageControl
{
// 添加PageControl
UIPageControl *pageControl = [[UIPageControl alloc] init];
pageControl.numberOfPages = JMNewfeatureImageCount;
pageControl.centerX = self.view.width * 0.5;
pageControl.centerY = self.view.height - ;
[self.view addSubview:pageControl]; // 设置圆点的颜色
self.pageControl = pageControl;
[self changePageControlImage:self.pageControl];
} //设置最后一个UIImageView中的内容
- (void)setupLastImageView:(UIImageView *)imageView
{
imageView.userInteractionEnabled = YES; // 添加开始按钮
[self setupStartButton:imageView];
} //添加开始按钮
- (void)setupStartButton:(UIImageView *)imageView
{
// 1.添加开始按钮
UIButton *startButton = [[UIButton alloc] init];
imageView.userInteractionEnabled = YES;
[imageView addSubview:startButton]; // 2.设置背景图片
[startButton setBackgroundImage:[UIImage imageNamed:@"banner_button_moren.jpg"] forState:UIControlStateNormal];
[startButton setBackgroundImage:[UIImage imageNamed:@"banner_button_dianji.jpg"] forState:UIControlStateHighlighted]; // 3.设置frame
startButton.size = startButton.currentBackgroundImage.size;
startButton.centerX = self.view.width * 0.5;
startButton.centerY = self.view.height * 0.8; // 4.设置文字
[startButton setTitle:@"立即体验" forState:UIControlStateNormal];
[startButton setTitle:@"" forState:UIControlStateHighlighted];
[startButton addTarget:self action:@selector(start) forControlEvents:UIControlEventTouchUpInside];
} //改变pagecontrol中圆点样式
- (void)changePageControlImage:(UIPageControl *)pageControl
{
static UIImage *imgCurrent = nil;
static UIImage *imgOther = nil;
static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{
imgCurrent = [UIImage imageNamed:@"yuan_01"];
imgOther = [UIImage imageNamed:@"yuan1"];
}); if (kSystemVersionMoreThan7) {
[pageControl setValue:imgCurrent forKey:@"_currentPageImage"];
[pageControl setValue:imgOther forKey:@"_pageImage"];
} else {
for (int i = ;i < pageControl.numberOfPages; i++) {
UIImageView *imgv = [pageControl.subviews objectAtIndex:i];
imgv.frame = CGRectMake(imgv.frame.origin.x, imgv.frame.origin.y, , );
imgv.image = pageControl.currentPage == i ? imgCurrent : imgOther;
}
}
} #pragma mark ---------------------
#pragma mark - Events //立即体验
- (void)start
{
[UIApplication sharedApplication].statusBarHidden = NO; //判断类型
if (self.newfeatureType == NewfeatureTypeFromeWelcom) {
JMTabBarViewController *tabVC = [[JMTabBarViewController alloc]init];
// 切换控制器
UIWindow *window = [UIApplication sharedApplication].keyWindow;
window.rootViewController = tabVC;
}else
{ [self.navigationController popViewControllerAnimated:YES];
[self.navigationController setNavigationBarHidden:NO animated:NO];
} } #pragma mark - UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
// 获得页码
CGFloat doublePage = scrollView.contentOffset.x / scrollView.width;
int intPage = (int)(doublePage + 0.5); // 设置页码
self.pageControl.currentPage = intPage;
[self changePageControlImage:self.pageControl];
} @end

注意点:

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

 //改变pagecontrol中圆点样式
- (void)changePageControlImage:(UIPageControl *)pageControl
{
static UIImage *imgCurrent = nil;
static UIImage *imgOther = nil;
static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{
imgCurrent = [UIImage imageNamed:@"yuan_01"];
imgOther = [UIImage imageNamed:@"yuan1"];
}); if (kSystemVersionMoreThan7) {
[pageControl setValue:imgCurrent forKey:@"_currentPageImage"];
[pageControl setValue:imgOther forKey:@"_pageImage"];
} else {
for (int i = ;i < pageControl.numberOfPages; i++) {
UIImageView *imgv = [pageControl.subviews objectAtIndex:i];
imgv.frame = CGRectMake(imgv.frame.origin.x, imgv.frame.origin.y, , );
imgv.image = pageControl.currentPage == i ? imgCurrent : imgOther;
}
}
}

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

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

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

05-08 15:13