zoukankan      html  css  js  c++  java
  • iOS UIScrollView 的基本用法

    滚视图的用途很普遍,掌握一些基本用法是必须的,以下是一些基本的用法:

    #import <UIKit/UIKit.h>

    //宏定义

    #define Width 300

    #define Height 300

    #define X 60

    #define Y 100

    @interface ViewController : UIViewController<UIScrollViewAccessibilityDelegate>

    @property(strong,nonatomic) UIScrollView *MyScrollView;

    //页码

    @property(strong,nonatomic) UIPageControl *MyPageControl;

    //存储图片的集合

    @property(strong,nonatomic) NSMutableArray *imagesArray;

    //当前页码

    @property(assign,nonatomic) int CurrentPage;

    @property(strong,nonatomic) UIImageView *FirstImage ;

    @property(strong,nonatomic) UIImageView *SecondImage ;

    @property(strong,nonatomic) UIImageView *ThirdImage ;

    @end

    //创建滚视图

        self.MyScrollView=[[UIScrollView alloc] initWithFrame:CGRectMake(X, Y, Width,Height)];

        self.MyScrollView.backgroundColor=[UIColor grayColor];

        self.MyScrollView.contentSize=CGSizeMake(Width*3,0);

        self.MyScrollView.delegate=self;

        self.MyScrollView.pagingEnabled=YES;

        self.MyScrollView.showsHorizontalScrollIndicator=NO;

        [self.view addSubview:self.MyScrollView];

        

        //初始化存储图片的集合

        

        self.imagesArray=[NSMutableArray arrayWithCapacity:100];

        for (int i=1; i<7; i++)

        {

            UIImage *images=[UIImage imageNamed:[NSString stringWithFormat:@"%d.png",i]];

            [self.imagesArray addObject:images];

        }

        

        self.FirstImage=[[UIImageView alloc] init];

        self.SecondImage=[[UIImageView alloc] init];

        self.ThirdImage=[[UIImageView alloc] init];

        

        self.CurrentPage=0;

        

      

         //设置分页

        self.MyPageControl=[[UIPageControl alloc] init];

        CGSize page=CGSizeMake(120, 44);

        self.MyPageControl.frame=CGRectMake((400-page.width)/2, 370, page.width, page.height);

        self.MyPageControl.numberOfPages=6;

        self.MyPageControl.currentPage=0;

        //当前页码的颜色

        self.MyPageControl.currentPageIndicatorTintColor=[UIColor greenColor];

        //指定页码颜色

        self.MyPageControl.pageIndicatorTintColor=[UIColor purpleColor];

        [self.view addSubview:self.MyPageControl];

        //刷新

        [self reloadImage];

    }

    -(void)reloadImage

    {

        //第一种情况 显示图片在第一页 是最后一张图片

        if (self.CurrentPage==0)

        {

            self.FirstImage.image=[self.imagesArray lastObject];

            self.SecondImage.image=[self.imagesArray objectAtIndex:self.CurrentPage];

            self.ThirdImage.image=[self.imagesArray objectAtIndex:self.CurrentPage+1];

        }

        //第二种情况 显示图片在最后一页  是前面一张图片

        else if (self.CurrentPage==self.imagesArray.count-1)

        {

            self.FirstImage.image=[self.imagesArray objectAtIndex:self.CurrentPage-1];

            self.SecondImage.image=[self.imagesArray objectAtIndex:self.CurrentPage];

            self.ThirdImage.image=[self.imagesArray objectAtIndex:0];

        }

        //中间页 显示图片按正常顺序开始

        else

        {

            self.FirstImage.image=[self.imagesArray objectAtIndex:self.CurrentPage-1];

            self.SecondImage.image=[self.imagesArray objectAtIndex:self.CurrentPage];

            self.ThirdImage.image=[self.imagesArray objectAtIndex:self.CurrentPage+1];

        }

        //默认是 NO

        self.FirstImage.userInteractionEnabled=YES;

        //图片的框架大小

        self.FirstImage.frame=CGRectMake(0,0,Width,Height);

        self.SecondImage.frame=CGRectMake(Width,0, Width, Height);

        self.ThirdImage.frame=CGRectMake(Width*2,0, Width, Height);

        //把图片添加到滚动视图上

        [self.MyScrollView addSubview:self.FirstImage];

        [self.MyScrollView addSubview:self.SecondImage];

        [self.MyScrollView addSubview:self.ThirdImage];

        

        //滚视图的偏移量

        self.MyScrollView.contentOffset=CGPointMake(Width, 0);

        

    }

    #pragma mark  delegate 代理

    -(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView

    {

        float ScroPage=self.MyScrollView.contentOffset.x;

        //向右滑动

        if (ScroPage> 2||ScroPage==2)

        {

            if(self.CurrentPage ==(int)self.imagesArray.count-1)

            {

                self.CurrentPage=0;

            }

            else

            {

                self.CurrentPage++;

            }

        }

        

       //向左滑动

        if (ScroPage<0||ScroPage==0)

        {

            if (self.CurrentPage==0)

            {

                self.CurrentPage =(int)self.imagesArray.count-1;

            }

            else

            {

                self.CurrentPage--;

            }

        }

        self.MyPageControl.currentPage=self.CurrentPage;

        //刷新

        [self reloadImage];

  • 相关阅读:
    iOS开发CoreAnimation解读之三——几种常用Layer的使用解析
    iOS开发CoreAnimation解读之二——对CALayer的分析
    iOS开发CoreAnimation解读之一——初识CoreAnimation核心动画编程
    在最完整的搜索提示降史上的用户交互的研究——阅读《An Eye-tracking Study of User Interactions with Query Auto Completion》
    学习算法
    This Android SDK requires Android Developer Toolkit version 22.6.2 or above.
    一切都不是为了营销手段的目的都是耍流氓
    LeetCode219:Contains Duplicate II
    无尽的循环ViewPager
    允许Ubuntu14.04&quot;保存&quot;屏幕亮度值
  • 原文地址:https://www.cnblogs.com/tmf-4838/p/5357166.html
Copyright © 2011-2022 走看看