滚动视图(UIScrollView)

来源:互联网 发布:java免费商城系统源码 编辑:程序博客网 时间:2024/04/28 16:47

NSString *path = [[NSBundle mainBundle]pathForResource:@“003” ofType:@”png”];

UIImage *bgImage = [UIImage imageWithContentsOfFile:path];//用这种方式加载图片不耗内存

1、查看大图片

UIScrollView *scrollView = [[UIScrollView alloc]initWith:self.view.frame];

scrollView.backgroundColor = [UIColor redColor];

设置滚动视图的contentSize 如果contentSize小于滚动视图的边界(bounds)就不会滚动


设置滚动视图的contentSize跟图片尺寸一样


scrollView.contentSize = bgImage.size;

设置滚动视图缩小的最小倍数

srcollView.minmumZoomScale = 0.1;

设置滚动视图放大的最大倍数

srcollView.maxmumZoomScale = 100;

//使用视图的代理方法 需要挂上代理

srcollView.delegate = self;

[self.view addSubview:srcollView];


//在滚动视图上添加图片(在滚动视图上添加内容)

imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0,0,bgImage.size.width,bgImage.size.height)];

imageView.image = bgImage;

[scrollView addSubview:imageView];

#pragma mark—--———代理方法—————————————-

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{

    

   return imageView;

}





2、内容过多 需要一个页面显示(如:注册 修改个人信息)

UIScrollView *myScrollView = [[UIScrollView alloc]initWithFrame:self.view.frame];

    

    myScrollView.contentSize = CGSizeMake(0, CGRectGetHeight(self.view.frame)+500);

    

    myScrollView.backgroundColor = [UIColor grayColor];

//    屏蔽竖向滚动条

    myScrollView.showsVerticalScrollIndicator = NO;

//    屏蔽横向滚动条

    myScrollView.showsHorizontalScrollIndicator = NO;

    myScrollView.delegate = self;

    

//设置滚动视图 是否允许点击顶部  滚动到初始位置

    myScrollView.scrollsToTop = YES;

    [self.view addSubview:myScrollView];

    

    accTextField = [[UITextField alloc]initWithFrame:CGRectMake(80, 200, 200, 35)];

    

    accTextField.borderStyle = UITextBorderStyleLine;

    

    [myScrollView addSubview:accTextField];

//已经开始滚动的时候调用

//- (void)scrollViewDidScroll:(UIScrollView *)scrollView

//{

//    [accTextField resignFirstResponder];

//}

//将要开始拖拽的时候调用(手指触摸屏幕的时候)

//- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView

//

//{

//     [accTextField resignFirstResponder];

//}

//滚动到屏幕的最顶部时调用

//- (void)scrollViewDidScrollToTop:(UIScrollView *)scrollView

//{

//    [accTextField resignFirstResponder];

//}




3、分页查看图片

一、首先准备图片数组

 imageList = @[@"0.png",@"000.png",@"001.png",@"002.png"];

UIScrollView *myScrollView = [[UIScrollView alloc]initWithFrame:self.view.frame];

//分屏的页数(由数组的图片决定)

myScrollView.contentSize = CGSizeMake(CGRectGetWidth(self.view.frame)*imageList.count,0);

//挂上代理

myScrollView.delegate = self;

//设置背景颜色

mySrcollView.backgroundColor = [UIColor redColor];

//设置滚动视图的分页效果

myScrollView.pagingEnabled = YES;

//设置滚动样式

myScrollView.indicatorStyle = UIScrollViewIndicatorStyleBlack;

//设置滚动视图的偏移量 可以达到设置滚动视图默认在第几屏 的位置

//还可以通过contentOffset 来判断滚动到第几屏
 myScrollView.contentOffset = CGPointMake(CGRectGetWidth(self.view.frame),0);

//    设置是否有反弹效果(默认值是YES 允许看到底图 并有反弹效果)

    myScrollView.bounces = YES;

    //隐藏横向滚动条myScrollView.showsHorizontalScrollIndicator = NO;

    [self.view addSubview:myScrollView];


 UIPageControl (点点点)

UIPageControl *pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(0, CGRectGetHeight(self.view.frame)-40, CGRectGetWidth(self.view.frame), 20)];


//    设置pageControl总共有多少页面

    pageControl.numberOfPages = imageList.count;

//    设置指示的当前页面

    pageControl.currentPage = 1;

    

    pageControl.hidesForSinglePage = 1;//当只有一个页面的时候  隐藏pageContentrol

    pageControl.tag = 119;

//    设置空心小圆点的颜色

    pageControl.pageIndicatorTintColor = [UIColor redColor];

//    设置当前小圆点的颜色

    pageControl.currentPageIndicatorTintColor = [UIColor blueColor];

    [self.view addSubview:pageControl];


//滚动视图已经降速的时候调用

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView

{

//   得到一个当前页数

//    滚动视图的偏移量(持有的数据) 屏幕的宽也是我们持有的数据

//    滚动视图x的偏移量

    CGFloat x = scrollView.contentOffset.x;

//   屏幕的宽

    CGFloat w = CGRectGetWidth(self.view.frame);

    

    NSInteger curPage =(NSInteger) (x/w);

    UIPageControl *pageControl = (UIPageControl *)[self.view viewWithTag:119];

    pageControl.currentPage = curPage;


}


0 0