• 改良UIScrollView滚动视图


    #define HEIGHT  self.view.frame.size.height

    #define WIDTH    self.view.frame.size.width

    @interface ViewController : UIViewController<UIScrollViewDelegate>

    @property (strong,nonatomic) UIScrollView *myScorolV;

    @property (strong,nonatomic) UIPageControl *pageC;

    @property (strong,nonatomic) NSMutableArray *arrPage;

    @property (strong,nonatomic) UIImageView *threeImage;

    @property (strong,nonatomic) UIImageView *secondImage;

    @property (strong,nonatomic) UIImageView *firstImage;

     @property (assign,nonatomic) int currentPage; 

    @end

      

       //滚动视图创建

        self.myScorolV = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];

        self.myScorolV.contentSize = CGSizeMake(WIDTH * 3, 0);

        self.myScorolV.pagingEnabled = YES;

        self.myScorolV.showsHorizontalScrollIndicator = NO;

        self.myScorolV.delegate = self;

      

        [self.view addSubview:self.myScorolV];

        

       //分页视图

        self.pageC = [[UIPageControl alloc] initWithFrame:CGRectMake(WIDTH / 5 * 2, HEIGHT - 80, WIDTH / 5, 40)];

        self.pageC.backgroundColor = [UIColor clearColor];

        self.pageC.currentPage = 0;

        self.pageC.numberOfPages = 5;

        //指定页码颜色

        self.pageC.currentPageIndicatorTintColor = [UIColor redColor];

        self.pageC.pageIndicatorTintColor = [UIColor greenColor];

        [self.view addSubview:self.pageC];

        

        self.arrPage = [NSMutableArray arrayWithCapacity:10];

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

        {

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

            [self.arrPage addObject:image];

        }

        

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

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

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

        

        self.currentPage = 0;

        

        [self relodeImage];

        

    }

    -(void)relodeImage

    {

        if (self.currentPage == 0)

        {

            self.firstImage.image = [self.arrPage lastObject];

            self.secondImage.image = [self.arrPage objectAtIndex:self.currentPage];

            self.threeImage.image = [self.arrPage objectAtIndex:self.currentPage + 1];

        }

        

        else if (self.currentPage == self.arrPage.count - 1)

        {

            self.firstImage.image = [self.arrPage objectAtIndex:self.currentPage - 1];

            self.secondImage.image = [self.arrPage objectAtIndex:self.currentPage];

            self.threeImage.image = [self.arrPage objectAtIndex:0];

        }

        

        else

        {

            self.firstImage.image = [self.arrPage objectAtIndex:self.currentPage - 1];

            self.secondImage.image = [self.arrPage objectAtIndex:self.currentPage];

            self.threeImage.image = [self.arrPage objectAtIndex:self.currentPage + 1];

        }

        

        

        self.firstImage.frame = CGRectMake(0, 0, WIDTH, HEIGHT);

        self.secondImage.frame = CGRectMake(WIDTH, 0, WIDTH, HEIGHT);

        self.threeImage.frame = CGRectMake(WIDTH * 2, 0, WIDTH, HEIGHT);

        

        [self.myScorolV addSubview:self.firstImage];

        [self.myScorolV addSubview:self.secondImage];

        [self.myScorolV addSubview:self.threeImage];

        

        self.myScorolV.contentOffset = CGPointMake(WIDTH, 0);

    }

    -(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView

    {

        int number = (int)self.myScorolV.contentOffset.x;

        

        if (number < 0 || number == 0)

        {

            if (self.currentPage == 0)

            {

                self.currentPage = (int)(self.arrPage.count - 1);

            }

            

            else

            {

               self.currentPage--;

            }

        }

        

        else if (number > WIDTH * 2 || number == WIDTH * 2)

        {

            if (self.currentPage == (int)self.arrPage.count - 1)

            {

                self.currentPage = 0;

            }

            else

            {

                self.currentPage++;

            }

        }

        self.pageC.currentPage = self.currentPage;

        [self relodeImage];

        

    }

  • 相关阅读:
    升级xcode7.0 第三方库不能用的解决方法(bitcode是什么鬼?)
    java调用C#的dll
    微信JS SDK Demo
    【jQuery】smartMenu右键自定义上下文菜单插件(似web QQ)
    搭建Windows SVN服务器及TortoiseSVN使用帮助和下载
    微信jsSDK开发
    用c#开发微信(10) JSSDK 基本用法 分享接口“发送到朋友”
    MVC4.0网站发布和部署到IIS7.0上的方法
    C#的事件处理机制
    ASP.Net MVC开发基础学习笔记(1):走向MVC模式
  • 原文地址:https://www.cnblogs.com/wujie123/p/5277281.html
Copyright © 2020-2023  润新知