UIScrollView(滚动视图)/UIPageControl(分页控件)

来源:互联网 发布:电子教室软件价格 编辑:程序博客网 时间:2024/05/16 07:55

UIScrollView是UITableView和UITextView的超类

下面通过一个相册实例分别总结一下这两个控件的使用

我添加了3张照片在主页面显示,然后点击图片进入浏览图片,并且点击进入的图片是下一页当前的图片,然后可以循环滚动,

- (void)viewDidLoad {

    [superviewDidLoad];

    [selfcreateView];

}

- (void)createView

{

   int m = 1;

   for (int i =1; i <4; i++) {

       for (int j =1; j <2; j++) {

           self.imageView = [[UIImageViewalloc]initWithFrame:CGRectMake(0,0,20, 30)];

           // 循环便利图片

           NSString *name = [NSStringstringWithFormat:@"hh%d",m];

            m++;

           // 创建一个图像对象

           NSString *path = [[NSBundlemainBundle]pathForResource:nameofType:@"jpg"];

           UIImage *aImage = [UIImageimageWithContentsOfFile:path];

      

           UIButton *button = [UIButtonbuttonWithType:UIButtonTypeCustom];

            button.frame =CGRectMake(j * 80 +20, i * 160 -50 ,150 , 150);

            [buttonsetBackgroundImage:aImageforState:UIControlStateNormal];

            button.tag = i *4 + j;

            [buttonaddTarget:selfaction:@selector(buttonAction:)forControlEvents:UIControlEventTouchUpInside];

            [self.viewaddSubview:button];

            [self .viewaddSubview:_imageView];

            [_imageViewrelease];

        }

    }  

}

- (void)buttonAction:(id)sender

{

   SecondViewController *second = [[SecondViewControlleralloc]init];

    [selfpresentViewController:secondanimated:YEScompletion:nil];

    

    [secondrelease];

}



- (void)createView

{

   UIButton *button = [UIButtonbuttonWithType:UIButtonTypeCustom];

    button.frame =CGRectMake(0,20, 80, 20);

    [buttonsetTitle:@"返回"forState:UIControlStateNormal];

    [buttonsetTitleColor:[UIColorredColor]forState:UIControlStateNormal];

    [buttonaddTarget:selfaction:@selector(buttonAction:)forControlEvents:UIControlEventTouchUpInside];

    [self.viewaddSubview:button];

    

    

   

   self.scroll = [[UIScrollViewalloc]initWithFrame:CGRectMake(0,0,self.view.frame.size.width,self.view.frame.size.height)];

    [self.scrollsetBackgroundColor:[UIColorblackColor ]];

   // 最小放大比例

   _scroll.minimumZoomScale =0.5;

   _scroll.maximumZoomScale =2.0;

   //ContentSize:滚动范围的大小,也是镜头的大小

    [_scrollsetContentSize:CGSizeMake(_scroll.frame.size.width *5, 0)];

   // 滚动条的样式

   _scroll.indicatorStyle =UIScrollViewIndicatorStyleBlack;

   // 加载后立即横向移动

    [_scrollsetContentOffset:CGPointMake(_scroll.frame.size.width,0)];

   //滚动的内容将以页面为单位进行滚动

   _scroll.pagingEnabled =YES;

    

    [self.viewaddSubview:_scroll];

    [_scrollrelease];


   for (int i =1; i <5; i++) {

       NSString *name = [NSStringstringWithFormat:@"hh%d",i];

        

       NSString *path = [[NSBundlemainBundle]pathForResource:nameofType:@"jpg"];

       UIImage *image = [UIImageimageWithContentsOfFile:path];

       UIImageView *imageView = [[UIImageViewalloc]initWithFrame:CGRectMake(0,0,_scroll.frame.size.width ,200)];

        [imageViewsetImage:image];

        

        

       //这里为什么还要设置一个滚动视图呢,解释一下,,如果不设置,那么我们放大一张图片的时候第二张图片会跑到第一张图片上,为了解决这个问题,所以需要新建一个小的ScrollVIew

       UIScrollView *little = [[UIScrollViewalloc]initWithFrame:CGRectMake((i -1)*_scroll.frame.size.width,150,_scroll.frame.size.width,300)];

        little.minimumZoomScale =0.5;

        little.maximumZoomScale =3.0;

        little.delegate =self;

        [littleaddSubview:imageView];

        [_scrolladdSubview:little];

        [imageViewrelease];   

    }

   self.page = [[UIPageControlalloc]initWithFrame:CGRectMake(0,450,375, 20)];

   _page.numberOfPages =3;

    [self.viewaddSubview:_page];

    [_pageaddTarget:selfaction:@selector(pageAction:)forControlEvents:UIControlEventValueChanged];

    [_pagerelease];

   

}

- (void)pageAction:(id)sender

{

   // 点击事件

   self.page = (UIPageControl *)sender;

   CGFloat value =_page.currentPage *_scroll.frame.size.width;

    [_scrollsetContentOffset:CGPointMake(value,0)];

}


- (void)buttonAction:(id)sender

{

   MainViewController *main = [[MainViewControlleralloc]init];

    [selfpresentViewController:mainanimated:YEScompletion:nil];

    [main release];

}

#pragma mark scrollViewdelegate协议

//已经滚动

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

   //打印CGPoint类型的变量

   //contentOffset/ 屏幕的宽度就是当前的页

   NSLog(@"%@",NSStringFromCGPoint(scrollView.contentOffset));

}

// 将要开始拖拽

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

   NSLog(@"%s",__func__);

}

// 将要停止拖拽

- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inoutCGPoint *)targetContentOffset

{

   NSLog(@"%s",__func__);

}

// 已经停止拖拽

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{

   NSLog(@"%s",__func__);

}

// 将要开始减速

- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView{

   NSLog(@"%s",__func__);

}

//已经停止减速,彻底停止

// 还原放大的效果:有两种方法:第一种通过判断类型(isKindOfClass)

//- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{

//    if (self.scroll == scrollView) {

//        NSArray *array = [scrollView subviews];

//        for (UIScrollView *view in array) {

//            //判断类型

//            if([view isKindOfClass:[UIScrollView class]]){

//                view.zoomScale = 1.0;

//            }

//        }

//    }

//}

// 第二种方法:通过tag值

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{

   if(_scroll == scrollView){

       for(int i =0 ;i <5 ;i++ ){

           int tag = 100 + i;

           UIScrollView *sc = (UIScrollView *)[self.viewviewWithTag:tag];

            [scsetZoomScale:1.0];

        }

    }

   if(_scroll.contentOffset.x ==375 *3){

        [_scrollsetContentOffset:CGPointMake(0,0)];

    }elseif (_scroll.contentOffset.x ==0){

        [_scrollsetContentOffset:CGPointMake(375*3,0)];

    }

    

   if (_scroll.contentOffset.x/_scroll.frame.size.width == 5) {

       _page.numberOfPages =3;

    }elseif (_scroll.contentOffset.x/_scroll.frame.size.width == 0){

       _page.numberOfPages =1;

    }

   // currentPage 是当前页码,从0开始

   _page.currentPage =_scroll.contentOffset.x/_scroll.frame.size.width;  

}

#pragma mark 放大图片的协议

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

{

   // 放大第一张图片,找第一张图片,1.设置tag值,2.遍历子视图

   NSArray *array = scrollView.subviews;

   return [array objectAtIndex:0];

}

#pragma mark实现首页点击那张图片进入下一页时现显示的就是当前点击的图片并且从当前页开始滚动,但问题是怎么把mainViewController的tag值传到SecondViewController呢?

//方法

- (void)method:(NSInteger)temp

{

   self.scroll.contentOffset =CGPointMake((temp -1)*375, 0);

   self.page.currentPage = temp -1;

   

}




0 0