你的位置:首页 > 操作系统

[操作系统]iOS学习笔记——滚动视图(scrollView)


滚动视图:在根视图中添加UIScrollViewDelegate协议,声明一些对象属性

@interface BoViewController : UIViewController<UIScrollViewDelegate> //滚动视图对象 @property (retain, nonatomic) UIScrollView *scrollView; //视图中小圆点,对应视图的页码 @property (retain, nonatomic) UIPageControl *pageControl; //动态数组对象,存储图片 @property (retain, nonatomic) NSMutableArray *images; @end /* 何问起 hovertree.com */

在程序中导入图片,在.m文件中的代码实现:

- (void)viewDidLoad {   [super viewDidLoad];   //初始化scrollView   self.scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, 320, 345)];   //初始化pageControl   self.pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(0, 344, 320, 36)];   //初始化数组,存储滚动视图的图片   self.images = [NSMutableArray arrayWithObjects:[UIImage imageNamed:@"text1.png"],[UIImage imageNamed:@"text2.png"],[UIImage imageNamed:@"text3.png"],[UIImage imageNamed:@"text4.png"], nil nil];   //把scrollView与pageControl添加到当前视图中   [self.view addSubview:self.scrollView];   [self.view addSubview:self.pageControl];   //设置视图的背景颜色   self.view.backgroundColor = [UIColor blackColor];   //调用 setuoPage方法   [self setupPage:nil];   } //改变滚动视图的方法实现 - (void)setupPage:(id)sender {   //设置委托   self.scrollView.delegate = self;   //设置背景颜色   self.scrollView.backgroundColor = [UIColor blackColor];   //设置取消触摸   self.scrollView.canCancelContentTouches = NO;   //设置滚动条类型   self.scrollView.indicatorStyle = UIScrollViewIndicatorStyleWhite;   //是否自动裁切超出部分   self.scrollView.clipsToBounds = YES;   //设置是否可以缩放   self.scrollView.scrollEnabled = YES;   //设置是否可以进行画面切换   self.scrollView.pagingEnabled = YES;   //设置在拖拽的时候是否锁定其在水平或者垂直的方向   self.scrollView.directionalLockEnabled = NO;   //隐藏滚动条设置(水平、跟垂直方向)   self.scrollView.alwaysBounceHorizontal = NO;   self.scrollView.alwaysBounceVertical = NO;   self.scrollView.showsHorizontalScrollIndicator = NO;   self.scrollView.showsVerticalScrollIndicator = NO;   //用来记录页数   NSUInteger pages = 0;   //用来记录scrollView的x坐标   int originX = 0;     for(UIImage *image in self.images)   {     //创建一个视图     UIImageView *pImageView = [[[UIImageView alloc]initWithFrame:CGRectZero]autorelease];     //设置视图的背景色     pImageView.backgroundColor = [UIColor colorWithRed:0.6 green:0.6 blue:0.6 alpha:1.0];     //设置imageView的背景图     [pImageView setImage:image];     //给imageView设置区域     CGRect rect = self.scrollView.frame;     rect.origin.x = originX;     rect.origin.y = 0;     rect.size.width = self.scrollView.frame.size.width;     rect.size.height = self.scrollView.frame.size.height;     pImageView.frame = rect;     //设置图片内容的显示模式(自适应模式)     pImageView.contentMode = UIViewContentModeScaleAspectFill;     //把视图添加到当前的滚动视图中     [self.scrollView addSubview:pImageView];     //下一张视图的x坐标:offset为:self.scrollView.frame.size.width.     originX += self.scrollView.frame.size.width;     //记录scrollView内imageView的个数     pages++;   }   //设置页码控制器的响应方法   [self.pageControl addTarget:self action:@selector(changePage:) forControlEvents:UIControlEventValueChanged];   //设置总页数   self.pageControl.numberOfPages = pages;   //默认当前页为第一页   self.pageControl.currentPage = 0;   //为页码控制器设置标签   self.pageControl.tag = 110;   //设置滚动视图的位置   [self.scrollView setContentSize:CGSizeMake(originX, self.scrollView.bounds.size.height)];   } //改变页码的方法实现 - (void)changePage:(id)sender {   NSLog(@"指示器的当前索引值为:%i",self.pageControl.currentPage);   //获取当前视图的页码   CGRect rect = self.scrollView.frame;   //设置视图的横坐标,一幅图为320*460,横坐标一次增加或减少320像素   rect.origin.x = self.pageControl.currentPage * self.scrollView.frame.size.width;   //设置视图纵坐标为0   rect.origin.y = 0;   //scrollView可视区域   [self.scrollView scrollRectToVisible:rect animated:YES];   } #pragma mark-----UIScrollViewDelegate--------- //实现协议UIScrollViewDelegate的方法,必须实现的 - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {   //获取当前视图的宽度   CGFloat pageWith = scrollView.frame.size.width;   //根据scrolView的左右滑动,对pageCotrol的当前指示器进行切换(设置currentPage)   int page = floor((scrollView.contentOffset.x - pageWith/2)/pageWith)+1;   //切换改变页码,小圆点   self.pageControl.currentPage = page; }  - (void)didReceiveMemoryWarning {   [super didReceiveMemoryWarning];   // Dispose of any resources that can be recreated. } //释放创建的对象 - (void)dealloc {   [_pageControl release];   [_scrollView release];   [super dealloc]; } /* 何问起 hovertree.com */

推荐:http://www.cnblogs.com/roucheng/p/3528371.html