UIScrollView手动轮播图
2015-07-20 15:47
453 查看
没有添加定时器,所以不能达到自动的效果
#import "JZScrollViewController.h"
@interface JZScrollViewController ()
@property (nonatomic, strong) UIScrollView * scrollView;
@property (nonatomic, strong) UIPageControl * pageControl;
@end
@implementation JZScrollViewController
- (void)viewDidLoad {
[super
viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
[self
initScroll];
[self
addImageWithtag:1000];
[self
initpageControl];
}
//初始化Scroll
- (void)initScroll
{
UIScrollView
* scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0,
[UIScreen mainScreen].bounds.size.width, [UIScreen
mainScreen].bounds.size.height)];
scroll.showsHorizontalScrollIndicator = NO;
scroll.showsVerticalScrollIndicator = NO;
scroll.contentSize = CGSizeMake([UIScreen
mainScreen].bounds.size.width * 3, 0);
scroll.pagingEnabled = YES;
scroll.delegate = self;
self.scrollView = scroll;
[self.view
addSubview:self.scrollView];
}
//初始化pageControl
- (void)initpageControl
{
UIPageControl * page = [[UIPageControl alloc]
initWithFrame:CGRectMake(([UIScreen mainScreen].bounds.size.width -
200) / 2, 300 - 30, 200, 30)];
page.numberOfPages = 3;
page.currentPage = 0;
page.currentPageIndicatorTintColor = [UIColor blackColor];
page.pageIndicatorTintColor = [UIColor greenColor];
page.userInteractionEnabled = NO;
self.pageControl = page;
[self.view
addSubview:page];
}
//添加图片
- (void)addImageWithtag:(NSInteger) tag
{
for(int i =
0; i < 3; i++){
NSString * name = [NSString stringWithFormat:@"%d.jpg", i +
1];
UIImage * image = [UIImage imageNamed:name];
UIImageView * imageView = [[UIImageView alloc]
initWithFrame:CGRectMake(i * [UIScreen
mainScreen].bounds.size.width, 100, [UIScreen
mainScreen].bounds.size.width, 200)];
imageView.tag = tag + i;
imageView.image = image;
[self.scrollView addSubview:imageView];
}
}
//滚动图片达到轮播图的效果
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
CGFloat
scrollX = self.scrollView.contentOffset.x + [UIScreen
mainScreen].bounds.size.width / 2;
int num =
(int)(scrollX / [UIScreen mainScreen].bounds.size.width);
NSLog(@"%d",
num);
self.pageControl.currentPage = num;
}
@end
#import "JZScrollViewController.h"
@interface JZScrollViewController ()
@property (nonatomic, strong) UIScrollView * scrollView;
@property (nonatomic, strong) UIPageControl * pageControl;
@end
@implementation JZScrollViewController
- (void)viewDidLoad {
[super
viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
[self
initScroll];
[self
addImageWithtag:1000];
[self
initpageControl];
}
//初始化Scroll
- (void)initScroll
{
UIScrollView
* scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0,
[UIScreen mainScreen].bounds.size.width, [UIScreen
mainScreen].bounds.size.height)];
scroll.showsHorizontalScrollIndicator = NO;
scroll.showsVerticalScrollIndicator = NO;
scroll.contentSize = CGSizeMake([UIScreen
mainScreen].bounds.size.width * 3, 0);
scroll.pagingEnabled = YES;
scroll.delegate = self;
self.scrollView = scroll;
[self.view
addSubview:self.scrollView];
}
//初始化pageControl
- (void)initpageControl
{
UIPageControl * page = [[UIPageControl alloc]
initWithFrame:CGRectMake(([UIScreen mainScreen].bounds.size.width -
200) / 2, 300 - 30, 200, 30)];
page.numberOfPages = 3;
page.currentPage = 0;
page.currentPageIndicatorTintColor = [UIColor blackColor];
page.pageIndicatorTintColor = [UIColor greenColor];
page.userInteractionEnabled = NO;
self.pageControl = page;
[self.view
addSubview:page];
}
//添加图片
- (void)addImageWithtag:(NSInteger) tag
{
for(int i =
0; i < 3; i++){
NSString * name = [NSString stringWithFormat:@"%d.jpg", i +
1];
UIImage * image = [UIImage imageNamed:name];
UIImageView * imageView = [[UIImageView alloc]
initWithFrame:CGRectMake(i * [UIScreen
mainScreen].bounds.size.width, 100, [UIScreen
mainScreen].bounds.size.width, 200)];
imageView.tag = tag + i;
imageView.image = image;
[self.scrollView addSubview:imageView];
}
}
//滚动图片达到轮播图的效果
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
CGFloat
scrollX = self.scrollView.contentOffset.x + [UIScreen
mainScreen].bounds.size.width / 2;
int num =
(int)(scrollX / [UIScreen mainScreen].bounds.size.width);
NSLog(@"%d",
num);
self.pageControl.currentPage = num;
}
@end
相关文章推荐
- icvGetUsedValues的详细解析
- Qt数据库:(五)QSqlQueryModel
- MSSQL - 用GUID值来完成数据表行标识
- the difficult issue in face recognize
- KBuild MakeFile介绍
- android stuido 在线安装svn插件,添加版本库无响应
- Android UI 自动化测试
- 使用MG-SOFT的MIB Builder编写mib文件或者.my文件方法
- [数论]Number Sequence
- Gradle Plugin Samples 之 Gradle Build Variants(六)
- Gradle Plugin Samples 之 Gradle Build Configs(五)
- break、return与continue的区别.
- 如何解决"the currently displayed page contains invalid values"
- 关于一些UI的property应该使用retain、strong还是weak的问题
- Paypal SDK PayPal SDK: Request has failed with error: PAYMENT_CREATION_ERROR (422)
- TelerikUI_RadGrid_Filter 自定义方法
- BFS Codeforces Beta Round #94 (Div. 2 Only) C. Statues
- 将uiimageview设置成纯圆形
- priority_queue用法
- 动态生成juery-easyui下的折叠面板,在谷歌下格式不正确问题