iOS UIScrollView 的基本用法
Posted 唐唐_010
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了iOS UIScrollView 的基本用法相关的知识,希望对你有一定的参考价值。
滚视图的用途很普遍,掌握一些基本用法是必须的,以下是一些基本的用法:
#import <UIKit/UIKit.h>
//宏定义
#define Width 300
#define Height 300
#define X 60
#define Y 100
@interface ViewController : UIViewController<UIScrollViewAccessibilityDelegate>
@property(strong,nonatomic) UIScrollView *MyScrollView;
//页码
@property(strong,nonatomic) UIPageControl *MyPageControl;
//存储图片的集合
@property(strong,nonatomic) NSMutableArray *imagesArray;
//当前页码
@property(assign,nonatomic) int CurrentPage;
@property(strong,nonatomic) UIImageView *FirstImage ;
@property(strong,nonatomic) UIImageView *SecondImage ;
@property(strong,nonatomic) UIImageView *ThirdImage ;
@end
//创建滚视图
self.MyScrollView=[[UIScrollView alloc] initWithFrame:CGRectMake(X, Y, Width,Height)];
self.MyScrollView.backgroundColor=[UIColor grayColor];
self.MyScrollView.contentSize=CGSizeMake(Width*3,0);
self.MyScrollView.delegate=self;
self.MyScrollView.pagingEnabled=YES;
self.MyScrollView.showsHorizontalScrollIndicator=NO;
[self.view addSubview:self.MyScrollView];
//初始化存储图片的集合
self.imagesArray=[NSMutableArray arrayWithCapacity:100];
for (int i=1; i<7; i++)
{
UIImage *images=[UIImage imageNamed:[NSString stringWithFormat:@"%d.png",i]];
[self.imagesArray addObject:images];
}
self.FirstImage=[[UIImageView alloc] init];
self.SecondImage=[[UIImageView alloc] init];
self.ThirdImage=[[UIImageView alloc] init];
self.CurrentPage=0;
//设置分页
self.MyPageControl=[[UIPageControl alloc] init];
CGSize page=CGSizeMake(120, 44);
self.MyPageControl.frame=CGRectMake((400-page.width)/2, 370, page.width, page.height);
self.MyPageControl.numberOfPages=6;
self.MyPageControl.currentPage=0;
//当前页码的颜色
self.MyPageControl.currentPageIndicatorTintColor=[UIColor greenColor];
//指定页码颜色
self.MyPageControl.pageIndicatorTintColor=[UIColor purpleColor];
[self.view addSubview:self.MyPageControl];
//刷新
[self reloadImage];
}
-(void)reloadImage
{
//第一种情况 显示图片在第一页 是最后一张图片
if (self.CurrentPage==0)
{
self.FirstImage.image=[self.imagesArray lastObject];
self.SecondImage.image=[self.imagesArray objectAtIndex:self.CurrentPage];
self.ThirdImage.image=[self.imagesArray objectAtIndex:self.CurrentPage+1];
}
//第二种情况 显示图片在最后一页 是前面一张图片
else if (self.CurrentPage==self.imagesArray.count-1)
{
self.FirstImage.image=[self.imagesArray objectAtIndex:self.CurrentPage-1];
self.SecondImage.image=[self.imagesArray objectAtIndex:self.CurrentPage];
self.ThirdImage.image=[self.imagesArray objectAtIndex:0];
}
//中间页 显示图片按正常顺序开始
else
{
self.FirstImage.image=[self.imagesArray objectAtIndex:self.CurrentPage-1];
self.SecondImage.image=[self.imagesArray objectAtIndex:self.CurrentPage];
self.ThirdImage.image=[self.imagesArray objectAtIndex:self.CurrentPage+1];
}
//默认是 NO
self.FirstImage.userInteractionEnabled=YES;
//图片的框架大小
self.FirstImage.frame=CGRectMake(0,0,Width,Height);
self.SecondImage.frame=CGRectMake(Width,0, Width, Height);
self.ThirdImage.frame=CGRectMake(Width*2,0, Width, Height);
//把图片添加到滚动视图上
[self.MyScrollView addSubview:self.FirstImage];
[self.MyScrollView addSubview:self.SecondImage];
[self.MyScrollView addSubview:self.ThirdImage];
//滚视图的偏移量
self.MyScrollView.contentOffset=CGPointMake(Width, 0);
}
#pragma mark delegate 代理
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
float ScroPage=self.MyScrollView.contentOffset.x;
//向右滑动
if (ScroPage> 2||ScroPage==2)
{
if(self.CurrentPage ==(int)self.imagesArray.count-1)
{
self.CurrentPage=0;
}
else
{
self.CurrentPage++;
}
}
//向左滑动
if (ScroPage<0||ScroPage==0)
{
if (self.CurrentPage==0)
{
self.CurrentPage =(int)self.imagesArray.count-1;
}
else
{
self.CurrentPage--;
}
}
self.MyPageControl.currentPage=self.CurrentPage;
//刷新
[self reloadImage];
以上是关于iOS UIScrollView 的基本用法的主要内容,如果未能解决你的问题,请参考以下文章