iOS - 关于轮播图的实现实例
来源:互联网 发布:nginx 经典书籍 编辑:程序博客网 时间:2024/06/06 06:54
在此,因为只有一个页面,所以没有管理定时器。如果在实际开发项目中,则需要把定时器在合适的位置 kill 掉。本博客只作为一个参考的小 Demo 。。。代码如下:
// 屏幕尺寸
#define WIDTH [UIScreen mainScreen].bounds.size.width
#define HEIGHT [UIScreen mainScreen].bounds.size.height
// 照片的数量
#define PHOTOCOUNT 4
// 轮播时间的间隔
#define MOVETIME 2.0
#import "ViewController.h"
@interface ViewController () <UIScrollViewDelegate> {
UIScrollView *_scroll;
UIPageControl *_page;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[superviewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIScrollView *scrollView = [[UIScrollViewalloc] init];
scrollView.frame =self.view.bounds;
scrollView.backgroundColor = [UIColorblackColor];
scrollView.delegate =self;
scrollView.pagingEnabled =YES;
scrollView.bounces =NO;
[self.viewaddSubview:scrollView];
_scroll = scrollView;
UIPageControl *page = [[UIPageControlalloc] init];
page.frame =CGRectMake((WIDTH-100)/2,HEIGHT-50,100, 20);
page.backgroundColor = [UIColorblackColor];
page.numberOfPages =PHOTOCOUNT;
page.currentPageIndicatorTintColor = [UIColorwhiteColor];
page.pageIndicatorTintColor = [UIColorredColor];
[self.viewaddSubview:page];
_page = page;
for (NSInteger i =0; i<PHOTOCOUNT+1; i++) {
UIImageView *imageView = [[UIImageViewalloc] init];
imageView.frame =CGRectMake(WIDTH*i,0, WIDTH,HEIGHT);
imageView.contentMode =UIViewContentModeScaleAspectFit;
NSString *name = [NSStringstringWithFormat:@"%ld.jpg",i];
if (PHOTOCOUNT == i) {
name = @"0.jpg";
}
imageView.image = [UIImageimageNamed:name];
[scrollView addSubview:imageView];
}
scrollView.contentSize =CGSizeMake(WIDTH*(PHOTOCOUNT+1),0);
NSTimer *timer = [NSTimertimerWithTimeInterval:MOVETIMEtarget:selfselector:@selector(movePhoto)userInfo:nilrepeats:YES];
[[NSRunLoopcurrentRunLoop] addTimer:timerforMode:NSRunLoopCommonModes];
}
- (void)movePhoto {
NSInteger contentOffSet_x =_scroll.contentOffset.x;
if (contentOffSet_x >=PHOTOCOUNT*WIDTH) {
[_scrollsetContentOffset:CGPointMake(0,0) animated:NO];
_page.currentPage =0;
}else {
[_scrollsetContentOffset:CGPointMake(contentOffSet_x+WIDTH,0) animated:YES];
_page.currentPage =_scroll.contentOffset.x/WIDTH + 1;
}
if (contentOffSet_x == (PHOTOCOUNT-1)*WIDTH) {
_page.currentPage =0;
}
}
- iOS - 关于轮播图的实现实例
- iOS关于通讯录的实现
- [贪心] 关于贪心的实例的实现
- iOS关于悬浮窗口的实现
- 关于ios播放音频的简单实现
- iOS关于系统原生通知的实现
- iOS - 关于蒙版渐变的实现
- iOS 实现简单的移动UIView代码实例
- iOS访问相册及拍照保存的实现实例
- iOS之 实现单例的步骤和具体实例:
- iOS的动态创建实例方法和实现消息转发
- 关于IOS登录实现
- 关于DataList使用DropDownList的分页实现 技巧实例源码
- 实例8:实现应用对话框主题的关于Activity
- 关于AXIS1.4实现webservice接口部署的基础实例
- IOS视图交换实现实例
- IOS视图交换实现实例
- iOS轮播图的实现
- echarts 颜色设置
- leetcode412 笔记
- 删数问题
- wdcp常见组件安装方法
- 第十四周--平衡二叉树
- iOS - 关于轮播图的实现实例
- NavigationView的简单使用
- HBASE 结构详解(转载)
- nmap端口扫描
- 关于重载流插入运算符和流提取运算符的问题
- iOS打印功能详解 UIPrintInteractionController
- Android绘图机制(三)——自定义View的实现方式以及半弧圆新控件
- 第十五周项目1(1)--验证哈希表相关算法
- Warning:mysql_num_rows() expects parameter 1 to be resource,boolean given in F:.....\db_func.php onl