• UIScrollView, 滚动视图


     UIScrollView, 滚动视图

        UIScrollView, 滚动视图, 继承于UIView, 用于显示超过一个屏幕的内容

        frame: 是滚动视图可视区域的大小

        UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
        scrollView.backgroundColor = [UIColor colorWithRed:0.586 green:1.000 blue:0.977 alpha:1.000];

        内容页的大小, 默认值:CGSizeZero

        contentSize.width > frame. 水平滚动

        contentSize.height > frame.height: 垂直滚动

     scrollView.contentSize = CGSizeMake(375 * 2, 667 * 2); 

       是否能滚动到顶部(点击状态栏触发), 默认是YES
        scrollView.scrollsToTop = YES;
        内容页的偏移量
        scrollView.contentOffset = CGPointMake(0, 0);
        [scrollView setContentOffset:CGPointMake(0, 0) animated:YES];
        是否可以滚动, 默认YES
        scrollView.scrollEnabled = YES;
        是否显示垂直滚动条, 默认是YES
        scrollView.showsVerticalScrollIndicator = YES;
        是否显示水平滚动条, 默认是YES
        scrollView.showsHorizontalScrollIndicator = YES;
        是否回弹(前提: 能够滚动), 默认YES
        scrollView.bounces = YES;
        是否回弹(前提: 不能滚动, bounces是YES), 默认NO
        scrollView.alwaysBounceVertical = YES;
        scrollView.alwaysBounceHorizontal = YES;
        是否整屏滚动, 默认是NO
        scrollView.pagingEnabled = NO;
        代理
        scrollView.delegate = self;
        scrollView.tag = 1024;
        [self.view addSubview:scrollView];
        [scrollView release];

    UIImageView如果不设置frame, frame大小 = 图片大小

      imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Baymax.jpg"]]; 

    只有添加到scrollView, 视图才能跟着scrollView一起滚动

        [scrollView addSubview:imageView];
        [imageView release];

     滚动视图上的内容缩放

        1.设置delegate

        2.修改scrollView属性

       当前的缩放比例, 默认值1.0
        scrollView.zoomScale = 1.0;
        最大缩放比例, 默认为1.0
        scrollView.maximumZoomScale = 4.0;
        最小缩放比例, 默认为1.0
        scrollView.minimumZoomScale = 0.5;
     3.指定缩放的视图

        UIPageControl, 页面控件, 继承于UIControl, 一般配合UIScrollView使用

        : UIPageControl适用于页数比较少的情况, 比如5页以内; 如果页数比较多, 可以使用UILabel直接显示"99"

       UIPageControl *pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(100, 600, 175, 40)];
        pageControl.backgroundColor = [UIColor blackColor];
        设置页数, 默认为0
        pageControl.numberOfPages = 5;
        当前页数, 默认为0
        pageControl.currentPage = 1;
        未选中圆点颜色
        pageControl.pageIndicatorTintColor = [UIColor redColor];
        选中圆点颜色
        pageControl.currentPageIndicatorTintColor = [UIColor greenColor];
        当只有一页时, 是否隐藏
        pageControl.hidesForSinglePage = YES;
        [pageControl addTarget:self action:@selector(changePage:) forControlEvents:UIControlEventValueChanged];
        [self.view addSubview:pageControl];
        [pageControl release];
        
        UIButton *nextButton = [UIButton buttonWithType:UIButtonTypeSystem];
        nextButton.frame = CGRectMake(375 - 100, 40, 80, 30);
        [nextButton setTitle:@"下 一 页" forState:UIControlStateNormal];
        [nextButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [nextButton addTarget:self action:@selector(nextPage) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:nextButton];
        
        UIButton *reset = [UIButton buttonWithType:UIButtonTypeSystem];
        reset.frame = CGRectMake(20, 40, 80, 30);
        [reset setTitle:@"还    原" forState:UIControlStateNormal];
        [reset setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [reset addTarget:self action:@selector(reset) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:reset];
    
    - (void)nextPage {
        UIScrollView *scrollView = (UIScrollView *)[self.view viewWithTag:1024];
        改变偏移量
    //  scrollView.contentOffset = CGPointMake(375, 0);
        [scrollView setContentOffset:CGPointMake(375, 0) animated:YES];
        
    }
    
    - (void)reset {
        UIScrollView *scrollView = (UIScrollView *)[self.view viewWithTag:1024];
        重置scrollView的缩放比例
    //    scrollView.zoomScale = 1.0;
        [scrollView setZoomScale:1.0 animated:YES];
    }
    #pragma mark - UIScrollViewDelegate
    //当滚动视图的偏移量做改变, 执行这个方法
    - (void)scrollViewDidScroll:(UIScrollView *)scrollView {
        NSLog(@"已经滚动");
    }
    
    - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
        NSLog(@"将要开始拖拽");
    }
    
    - (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset {
        //inout: 方法 内部使用的变量 和 外部使用的变量 是同一个
        NSLog(@"将要结束拖拽");
    }
    
    - (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
        NSLog(@"已经结束拖拽");
    }
    
    - (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView {
        NSLog(@"将要开始减速");
    }
    
    - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
        NSLog(@"已经结束减速");
    }
    
    //调用setContentOffset:animated:这个方法触发
    - (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {
         NSLog(@"已经结束滚动动画");
    }
    
    - (BOOL)scrollViewShouldScrollToTop:(UIScrollView *)scrollView {
        return YES;
    }
    
    - (void)scrollViewDidScrollToTop:(UIScrollView *)scrollView {
        NSLog(@"已经滚动到顶部");
    }
    
    - (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
        //指定缩放的视图
        return imageView;
    }
    
    - (void)scrollViewDidZoom:(UIScrollView *)scrollView {
        NSLog(@"滚动视图已经缩放");
    }
    
    - (void)scrollViewWillBeginZooming:(UIScrollView *)scrollView withView:(UIView *)view {
        NSLog(@"滚动视图将要开始缩放");
    }

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

        NSLog(@"滚动视图已经结束缩放");

    } 

    #import "WelcomeViewController.h"
    @interface WelcomeViewController () <UIScrollViewDelegate> {
        UIScrollView *bgScrollView;
        UIPageControl *pagecontrol;
    }
    @end
    @implementation WelcomeViewController
    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view.
        
        //创建滚动视图
        bgScrollView = [[UIScrollView alloc] initWithFrame:self.view.frame];
        bgScrollView.backgroundColor = [UIColor colorWithRed:0.966 green:1.000 blue:0.429 alpha:1.000];
        bgScrollView.contentSize = CGSizeMake(320 * 4, 568);
        bgScrollView.pagingEnabled = YES;
        bgScrollView.showsHorizontalScrollIndicator = NO;
        bgScrollView.tag = 101;
        bgScrollView.delegate = self;
        [self.view addSubview:bgScrollView];
        [bgScrollView release];
        
        for (NSInteger i = 1; i <= 4 ; i++) {
            NSString *name = [NSString stringWithFormat:@"%ld.jpg", i];
            UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:name]];
            imageView.frame = CGRectMake(320 * (i - 1), 0, 320, 568);
            [bgScrollView addSubview:imageView];
        }
        pagecontrol = [[UIPageControl alloc] initWithFrame:CGRectMake(0, 528, 320, 40)];
    //  pagecontrol.backgroundColor = [UIColor yellowColor];
        pagecontrol.numberOfPages = 4;
        pagecontrol.pageIndicatorTintColor = [UIColor grayColor];
        pagecontrol.currentPageIndicatorTintColor = [UIColor greenColor];
        [pagecontrol addTarget:self action:@selector(changePage:) forControlEvents:UIControlEventValueChanged];
        [self.view addSubview:pagecontrol];
        [pagecontrol release];
    }
    - (void)changePage:(UIPageControl *)pageControl {
        bgScrollView.contentOffset = CGPointMake(320 * pageControl.currentPage, 0);
    }
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    #pragma mark - UIScrollViewDelegate
    - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
    {
        pagecontrol.currentPage = scrollView.contentOffset.x / 320;
        NSLog(@"%.2lf", scrollView.contentOffset.x / 320);
    }
    @end

    The one who wants to wear a crown must bear the weight!
  • 相关阅读:
    007 jquery过滤选择器-----------(屬性过滤选择器)
    006 jquery过滤选择器-----------(可见性过滤选择器)
    005 jquery过滤选择器-----------(内容过滤选择器)
    004 jquery过滤选择器-----------(基本过滤选择器)
    003 jquery层次选择器
    002 jquery基本选择器
    css基本知识
    000 Html基本标签与案例
    001 jquery对象与dom对象的转换
    000 Jquery的Hello World程序
  • 原文地址:https://www.cnblogs.com/OrangesChen/p/4925875.html
Copyright © 2020-2023  润新知