UI_滚动视图

#import "AppDelegate.h"

#import "RootViewController.h"

#import "TwoViewController.h"

#import "ThreeViewController.h"

#import "FourController.h"

@interface AppDelegate ()


@end


@implementation AppDelegate



- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

  

    //  RootViewController *rootVC = [[RootViewController alloc]init];

    //  self.window.rootViewController = rootVC;

    

     //TwoViewController *twoVC =[[TwoViewController alloc]init];

    //self.window.rootViewController = twoVC;

    

    //ThreeViewController *threeVC = [[ThreeViewController alloc]init];

    //self.window.rootViewController = threeVC;

    

    FourController *fourVC = [[FourController alloc]init];

    self.window.rootViewController = fourVC;

    

    

    

    self.window.backgroundColor = [UIColor blackColor];

    [self.window makeKeyAndVisible];

    return YES;

}

@end







#import "RootViewController.h"


@interface RootViewController ()<UIScrollViewDelegate>


@end


@implementation RootViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    //滚动视图的学习

        //作用就是:当内容超过一个屏幕时,我们为了能将视图呈现给客户,所以需要滚动视图

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

        //设置滚动视图的滚动区域

    myScrollView.contentSize = CGSizeMake(CGRectGetWidth(self.view.frame)*3,CGRectGetHeight(self.view.frame));

    

    [self.view addSubview:myScrollView];

    

        //为滚动视图添加子视图

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

        UILabel *myLabel = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetWidth(self.view.frame)*i, 0,CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame))];

        myLabel.text = [NSString stringWithFormat:@"我是第%d个视图",i];

        myLabel.font = [UIFont systemFontOfSize:80];

        //让字体根据labelwitdh来适应大小

        myLabel.adjustsFontSizeToFitWidth = YES;

        myLabel.backgroundColor =[UIColor colorWithRed:arc4random()%256/255.0 green:arc4random()%256/255.0 blue:arc4random()%256/255.0 alpha:1.0];

        

        [myScrollView addSubview:myLabel];

    }

    

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

    myScrollView.pagingEnabled = YES;

        //隐藏水平滚动条(水平方向)

    myScrollView.showsHorizontalScrollIndicator = NO;

//        //隐藏垂直方向滚动条

//    myScrollView.showsVerticalScrollIndicator = NO;

        //为滚动视图添加一个tag

    myScrollView.tag = 1000;

    

        //设置反弹效果    (反方向滑动会出现白色的未设置页面)

    myScrollView.bounces = NO;  //关闭就ok

    

        //*设置滚动视图的代理

    myScrollView.delegate = self;

    

    

    

    

    

        //pageControl  页面的小白点。

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

        //设置小白点的个数(滚动视图的子视图个数)

    pageControl.numberOfPages = 3;

        //设置当前选中的小白点(当前所在的子视图的位置)

    pageControl.currentPage = 2;

        //添加回调方法

    pageControl.tag = 1001;

    [pageControl addTarget:self action:@selector(changepage:) forControlEvents:UIControlEventValueChanged];

    

        //添加小白点到视图上

    [self.view addSubview:pageControl];

/*

  //按钮分享

    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];

    [button setTitle:@"警示框按钮" forState:UIControlStateNormal];

    [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];

    [button setFrame:CGRectMake(100, 100, 100, 100)];

    [self.view addSubview:button];

   //

*/

    

   

    

    

}

//小白点的实现方法:

-(void)changepage:(UIPageControl *)sender{

        //得到当前所在的小白点(当前所在的页数)

    int index = (int)sender.currentPage;

        //得到滚动视图

    UIScrollView *myScrollView = (UIScrollView *)[self.view viewWithTag:1000];

        //要切换scrollView上面的子视图,是通过scrollView的一个属性来切换,该属性表示scrollView的偏移量

        //偏移量contentoffset就是 滚动视图的bounds original (x,y)

    myScrollView.contentOffset = CGPointMake(index *CGRectGetWidth(self.view.frame), 0);

    

}



#pragma mark-滚动视图的代理方法

//开始拖拽视图    (只有"触摸对象"滑动视图才会触发此代理方法)(加着NStimer的广告不会触发此代理方法,只有手动才可以)

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


    NSLog(@"开始拖拽");


}


//停止拖拽  (手指离开,视图并没有停止移动,将要减速)

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


    NSLog(@"停止拖拽");


}


//视图正在滚动    ()

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


    NSLog(@"视图正在滚动");

    

}


//视图停止滚动

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

    

    //根据偏移量来计算当前是在哪个界面,也就是pageControl(小白点)currentpage(当前小白点所在的位置)的值

        //得到当前滚动视图的偏移量

    CGPoint offset = scrollView.contentOffset;

        //小白点的视图

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

        //水平方向的计算

  pageControl.currentPage =offset.x/CGRectGetWidth(self.view.frame);

    

    

    

    NSLog(@"视图停止滚动");

}



/*

//UIAlertControllerStyleAlert          // 从中间弹出来警示框

//UIAlertControllerStyleActionSheet    // 从底部弹出来警示框

-(void)buttonAction:(UIButton *)sender{

    //初始化一个警示框

    UIAlertController *alertController =[UIAlertController alertControllerWithTitle:@"保存" message:@"删除" preferredStyle:UIAlertControllerStyleActionSheet];

    //用模态方法显示

    [self presentViewController:alertController animated:YES completion:nil];

    //初始化一个按钮

    UIAlertAction *action = [UIAlertAction actionWithTitle:@"哈哈" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {

        NSLog(@"按钮真心很复杂");

    }];

    //添加按钮

    [alertController addAction:action];

}


*/


//------------------------------------------------------------------------------------

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


/*

#pragma mark - Navigation


// In a storyboard-based application, you will often want to do a little preparation before navigation

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    // Get the new view controller using [segue destinationViewController].

    // Pass the selected object to the new view controller.

}

*/


@end





#import "TwoViewController.h"


@interface TwoViewController ()<UIScrollViewDelegate>


@property(nonatomic,retain)NSTimer *Timer;      //让视图自动播放


@end


@implementation TwoViewController


- (void)viewDidLoad {

    [super viewDidLoad];

        //初始化一个滚动

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

        //设置滚动范围

    scrollView.contentSize = CGSizeMake(CGRectGetWidth(self.view.frame)*3, CGRectGetHeight(self.view.frame));

        //设置分页效果

    scrollView.pagingEnabled = YES;

        //将滚动条隐藏掉--(水平方向的)

    scrollView.showsHorizontalScrollIndicator = NO;

        //添加3个子视图View--UILabel类型

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

        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetWidth(self.view.frame)*i, 0, CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame))];

        label.text =[NSString stringWithFormat:@"我是第%d个子视图",i];

        label.font = [UIFont systemFontOfSize:80];

            //字体是否适应标签的大小

        label.adjustsFontSizeToFitWidth =YES;

        label.backgroundColor =[UIColor colorWithRed:arc4random()%256/255.0 green:arc4random()%256/255.0 blue:arc4random()%256/255.0 alpha:1.0];

        ;

        [scrollView addSubview:label];

    }

    [self.view addSubview:scrollView];

    scrollView.tag = 1000;

    

    //启动定时器

    self.Timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(changeView) userInfo:nil repeats:YES];

    

    //为滚动视图指定代理

    scrollView.delegate = self;

    

    

    

    //为视图添加页面小白点

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

        //小白点数量

    pageControl.numberOfPages =3;

    pageControl.currentPage = 0;

    [self.view addSubview:pageControl];

    pageControl.tag = 1001;

    [pageControl addTarget:self action:@selector(pageControl:) forControlEvents:UIControlEventValueChanged];

        

    

}

#pragma mark - 滚动试图的代理方法

//开始拖拽的代理方法,在此方法中暂停计时器

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

    NSLog(@"正在拖拽视图,所以要将自动播放暂停");

    

    //setFireDate: 设置定时器在什么时间启动

    //[NSDate distantFuture]: 将来的某一时刻

    [self.Timer setFireDate:[NSDate distantFuture]]; //暂停定时器

    

}


//视图静止时(没有人在拖拽),开启定时器,让自动轮播

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

    

    //视图静止之后,过一段时间(1.5s)在开启定时器

        //[NSDate dateWithTimeInterval:1.5 sinceDate:[NSDate date]]返回值为从现在开始,再过1.5s的时刻

    NSLog(@"开启定时器");

    [self.Timer setFireDate:[NSDate dateWithTimeInterval:1.5 sinceDate:[NSDate date]]];

    

    //根据偏移量来计算当前是在哪个界面,也就是pageControl(小白点)currentpage(当前小白点所在的位置)的值

    //得到当前滚动视图的偏移量

    

    

    

    

    

}


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


CGPoint offset = scrollView.contentOffset;

    //小白点的视图

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

    //水平方向的计算

    pageControl.currentPage =offset.x/CGRectGetWidth(self.view.frame);


}




//定时器的回调方法  用途:切换界面

-(void)changeView{

        //得到滚动视图scrollView

    UIScrollView *scrollView = (UIScrollView *)[self.view viewWithTag:1000];

        //通过contentOffset 来切换滚动视图的子界面

    float offset_X = scrollView.contentOffset.x;

    

    //每次切换一个屏幕

    offset_X +=CGRectGetWidth(self.view.frame);

    

            //判断是否是最后一个子视图

    if (offset_X > 2*CGRectGetWidth(self.view.frame)) {

        offset_X = 0;

    }

    

        //得到最终的偏移量

    CGPoint resultPoint = CGPointMake(offset_X, 0);

        //切换视图的时候带动画效果

            //从最后一张回到第一张的时候,不带动画效果

    if (offset_X ==0) {

        [scrollView setContentOffset:resultPoint animated:NO];

    }else{

    [scrollView setContentOffset:resultPoint animated:YES];

    }

}






-(void)pageControl:(UIPageControl *)sender{

    //得到现在小白点的位置

    int i =(int)sender.currentPage;

    //得到滚动视图

    UIScrollView *scrollView = (UIScrollView *)[self.view viewWithTag:1000];

    //要切换scrollView滚动视图上面的子视图,是通过scrollView滚动视图的一个属性来切换,该属性表示scrollView滚动视图的偏移量

    //偏移量contentoffset就是 滚动视图的大小bounds 初始点original (x,y)

    scrollView.contentOffset = CGPointMake(i *CGRectGetWidth(self.view.frame), 0);


    

    

    NSLog(@"请叫我小白点");

}







- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


/*

#pragma mark - Navigation


// In a storyboard-based application, you will often want to do a little preparation before navigation

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    // Get the new view controller using [segue destinationViewController].

    // Pass the selected object to the new view controller.

}

*/


@end





#import "ThreeViewController.h"


@interface ThreeViewController ()<UIScrollViewDelegate>


@end


@implementation ThreeViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    

        //新建一个scrollView滚动视图

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

    

        //初始化一张图片

    UIImage *myImage = [UIImage imageNamed:@"123.jpg"];

        //规定好图片宽度,根据图片宽度得到等比例的高度

  //  float imageWidth = 200;

        //得到图片的原比例宽高

  //  float imageHeight = myImage.size.height *(200/myImage.size.width);

    

        //initWithImage: 得到一个和图片宽高一样的imageView

    UIImageView *myImageView = [[UIImageView alloc]initWithImage:myImage];

        //根据我们规定的宽高重新设置相框的frame

//    myImageView.frame = CGRectMake(0, 0, imageWidth, imageHeight);

        myImageView.frame = CGRectMake(0, 0,self.view.frame.size.width, self.view.frame.size.height);

            //适应屏幕  Fit(按比例适应屏幕) Fill(平铺到屏幕,可能会导致图片变形)

        myImageView.contentMode = UIViewContentModeScaleAspectFit;

        //显示在屏幕中央

    myImageView.center = self.view.center;

    

        //设置scrollView的缩放范围

    myScrollView.minimumZoomScale = 0.5;

    myScrollView.maximumZoomScale = 10;

    

        //由于缩放需要代理方法,需要通过滚动视图的代理方法实现,所以需要指定代理

    myScrollView.delegate = self;

    

        //呈现视图

    [self.view addSubview:myScrollView];

    [myScrollView addSubview:myImageView];

        //myImageViewtag

    myImageView.tag = 1000;

    

    myScrollView.backgroundColor = [UIColor orangeColor];

}


#pragma mark -与缩放有关的代理方法

        //得到scrollView上要缩放的子视图

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

        //得到myImage

    UIImageView *imageView = (UIImageView *)[scrollView viewWithTag:1000];

    return imageView;

}



//将要开始缩放的时候,视图执行的代理方法

-(void)scrollViewWillBeginZooming:(UIScrollView *)scrollView withView:(UIView *)view{


    NSLog(@"%@",view);

}


//正在缩放的时候,视图执行的代理方法


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

    

//放大图片

        //在缩放过程中,这里我们分为两种情况分析问题,一种情况是视图未超出屏幕;第二种情况是视图超出屏幕,没有超出屏幕的时候,说明视图还在scrollViewframe矩形中,我们将视图的中心点重写和scrollView的重合即可。如果超出屏幕,我们就需要结合scrollViewcontentSizescrollView的可滚动范围)来计算视图位置。

        //得到正在缩放的视图(imageView) 根据视图的大小,来计算视图的范围是否超出屏幕

    UIImageView *myImageView = (UIImageView *)[scrollView viewWithTag:1000];

        //得到视图的宽度

    float imageViewWidth = myImageView.frame.size.width;

        //得到视图的高度

    float imageViewHeight = myImageView.frame.size.height;

        //判断是否超出屏幕,要根据所有情况重新计算出来的位置,更改imageView的中心点,让他重新居中

    float centerX = 0;

    float centerY = 0;

            //宽度

    if (imageViewWidth > scrollView.frame.size.width) {

        centerX = scrollView.contentSize.width/2;       //视图已经超出屏幕

    }else{

        centerX = scrollView.center.x;        //视图还在屏幕之内

    }

            //高度

    if (imageViewHeight > scrollView.bounds.size.height) {

        centerY = (scrollView.contentSize.height)/2;

    }else{

        centerY = scrollView.center.y;

    }

        //重新计算视图的中心点

    myImageView.center =CGPointMake(centerX, centerY);

    

    

    

    NSLog(@"正在缩放视图%s",__FUNCTION__);

}

/*

//缩放结束 的代理方法

    //view:正在缩放的shitu

    //scale:缩放结束后的缩放倍数

-(void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(CGFloat)scale{

        //方法图片,松手以后图片返回原来的大小。

    view.transform = CGAffineTransformMakeScale(1.0, 1.0);

    scrollView.contentSize = view.frame.size;

    view.center = self.view.center;

 

    

    NSLog(@"缩放结束%s",__FUNCTION__);

}


*/








- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


/*

#pragma mark - Navigation


// In a storyboard-based application, you will often want to do a little preparation before navigation

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    // Get the new view controller using [segue destinationViewController].

    // Pass the selected object to the new view controller.

}

*/


@end





#import "FourController.h"


@interface FourController ()<UIScrollViewDelegate>


@end


@implementation FourController


- (void)viewDidLoad {

    [super viewDidLoad];

        //初始化最大的滚动视图,只要负责切换各个图片

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

    myBigScrollView.tag = 2000;

    myBigScrollView.delegate = self;

        //初始化一个数组,用来存放图片名称

    NSArray *array = [NSArray arrayWithObjects:@"1.png",@"2.png",@"3.png",@"4.png",@"5.jpg", nil];

    

        //设置myBigScrollView的滚动范围

    myBigScrollView.contentSize = CGSizeMake(CGRectGetWidth(self.view.frame)*5, CGRectGetHeight(self.view.frame));

        //bigScrollView添加子视图

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

        //创建一个小的scrollView,该滚动视图主要负责图片的缩放

        UIScrollView *oneScroll = [[UIScrollView alloc]initWithFrame:CGRectMake(CGRectGetWidth(self.view.frame)*i, 0, CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame))];

        //设置缩放范围

        oneScroll.minimumZoomScale = 0.5;

        oneScroll.maximumZoomScale = 3.0;

        //指定代理

        oneScroll.delegate = self;

        //初始化一个imageView,将imageView添加到scrollView用来显示缩放

        UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 300, 300)];

        //显示在视图中央

        imageView.center = self.view.center;

        //imageView上添加图片

        imageView.image = [UIImage imageNamed:[array objectAtIndex:i]];

        

        

                //设置图片为圆形

            imageView.layer.cornerRadius = 150;

                //自适应图片宽高

            imageView.layer.masksToBounds = YES;

                //设置圆圈的边框颜色

            imageView.layer.borderColor = [UIColor redColor].CGColor;

                //设置圆圈的边框宽度

            imageView.layer.borderWidth = 5;

        

        

        

        //imageView 添加到oneScrollView上面

        [oneScroll addSubview:imageView];

        //oneScrollView添加到myBigScrollView上面

        [myBigScrollView addSubview:oneScroll];

                //Tag

        imageView.tag = 1000+i;

        oneScroll.tag = 3000+i;

    }

        //BIgScrollView添加到视图上面

    [self.view addSubview:myBigScrollView];

        //设置myBigScrollView的分页效果

    myBigScrollView.pagingEnabled = YES;

    

    

    

            //小白点

                //pageControl  页面的小白点。

    UIPageControl *mypageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(30, CGRectGetHeight(self.view.frame)-50, CGRectGetWidth(self.view.frame)-50, 50)];

                //设置小白点的个数(滚动视图的子视图个数)

    mypageControl.numberOfPages = 5;

                //设置当前选中的小白点(当前所在的子视图的位置)

    mypageControl.currentPage = 0;

                //添加回调方法

    mypageControl.tag = 1111;

    [mypageControl addTarget:self action:@selector(page:) forControlEvents:UIControlEventEditingChanged];

                //添加小白点到视图上

    [self.view addSubview:mypageControl];

    

    

    

    //启动定时器

    self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(changeView) userInfo:nil repeats:YES];


    

    

    

    

}



#pragma  mark - 滚动视图和缩放有关的代理方法

    //指定滚动视图上可缩放的子视图

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


    //取得scrollView上面所有的子视图,然后判断子视图类型,如果是UIImageView就返回

            //因为视图上还有滑动的也属于视图,所以要判断

        //遍历滚动视图的所有子视图

    for (id item in scrollView.subviews) {

        //判断子视图的类型是否为  UIImageView

        if ([item isKindOfClass:[UIImageView class]]) {

            return item;

        }

    }

    return nil;

}


#pragma  mark -滚动视图和滚动有关的代理方法

//当滚动结束以后       //当离开了放大的图片后,图片返回原来的大小

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

    

    //首先判断是哪一个scrollView在执行此代理方法,通过tag

    if (scrollView.tag == 2000) {

        //BigScrollView在执行此代理方法

            //确定当前在哪个界面

        float page = scrollView.contentOffset.x/CGRectGetWidth(self.view.frame);

            //得到当前所在界面上imageViewtag

        float myTag = 1000+page;

            //将当前界面相邻界面的位置和大小复原

            //一共有三种情况

                //1.当前界面是第一个界面(tag == 1000)和它相邻的只有下一个界面(tag ==1002)

                //2.当前界面是最后一个界面(tag ==1004)和它相邻的只有上一个届满(tag ==1003)

                //3.当前界面在中间(myTag)和它相邻的有两个界面,(tag ==myTag-1)(tag ==myTag+1)

        if (myTag ==1000) {

                //得到相邻界面的tag

            float nextTag = myTag + 1;

                //根据nextTag得到相应界面

            UIImageView *nextImageView = (UIImageView *)[scrollView viewWithTag:nextTag];

                //得到小得scrollView

            UIScrollView *aScrollView = (UIScrollView *)nextImageView.superview;

                //将大小复原

            aScrollView.zoomScale = 1.0;

                //将位置复原

            nextImageView.center = self.view.center;

                //contentiSize复原

            aScrollView.contentSize = CGSizeMake(CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame));

        }else if(myTag ==1004){

            float shangTag = myTag -1;

            UIImageView *shangImageView = (UIImageView *)[scrollView viewWithTag:shangTag];

            UIScrollView *bScrollView = (UIScrollView *)shangImageView.superview;

            bScrollView.zoomScale = 1.0;

            shangImageView.center = self.view.center;

            bScrollView.contentSize = CGSizeMake(CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame));

        }else if(myTag>1000 && myTag<1004){

            float shangTag = myTag -1;

            UIImageView *shangImageView = (UIImageView *)[scrollView viewWithTag:shangTag];

            UIScrollView *bScrollView = (UIScrollView *)shangImageView.superview;

            bScrollView.zoomScale = 1.0;

            shangImageView.center = self.view.center;

            bScrollView.contentSize = CGSizeMake(CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame));

            float nextTag = myTag + 1;

            UIImageView *nextImageView = (UIImageView *)[scrollView viewWithTag:nextTag];

            UIScrollView *aScrollView = (UIScrollView *)nextImageView.superview;

            aScrollView.zoomScale = 1.0;

            nextImageView.center = self.view.center;

            aScrollView.contentSize = CGSizeMake(CGRectGetWidth(self.view.frame),

                CGRectGetHeight(self.view.frame));

        }

    }

    

    

        //小白点的代理方法:

            //根据偏移量来计算当前是在哪个界面,也就是pageControl(小白点)currentpage(当前小白点所在的位置)的值

            //得到当前滚动视图的偏移量

    CGPoint offset =scrollView.contentOffset;

            //小白点的视图

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

    

            //水平方向的计算

    pageControl.currentPage =offset.x/CGRectGetWidth(self.view.frame);

    

    

    

    

    

   


 

   


    

    

    

    

    

    

}




//正在缩放的代理方法

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

        //得到scrollViewcontentSize的宽和高

    float contentWidth = scrollView.contentSize.width;

    float contentHeight = scrollView.contentSize.height;

        //得到scrollView  frame的宽和高

    float frameWidth = scrollView.frame.size.width;

    float frameHeight = scrollView.frame.size.height;

        //计算差量    也就是contentSize.width/2 +差量 ==水平中心点。

    float del_X = frameWidth > contentWidth ? (frameWidth - contentWidth)/2 : 0 ;

    float del_Y = frameHeight >contentHeight ? (frameHeight -contentHeight)/2 : 0;

        //得到imageView

    float imageViewTag = scrollView.tag - 2000;

    UIImageView *myImageView = (UIImageView *)[scrollView viewWithTag:imageViewTag];

        //设置中心点

    myImageView.center = CGPointMake(contentWidth/2+del_X, contentHeight/2+del_Y);

}




//小白点

-(void)page:(UIPageControl *)sender{

        //得到当前所在的小白点(当前所在的页数)

    int i = (int)sender.currentPage;

        //得到滚动视图

    UIScrollView *scrollView = (UIScrollView *)[self.view viewWithTag:2000];

        //要切换scrollView上面的子视图,是通过scrollView的一个属性来切换,该属性表示scrollView的偏移量

        //偏移量contentoffset就是 滚动视图的bounds original (x,y)

    scrollView.contentOffset = CGPointMake (i* CGRectGetWidth(self.view.frame), 0);

    

}





//定时器的回调方法  用途:切换界面

-(void)changeView{

    //得到滚动视图scrollView

    UIScrollView *scrollView = (UIScrollView *)[self.view viewWithTag:2000];

    //通过contentOffset 来切换滚动视图的子界面

    float offset_X = scrollView.contentOffset.x;

    

    //每次切换一个屏幕

    offset_X +=CGRectGetWidth(self.view.frame);

    

    //判断是否是最后一个子视图

    if (offset_X > 4*CGRectGetWidth(self.view.frame)) {

        offset_X = 0;

    }

    

    //得到最终的偏移量

    CGPoint resultPoint = CGPointMake(offset_X, 0);

    //切换视图的时候带动画效果

    //从最后一张回到第一张的时候,不带动画效果

    if (offset_X ==0) {

        [scrollView setContentOffset:resultPoint animated:NO];

    }else{

        [scrollView setContentOffset:resultPoint animated:YES];

    }

    UIPageControl *mypage = (UIPageControl *)[self.view viewWithTag:1111];

    

    mypage.currentPage += 1;

    if (mypage.currentPage > 4) {

        mypage.currentPage = 0;

    }

    

    

}















- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


/*

#pragma mark - Navigation


// In a storyboard-based application, you will often want to do a little preparation before navigation

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    // Get the new view controller using [segue destinationViewController].

    // Pass the selected object to the new view controller.

}

*/


@end



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值