iOS - 功能模块 - 幻灯片切换
来源:互联网 发布:中国2015旅游数据分析 编辑:程序博客网 时间:2024/05/21 09:34
依据性能需求,实现iOS幻灯片切换功能。
1.图片少的情况下
Demo:https://github.com/shileseal/SLCarouselDemo
#import "ViewController.h"@interfaceViewController ()<UIScrollViewDelegate>@property (weak, nonatomic) IBOutletUIScrollView *scrollView;@property (weak, nonatomic) IBOutletUIPageControl *pageControl;@property (nonatomic, strong) NSTimer *timer;@end@implementation ViewController- (void)viewDidLoad { [superviewDidLoad]; CGFloat imageW = self.scrollView.frame.size.width; CGFloat imageH = self.scrollView.frame.size.height; CGFloat imageY = 0; NSInteger number = 5; for (int i = 0; i < 5; i++) { UIImageView *imageView = [[UIImageViewalloc]init]; NSString *imageUrl = [NSStringstringWithFormat:@"%d",i]; NSLog(@"%@",imageUrl); imageView.image = [UIImageimageNamed:imageUrl]; CGFloat imageX = i * imageW; imageView.frame = CGRectMake(imageX, imageY, imageW, imageH); self.scrollView.showsHorizontalScrollIndicator = NO; [self.scrollViewaddSubview:imageView]; } CGFloat contentW = number * imageW; self.scrollView.contentSize = CGSizeMake(contentW, 0); self.scrollView.pagingEnabled = YES; self.scrollView.delegate = self; [selfaddTimer];}- (void)nextImage { int page = (int)self.pageControl.currentPage; if (page == 4){ page = 0; } else { page ++; } CGFloat x = page * self.scrollView.frame.size.width; self.scrollView.contentOffset = CGPointMake(x, 0);}- (void)scrollViewDidScroll:(UIScrollView *)scrollView { NSLog(@"滚动中"); CGFloat scrollviewW = scrollView.frame.size.width; CGFloat x = self.scrollView.contentOffset.x; int page = (x + scrollviewW/2)/scrollviewW; self.pageControl.currentPage = page;}- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView { [selfremoveTimer];}- (void)addTimer { self.timer = [NSTimerscheduledTimerWithTimeInterval:1target:selfselector:@selector(nextImage) userInfo:nilrepeats:YES];}- (void)removeTimer { [self.timerinvalidate];}@end
2.图片多的情况下,使用UICollectionView进行复用
Demo:https://github.com/shileseal/SLScrollViewReuseDemo
#import "ViewController.h"@interfaceViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>@property (weak, nonatomic) IBOutletUICollectionView *collectionView;@end staticNSString *identifier = @"cell";@implementation ViewController- (void)viewDidLoad { [superviewDidLoad]; self.collectionView.delegate = self; self.collectionView.dataSource = self; [self.collectionViewregisterClass:[UICollectionViewCellclass] forCellWithReuseIdentifier:identifier];}#pragma mark - UICollectionViewDataSource- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView { return1;}- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { return50;}- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifierforIndexPath:indexPath]; NSString *imageUrl = [NSStringstringWithFormat:@"%ld.png",(long)indexPath.row]; NSLog(@"%@",imageUrl); UIImageView *imageView = [[UIImageViewalloc]initWithImage:[UIImageimageNamed:imageUrl]]; #warning 为什么这么写就无法显示呢?// UIImageView *imageView = [[UIImageView alloc]init];// imageView.image = [UIImage imageNamed:imageUrl]; [cell addSubview:imageView]; [cell bringSubviewToFront:imageView]; switch (indexPath.row%2) { case1: cell.backgroundColor = [UIColorwhiteColor]; break; default: cell.backgroundColor = [UIColorblueColor]; break; } NSLog(@"%@",cell); return cell;}@end
3.iCarousel
https://github.com/nicklockwood/iCarousel
2 0
- iOS - 功能模块 - 幻灯片切换
- 幻灯片切换
- FLIASH幻灯片切换特效
- css3透明幻灯片切换
- 图片幻灯片切换特效
- JS幻灯片切换效果
- iOS - 功能模块 - 设置页面
- iOS - 功能模块 - 网络封装
- iOS - 功能模块 - 发布时间
- KinSlideshow幻灯片插件 Jquery幻灯片 图片切换
- 不错的幻灯片切换网站
- js图片切换 幻灯片效果
- jquery 幻灯片 图片切换效果
- 图片像幻灯片一样切换
- jquery 幻灯片图像切换效果
- 自定义PowerPoint切换幻灯片按钮
- js实现幻灯片自动切换
- 功能模块
- 中国式社交
- HDU 2512 一卡通大冒险 (集合划分)
- @property的默认属性
- uva 11177 不明所以的凸多边形与圆的面积模板——二分
- Java复习笔记(三)——文档注释
- iOS - 功能模块 - 幻灯片切换
- HBase分布式安装指南
- c\c++中的形参与实参
- ubuntu安装不了软件
- NYOJ 636 世界末日
- 数组和表
- finalize方法的使用
- 关于jsp网页弹出窗口[很多种方法......]
- 构造函数和get/set方法的区别与联系