用UIScrollView,UIPageControl来实现滚动视图。
2016-03-12 22:50
519 查看
@interface ViewController : UIViewController<UIScrollViewDelegate>
@property (nonatomic ,strong) NSMutableArray *array;
@property (nonatomic ,strong) UIImageView *imageView1;
@property (nonatomic ,strong) UIImageView *imgeView2;
@property (nonatomic ,strong) UIImageView *imgeView3;
@property (nonatomic ,strong) UIScrollView *myscroll;
@property (nonatomic ,strong) UIPageControl *mypage;
@property (nonatomic ,assign) int currentPage;
@end
#import "ViewController.h"
#define HEIGHT 200
#define WIDTH 300
//#define WIDTH1
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"1.jpg"]];
self.array = [[NSMutableArray alloc ] init];
for (int i = 0; i<6; i++) {
UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"%d.jpg",i]];
[self.array addObject:image];
}
self.myscroll = [[UIScrollView alloc] initWithFrame:CGRectMake(WIDTH/4 , HEIGHT/3, WIDTH, HEIGHT)];
self.myscroll.backgroundColor=[UIColor grayColor];
self.myscroll.contentSize = CGSizeMake(WIDTH*3, 0);
[self.view addSubview:self.myscroll];
self.myscroll.delegate = self;
//隐藏滚动条
self.myscroll.showsHorizontalScrollIndicator = NO;
//设置分页
self.myscroll.pagingEnabled = YES;
self.mypage = [[UIPageControl alloc] initWithFrame:CGRectMake(WIDTH-140, HEIGHT+40, 120, 30)];
self.mypage.backgroundColor = [UIColor clearColor];
//页数可以写成数组的个数
self.mypage.numberOfPages = self.array.count;
self.mypage.currentPage = 0;
self.mypage.currentPageIndicatorTintColor = [UIColor greenColor];
self.mypage.pageIndicatorTintColor = [UIColor blueColor];
[self.view addSubview:self.mypage];
self.imageView1 = [[UIImageView alloc] init];
self.imgeView2 = [[UIImageView alloc] init];
self.imgeView3 = [[UIImageView alloc] init];
self.currentPage = 0;
[self reloadImage];
}
-(void)reloadImage{
//第一种情况,当前页在第一张图片的时候
if (self.currentPage == 0)
{
self.imageView1.image = [self.array lastObject];
self.imgeView2.image = [self.array objectAtIndex:self.currentPage];
self.imgeView3.image = [self.array objectAtIndex:self.currentPage+1];
}
//当前页在最后一张图片的时候
else if (self.currentPage == self.array.count-1)
{ self.imageView1.image = [self.array objectAtIndex:self.currentPage-1 ];
self.imgeView2.image = [self.array objectAtIndex:self.currentPage];
self.imgeView3.image = [self.array firstObject];
}
else
{ self.imageView1.image = [self.array objectAtIndex:self.currentPage-1];
self.imgeView2.image = [self.array objectAtIndex:self.currentPage];
self.imgeView3.image = [self.array objectAtIndex:self.currentPage+1];
}
self.imageView1.frame = CGRectMake(0, 0, WIDTH, HEIGHT);
self.imgeView2.frame = CGRectMake(WIDTH, 0, WIDTH, HEIGHT);
self.imgeView3.frame = CGRectMake(WIDTH*2, 0, WIDTH, HEIGHT);
[self.myscroll addSubview:self.imageView1];
[self.myscroll addSubview:self.imgeView2];
[self.myscroll addSubview:self.imgeView3];
}
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
float x = self.myscroll.contentOffset.x;
//向左滑动
if (x<=0)
{ if(self.currentPage == 0)
{
self.currentPage = (int)self.array.count - 1;
}
else
{
self.currentPage--;
}
}
//向右滑动
if(x>=WIDTH*2)
{ if(self.currentPage ==self.array.count-1)
{
self.currentPage = 0;
}
else
{
self.currentPage++;
}
}
self.mypage.currentPage = self.currentPage;
[self reloadImage];
}
@property (nonatomic ,strong) NSMutableArray *array;
@property (nonatomic ,strong) UIImageView *imageView1;
@property (nonatomic ,strong) UIImageView *imgeView2;
@property (nonatomic ,strong) UIImageView *imgeView3;
@property (nonatomic ,strong) UIScrollView *myscroll;
@property (nonatomic ,strong) UIPageControl *mypage;
@property (nonatomic ,assign) int currentPage;
@end
#import "ViewController.h"
#define HEIGHT 200
#define WIDTH 300
//#define WIDTH1
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"1.jpg"]];
self.array = [[NSMutableArray alloc ] init];
for (int i = 0; i<6; i++) {
UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"%d.jpg",i]];
[self.array addObject:image];
}
self.myscroll = [[UIScrollView alloc] initWithFrame:CGRectMake(WIDTH/4 , HEIGHT/3, WIDTH, HEIGHT)];
self.myscroll.backgroundColor=[UIColor grayColor];
self.myscroll.contentSize = CGSizeMake(WIDTH*3, 0);
[self.view addSubview:self.myscroll];
self.myscroll.delegate = self;
//隐藏滚动条
self.myscroll.showsHorizontalScrollIndicator = NO;
//设置分页
self.myscroll.pagingEnabled = YES;
self.mypage = [[UIPageControl alloc] initWithFrame:CGRectMake(WIDTH-140, HEIGHT+40, 120, 30)];
self.mypage.backgroundColor = [UIColor clearColor];
//页数可以写成数组的个数
self.mypage.numberOfPages = self.array.count;
self.mypage.currentPage = 0;
self.mypage.currentPageIndicatorTintColor = [UIColor greenColor];
self.mypage.pageIndicatorTintColor = [UIColor blueColor];
[self.view addSubview:self.mypage];
self.imageView1 = [[UIImageView alloc] init];
self.imgeView2 = [[UIImageView alloc] init];
self.imgeView3 = [[UIImageView alloc] init];
self.currentPage = 0;
[self reloadImage];
}
-(void)reloadImage{
//第一种情况,当前页在第一张图片的时候
if (self.currentPage == 0)
{
self.imageView1.image = [self.array lastObject];
self.imgeView2.image = [self.array objectAtIndex:self.currentPage];
self.imgeView3.image = [self.array objectAtIndex:self.currentPage+1];
}
//当前页在最后一张图片的时候
else if (self.currentPage == self.array.count-1)
{ self.imageView1.image = [self.array objectAtIndex:self.currentPage-1 ];
self.imgeView2.image = [self.array objectAtIndex:self.currentPage];
self.imgeView3.image = [self.array firstObject];
}
else
{ self.imageView1.image = [self.array objectAtIndex:self.currentPage-1];
self.imgeView2.image = [self.array objectAtIndex:self.currentPage];
self.imgeView3.image = [self.array objectAtIndex:self.currentPage+1];
}
self.imageView1.frame = CGRectMake(0, 0, WIDTH, HEIGHT);
self.imgeView2.frame = CGRectMake(WIDTH, 0, WIDTH, HEIGHT);
self.imgeView3.frame = CGRectMake(WIDTH*2, 0, WIDTH, HEIGHT);
[self.myscroll addSubview:self.imageView1];
[self.myscroll addSubview:self.imgeView2];
[self.myscroll addSubview:self.imgeView3];
}
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
float x = self.myscroll.contentOffset.x;
//向左滑动
if (x<=0)
{ if(self.currentPage == 0)
{
self.currentPage = (int)self.array.count - 1;
}
else
{
self.currentPage--;
}
}
//向右滑动
if(x>=WIDTH*2)
{ if(self.currentPage ==self.array.count-1)
{
self.currentPage = 0;
}
else
{
self.currentPage++;
}
}
self.mypage.currentPage = self.currentPage;
[self reloadImage];
}
相关文章推荐
- iOS完美版的UIScrollView无缝循环:你值得一看
- 从sqlcommandbuilder引出的小想法
- 用UIpickView实现省市的联动
- IOS_压缩图片UIImage
- 用Source Insight追踪Arduino的setup和loop
- 1562 Guess the number
- 625A.Guest From the Past
- 62. 63. Unique Paths 64. Minimum Path Sum
- 设计模式-Builder模式
- JavaSE入门学习29:Java常用类之StringBuffer类和StringBuilder类
- UIImage
- Java 1.6 AbstractQueuedSynchronizer源码解析
- UIScrollViewDelegate
- 626A.Robot Sequence
- UICollectionView
- AJAX async:true(异步)或 false(同步)。
- iOS-UIScrollView和UIPageControl的综合实力,滚动图,轮播图
- java selenium (九) 常见web UI 元素操作 及API使用
- iOS— UIScrollView和 UIPageControl之间的那些事
- iOS 常用UI 部分常用 方法