滚动视图及缩放

作者: ThEAll | 来源:发表于2015-12-03 17:45 被阅读269次

    import "AppDelegate.h"

    import "RootViewController.h"

    import "ZoomViewController.h"

    @interface AppDelegate ()

    @end

    @implementation AppDelegate

    • (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
      self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
      // Override point for customization after application launch.
      self.window.backgroundColor = [UIColor whiteColor];
      [self.window makeKeyAndVisible];

      RootViewController *rootVC = [[RootViewController alloc]init];
      [self.window setRootViewController:rootVC];

      ZoomViewController *zoomVC = [[ZoomViewController alloc]init];
      [self.window setRootViewController:zoomVC];

      return YES;
      }


    import "RootViewController.h"

    @interface RootViewController ()<UIScrollViewDelegate>

    @end

    @implementation RootViewController

    • (void)viewDidLoad {
      [super viewDidLoad];
      self.view.backgroundColor = [UIColor orangeColor];

      // 创建滚动视图 当我们显示的内容超过一屏时,就需要用到滚动视图
      UIScrollView scroolView = [[UIScrollView alloc]initWithFrame:self.view.frame];
      scroolView.tag = 1000;
      scroolView.backgroundColor = [UIColor whiteColor];
      // 设置滚动区域
      [scroolView setContentSize:CGSizeMake(CGRectGetWidth(self.view.frame)
      3, CGRectGetHeight(self.view.frame))];
      // 为scroll添加子视图(设置为4屏)
      // NSArray *colorArr = [NSArray arrayWithObjects:[UIColor redColor],[UIColor blueColor],[UIColor whiteColor],[UIColor greenColor], nil];
      NSArray *imageArr = [NSArray arrayWithObjects:@"0.png",@"1.png",@"2.png", nil];
      for (int i = 0; i < 3; i++) {

        UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(CGRectGetWidth(self.view.frame)*i, 0, CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame))];
        imageView.image = [UIImage imageNamed:imageArr[i]];
        imageView.userInteractionEnabled = YES;
        [scroolView addSubview:imageView];
      
        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.backgroundColor = colorArr[i];
        label.textAlignment = NSTextAlignmentCenter;
        label.font = [UIFont systemFontOfSize:60];
        [scroolView addSubview:label];
      

      }
      [self.view addSubview:scroolView];
      // 设置分页效果(默认值为NO)
      scroolView.pagingEnabled = YES;
      // 设置横向滚动条是否显示(默认值为YES)
      scroolView.showsHorizontalScrollIndicator = YES;

      // 设置边界是否有反弹效果(默认值为YES)
      scroolView.bounces = NO;
      // 设置代理
      scroolView.delegate = self;

      // 初始化UIPageControl
      UIPageControl *pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(0, CGRectGetHeight(self.view.frame)-40, CGRectGetWidth(self.view.frame), 40)];
      pageControl.tag = 1001;

      pageControl.pageIndicatorTintColor = [UIColor blackColor];
      pageControl.currentPageIndicatorTintColor = [UIColor yellowColor];
      // 设置页数
      [pageControl setNumberOfPages:3];
      // 设置当前所在页
      pageControl.currentPage = 1;
      // 添加触发事件
      [pageControl addTarget:self action:@selector(pageAction:) forControlEvents:UIControlEventValueChanged];
      [pageControl setBackgroundColor:[UIColor grayColor]];
      [self.view addSubview:pageControl];

    }

    //pageControl的回调方法
    -(void)pageAction:(UIPageControl)sender{
    // 根据当前的页数,使得scrollView也滑动到对应的子视图 通过设置scrollView的contentOffset来实现(改变contentOffset实际上就是改变scrollView的bounds)
    // 得到当前的页数
    int page = (int)sender.currentPage;
    // 根据当前页数来计算偏移量
    // 得到scrollView
    UIScrollView scrollView = (UIScrollView)[self.view viewWithTag:1000];
    // 设置scrollView的偏移量 当前的页数乘以屏幕宽度
    [scrollView setContentOffset:CGPointMake(page
    CGRectGetWidth(self.view.frame), 0) animated:YES];
    }

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

    // 开始拖拽(手指触碰到屏幕,并且移动)
    -(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    NSLog(@"-------%s",func);
    }

    // 已经开始滚动(只要scrollView是滚动状态就会调用次方法)
    -(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    // 滚动视图上面的子视图可以移动,不是子视图改变自身的frame值,而是通过更改滚动视图(也就是子视图的父视图)的bounds的origin(也就是x,y)来更改子视图在父视图(滚动视图)上显示的位置
    // scrollView的偏移量
    CGPoint offSet = scrollView.contentOffset;
    NSLog(@"_____%f",offSet.x);
    // 得到scrollView的bounds的x点
    float x = scrollView.bounds.origin.x;
    NSLog(@"bounds********%f",x);
    NSLog(@"--------%s",func);
    }

    // 停止拖拽 当手指(触摸对象)离开,正在滚动的视图减速
    -(void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView{
    NSLog(@"_____%s",func);
    }

    // 视图真正静止(视图不动了)
    -(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    // 根据scrollView的偏移量来计算当前的页数
    int page = (int)scrollView.contentOffset.x/CGRectGetWidth(self.view.frame);
    // 得到pageControl
    UIPageControl pageControl = (UIPageControl)[self.view viewWithTag:1001];
    // 设置当前的页数
    pageControl.currentPage = page;

    NSLog(@"%s",__func__);
    

    }

    • (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 "ZoomViewController.h"

    @interface ZoomViewController ()<UIScrollViewDelegate>

    @end

    @implementation ZoomViewController

    • (void)viewDidLoad {
      [super viewDidLoad];

      // 初始化scrollView
      UIScrollView *scrollView = [[UIScrollView alloc]initWithFrame:self.view.frame];
      [scrollView setBackgroundColor:[UIColor orangeColor]];
      // 设置scrollView代理
      scrollView.delegate = self;
      // 设置缩放比例
      // 设置可缩小到的最小比例
      scrollView.minimumZoomScale = 0.5;
      // 设置可放大到的最大比例
      scrollView.maximumZoomScale = 2.0;

      [self.view addSubview:scrollView];
      // 添加图片,使得添加的图片宽高成比例
      UIImage myImage = [UIImage imageNamed:@"3.png"];
      // 得到图片的原始宽高
      float imageWidth = myImage.size.width;
      float imageHeight = myImage.size.height;
      // 这里规定imageView的宽为200,根据此宽度,得到等比例的高度
      float imageViewWidth = 200;
      float imageViewHeight = 200
      (imageHeight/imageWidth);
      // 初始化UIImageView
      UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, imageViewWidth, imageViewHeight)];
      imageView.tag = 1000;
      // 为imageView设置图片
      imageView.image = myImage;
      // 让imageView居中
      imageView.center = self.view.center;
      [scrollView addSubview:imageView];

    }

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

    // 指定scrollView的某一个子视图为可缩放视图,前提条件是此视图已经添加到scrollView上面
    -(UIView*)viewForZoomingInScrollView:(UIScrollView *)scrollView{
    UIImageView imageView = (UIImageView)[scrollView viewWithTag:1000];
    return imageView;
    }

    // 开始缩放 第二个参数view:是指我们将要缩放的视图(这里就是imageView)
    -(void)scrollViewWillBeginZooming:(UIScrollView *)scrollView withView:(UIView *)view{
    NSLog(@"%@",view);
    }

    // 正在缩放 只要正在缩放就会执行此方法,所以此方法在放缩过程中会多次调用
    -(void)scrollViewDidZoom:(UIScrollView *)scrollView{

    //  在缩放过程中,为了使得该子视图一直在屏幕中间位置,所以我们需要在它缩放过程中一直调整他的center
    //  得到scrollView的子视图
    UIImageView *imageView = (UIImageView*)[scrollView viewWithTag:1000];
    //  打印imageView的frame。分析为什么它的位置会改变
    NSLog(@"frame___%@",NSStringFromCGRect(imageView.frame));
    
    //  设置imageView的center,使得它的位置一直在屏幕中间
    imageView.center =self.view.center;
    
    //  打印contentSize  分析为什么缩放之后会滑动
    NSLog(@"contentSize------%@",NSStringFromCGSize(scrollView.contentSize));
    

    }

    // 缩放结束
    // 第二个参数 : 当前正在缩放的视图
    // 第三个参数 : 当前正在缩放视图的缩放比例
    -(void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(CGFloat)scale{
    // 缩放完成之后恢复原大小,这里使用的是2D方式变换函数中与捏合有关的函数
    // view.transform = CGAffineTransformMakeScale(1, 1);
    }

    • (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

    相关文章

      网友评论

        本文标题:滚动视图及缩放

        本文链接:https://www.haomeiwen.com/subject/rxdshttx.html