01-11 引导页(2)= 文顶顶
OS开发实用技巧篇—项目新特性页面的处理
说明:本文主要说明在项目开发中会涉及到的最最简单的新特性界面(实用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的效果如下: