• 改良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];

        

    }

  • 相关阅读:
    《相约星期二》--[美]米奇·阿尔博姆
    《把信送给加西亚》--[美]阿尔伯特·哈伯德
    《少有人走的路:心智成熟的旅程》--[美]M·斯科特·派克
    《穷爸爸和富爸爸》--[美]罗伯特·清崎,[美]莱希
    Error This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. T
    C#轻量级高性能日志组件EasyLogger
    我们为何要跳槽
    Grid++Report报表工具C/S实战篇(五)
    .NET 开源Protobuf-net从入门到精通
    怎样防止ddos攻击
  • 原文地址:https://www.cnblogs.com/wujie123/p/5277281.html
Copyright © 2020-2023  润新知