1 #import "ViewController.h" 2 3 4 5 #define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width 6 7 #define SCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height 8 9 #define IMAGEVIEW_COUNT 3 10 11 12 13 @interface ViewController () <UIScrollViewDelegate> { 14 15 UIScrollView *_scrollView; 16 17 UIImageView *_leftImageView; 18 19 UIImageView *_centerImageView; 20 21 UIImageView *_rightImageView; 22 23 UIPageControl *_pageControl; 24 25 UILabel *_label; 26 27 NSMutableDictionary *_imageData;//图片数据 28 29 int _currentImageIndex;//当前图片索引 30 31 int _imageCount;//图片总数 32 33 } 34 35 36 37 @end 38 39 40 41 @implementation ViewController 42 43 44 45 - (void)viewDidLoad { 46 47 [super viewDidLoad]; 48 49 //加载数据 50 51 [self loadImageData]; 52 53 //添加滚动控件 54 55 [self addScrollView]; 56 57 //添加图片控件 58 59 [self addImageViews]; 60 61 //添加分页控件 62 63 [self addPageControl]; 64 65 //添加图片信息描述控件 66 67 [self addLabel]; 68 69 //加载默认图片 70 71 [self setDefaultImage]; 72 73 } 74 75 #pragma mark 加载图片数据 76 77 -(void)loadImageData { 78 79 //读取程序包路径中的资源文件 80 81 NSString *path = [[NSBundle mainBundle] pathForResource:@"imageInfo" ofType:@"plist"]; 82 83 _imageData = [NSMutableDictionary dictionaryWithContentsOfFile:path]; 84 85 _imageCount = (int)_imageData.count; 86 87 } 88 89 90 91 #pragma mark 添加控件 92 93 -(void)addScrollView { 94 95 // _scrollView = [[UIScrollView alloc] initWithFrame:[UIScreen mainScreen].bounds]; 96 97 _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 20+20, SCREEN_WIDTH, 200)]; 98 99 _scrollView.backgroundColor = [UIColor lightGrayColor]; 100 101 //设置代理 102 103 _scrollView.delegate = self; 104 105 //设置滚动范围 106 107 _scrollView.contentSize = CGSizeMake(IMAGEVIEW_COUNT*SCREEN_WIDTH, 0); 108 109 //设置当前显示的位置为中间图片(设置scrollView偏移量) 110 111 [_scrollView setContentOffset:CGPointMake(SCREEN_WIDTH, 0)]; 112 113 //设置分页 114 115 _scrollView.pagingEnabled = YES; 116 117 //隐藏水平条 竖向条 118 119 _scrollView.showsHorizontalScrollIndicator = NO; 120 121 _scrollView.showsVerticalScrollIndicator = NO; 122 123 //关闭弹簧效果 124 125 // _scrollView.bounces = NO; 126 127 [self.view addSubview:_scrollView]; 128 129 } 130 131 132 133 #pragma mark 添加图片三个控件 134 135 -(void)addImageViews { 136 137 _leftImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, _scrollView.frame.size.height)]; 138 139 //会保证图片比例不变,而且全部显示在ImageView中,这意味着ImageView会有部分空白 140 141 _leftImageView.contentMode=UIViewContentModeScaleAspectFit; 142 143 [_scrollView addSubview:_leftImageView]; 144 145 146 147 _centerImageView = [[UIImageView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH, 0, SCREEN_WIDTH, _scrollView.frame.size.height)]; 148 149 _centerImageView.contentMode = UIViewContentModeScaleAspectFit; 150 151 [_scrollView addSubview:_centerImageView]; 152 153 154 155 _rightImageView = [[UIImageView alloc] initWithFrame:CGRectMake(2*SCREEN_WIDTH, 0, SCREEN_WIDTH, _scrollView.frame.size.height)]; 156 157 _rightImageView.contentMode = UIViewContentModeScaleAspectFit; 158 159 [_scrollView addSubview:_rightImageView]; 160 161 } 162 163 #pragma mark 设置默认显示图片 164 165 -(void)setDefaultImage { 166 167 //加载默认图片 168 169 _leftImageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%i.jpeg",_imageCount-1]]; 170 171 _centerImageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%i.jpeg",0]]; 172 173 _rightImageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%i.jpeg",1]]; 174 175 _currentImageIndex = 0; 176 177 //设置当前页 178 179 _pageControl.currentPage = _currentImageIndex; 180 181 NSString *imageName = [NSString stringWithFormat:@"%i.jpeg",_currentImageIndex]; 182 183 _label.text = _imageData[imageName]; 184 185 } 186 187 188 189 #pragma mark 添加分页控件 190 191 -(void)addPageControl { 192 193 _pageControl = [[UIPageControl alloc] init]; 194 195 // _pageControl.backgroundColor = [UIColor orangeColor]; 196 197 //注意此方法可以根据页数返回UIPageControl合适的大小 198 199 CGSize size = [_pageControl sizeForNumberOfPages:_imageCount]; 200 201 _pageControl.bounds = CGRectMake(0, 0, size.width, size.height); 202 203 _pageControl.center = CGPointMake(SCREEN_WIDTH*2/3, _scrollView.frame.origin.y+_scrollView.frame.size.height-10); 204 205 //设置颜色 206 207 _pageControl.pageIndicatorTintColor = [UIColor colorWithRed:193/255.0 green:219/255.0 blue:249/255.0 alpha:1]; 208 209 //设置当前页颜色 210 211 _pageControl.currentPageIndicatorTintColor = [UIColor colorWithRed:0 green:150/255.0 blue:1 alpha:1]; 212 213 //设置总页数 214 215 _pageControl.numberOfPages = _imageCount; 216 217 [self.view addSubview:_pageControl]; 218 219 } 220 221 222 223 #pragma mark 添加信息描述控件 224 225 -(void)addLabel { 226 227 _label = [[UILabel alloc] initWithFrame:CGRectMake(0, 20, SCREEN_WIDTH, 20)]; 228 229 // _label.backgroundColor = [UIColor orangeColor]; 230 231 _label.textAlignment = NSTextAlignmentCenter; 232 233 _label.textColor = [UIColor colorWithRed:0 green:150/255.0 blue:1 alpha:1]; 234 235 [self.view addSubview:_label]; 236 237 } 238 239 240 241 #pragma mark 滚动停止事件 242 243 -(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView { 244 245 //重新加载图片 246 247 [self reloadImage]; 248 249 //移动到中间 250 251 [_scrollView setContentOffset:CGPointMake(SCREEN_WIDTH, 0)]; 252 253 //设置分页 254 255 _pageControl.currentPage = _currentImageIndex; 256 257 //设置描述 258 259 NSString *imageName = [NSString stringWithFormat:@"%i.jpeg",_currentImageIndex]; 260 261 _label.text = _imageData[imageName]; 262 263 } 264 265 266 267 #pragma mark 重新加载图片 268 269 -(void)reloadImage { 270 271 int leftImageIndex,rightImageIndex; 272 273 CGPoint offset = [_scrollView contentOffset]; 274 275 if (offset.x > SCREEN_WIDTH) { //向右滑动 276 277 _currentImageIndex = (_currentImageIndex+1)%_imageCount; 278 279 }else if(offset.x < SCREEN_WIDTH) { //向左滑动 280 281 _currentImageIndex = (_currentImageIndex+_imageCount-1)%_imageCount; 282 283 } 284 285 _centerImageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%i.jpeg",_currentImageIndex]]; 286 287 288 289 //重新设置左右图片 290 291 leftImageIndex = (_currentImageIndex+_imageCount-1)%_imageCount; 292 293 rightImageIndex = (_currentImageIndex+1)%_imageCount; 294 295 _leftImageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%i.jpeg",leftImageIndex]]; 296 297 _rightImageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%i.jpeg",rightImageIndex]]; 298 299 } 300 301 @end 302 303