可以循环滚动的展示图
2014-11-01 17:22
183 查看
ViewController.h
@interface ViewController : UIViewController<UIScrollViewDelegate>
{
NSInteger _index;
UIPageControl *_pageCtrl;
UIScrollView *_scrollerView;
}
ViewController.m
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
_scrollerView= [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
_scrollerView.delegate = self;
_scrollerView.backgroundColor = [UIColor redColor];
_scrollerView.pagingEnabled = YES;
_scrollerView.showsHorizontalScrollIndicator = NO;
_scrollerView.contentSize = CGSizeMake(320*6, 480);
[self.view addSubview:_scrollerView];
for (int i=0; i<6; i++) {
NSString *name = [NSString stringWithFormat:@"%d",i];
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:name]];
imageView.frame = CGRectMake(320*i, 0, 320, 480);
[_scrollerView addSubview:imageView];
}
_pageCtrl = [[UIPageControl alloc] initWithFrame:CGRectMake(20, 400, 280, 30)];
_pageCtrl.backgroundColor = [UIColor grayColor];
_pageCtrl.numberOfPages = 5;
[self.view addSubview:_pageCtrl];
//自动滑动
[NSTimer scheduledTimerWithTimeInterval:2
target:self
selector:@selector(timeAction:)
userInfo:nil
repeats:YES];
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
int count = scrollView.contentOffset.x/320;
//实现循环滑动
if (count == 5) {
//重点是这句话
scrollView.contentOffset = CGPointMake(0, 0);
_pageCtrl.currentPage = 0;
}else {
_pageCtrl.currentPage = count;
}
}
- (void)timeAction:(NSTimer *)time {
_index ++;
if (_index == 5) {
_index = 0;
}
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:.5];
_scrollerView.contentOffset = CGPointMake(_index*320, 0);
[UIView commitAnimations];
_pageCtrl.currentPage = _scrollerView.contentOffset.x/320;
}
@end
@interface ViewController : UIViewController<UIScrollViewDelegate>
{
NSInteger _index;
UIPageControl *_pageCtrl;
UIScrollView *_scrollerView;
}
ViewController.m
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
_scrollerView= [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
_scrollerView.delegate = self;
_scrollerView.backgroundColor = [UIColor redColor];
_scrollerView.pagingEnabled = YES;
_scrollerView.showsHorizontalScrollIndicator = NO;
_scrollerView.contentSize = CGSizeMake(320*6, 480);
[self.view addSubview:_scrollerView];
for (int i=0; i<6; i++) {
NSString *name = [NSString stringWithFormat:@"%d",i];
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:name]];
imageView.frame = CGRectMake(320*i, 0, 320, 480);
[_scrollerView addSubview:imageView];
}
_pageCtrl = [[UIPageControl alloc] initWithFrame:CGRectMake(20, 400, 280, 30)];
_pageCtrl.backgroundColor = [UIColor grayColor];
_pageCtrl.numberOfPages = 5;
[self.view addSubview:_pageCtrl];
//自动滑动
[NSTimer scheduledTimerWithTimeInterval:2
target:self
selector:@selector(timeAction:)
userInfo:nil
repeats:YES];
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
int count = scrollView.contentOffset.x/320;
//实现循环滑动
if (count == 5) {
//重点是这句话
scrollView.contentOffset = CGPointMake(0, 0);
_pageCtrl.currentPage = 0;
}else {
_pageCtrl.currentPage = count;
}
}
- (void)timeAction:(NSTimer *)time {
_index ++;
if (_index == 5) {
_index = 0;
}
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:.5];
_scrollerView.contentOffset = CGPointMake(_index*320, 0);
[UIView commitAnimations];
_pageCtrl.currentPage = _scrollerView.contentOffset.x/320;
}
@end
相关文章推荐
- 非常漂亮的图片展示效果,倒影,可以用用鼠标中键滚动,半透明的效果,也很方便改为动态的,带图
- 向上循环滚动的图片展示效果
- jQuery实现列表自动滚动循环滚动展示新闻
- 新闻客户端首页图片无限循环滚动展示(可点击触发不同事件)初步封装IOS
- jQuery实现列表自动滚动循环滚动展示新闻
- 仿淘宝首页的淘宝头条垂直滚动,因为循环滚动的是布局,所以很多向上的情况都可以用,可以先收藏起。
- ViewPager实现可以自动滚动的广告展示
- 用户可以单击左上角的左右箭头,来控制视频展示的左右滚动。 说明: 当视频展示内容处于最后一个版面时,如果再向后,则应该跳转到第一个版面。 当视频展示处于第一个版面的时候,如果再向前,就应该跳转到最后一
- OCUI界面设计:滚动视图与分页控件结合NSTimer实现图片自动循环与无限滚动展示
- jQuery单行循环滚动展示代码
- jQuery循环滚动展示代码
- 新闻客户端首页图片无限循环滚动展示(可点击触发不同事件)初步封装IOS
- jQuery循环滚动展示代码
- jQuery循环滚动展示代码[转]
- php 展示本类别产品,同时指示当前产品的位置,可以手动左右滚动
- 让jqgrid里面数据循环滚动展示
- jQuery循环滚动展示代码 可应用到文字和图片上
- JavaScript学习笔记——简单无缝循环滚动展示图片的实现
- jQuery循环滚动展示代码 可应用到文字和图片上
- 源码推荐(5.05):快速集成版本新特性介绍页面,可以横向展示文本内容的自定义cell,根据文本无限滚动