• scrollView封装广告轮播实例代码(Hank版)


    #import <UIKit/UIKit.h>

    @interface PJXPageView : UIView

    /* 圆点颜色*/

    @property(strong,nonatomic)UIColor *PJX_currentColor;

    /*没选中的圆点颜色*/

    @property(strong,nonatomic)UIColor *PJX_otherColor;

    /*  图片数据*/

    @property(strong,nonatomic)NSArray *PJX_imageNames;

    +(instancetype)pageView;

    @end

    //

    //  PJXPageView.m

    //  AdShow_PJXWang

    //

    //  Created by 王俏 on 16/9/26.

    //  Copyright © 2016年 wangqiao. All rights reserved.

    //

    #import "PJXPageView.h"

    @interface PJXPageView()<UIScrollViewDelegate>

    @property (weak, nonatomic) IBOutlet UIScrollView *scrollView;

    @property (weak, nonatomic) IBOutlet UIPageControl *pageControl;

    @property (nonatomic,strong) NSTimer *timer;

    @end

    @implementation PJXPageView

    -(instancetype)initWithFrame:(CGRect)frame{

        if (self = [super initWithFrame:frame]) {

            self = [PJXPageView pageView];

        }

        return self;

    }

    -(void)awakeFromNib{

    //    NSTimer *timer =  [NSTimer timerWithTimeInterval:1.5 target:self selector:@selector(nextPage:) userInfo:nil repeats:YES];

    //    [timer fire];

       self.timer =  [NSTimer scheduledTimerWithTimeInterval:1.5 target:self selector:@selector(nextPage:)  userInfo:nil repeats:YES];

     [[NSRunLoop mainRunLoop]addTimer:self.timer forMode:NSRunLoopCommonModes];

    }

    -(void)nextPage:(NSTimer*)timer

    {

        NSInteger page = self.pageControl.currentPage +1;

        if (page == self.pageControl.numberOfPages) {

            page = 0;

        }

        CGPoint offset = self.scrollView.contentOffset;

        offset.x = page *self.scrollView.frame.size.width;

        [self.scrollView setContentOffset:offset animated:YES];

    }

    -(void)setPJX_imageNames:(NSArray *)pjx_imageNames

    {

        _PJX_imageNames = pjx_imageNames;

        for (int i = 0 ; i < pjx_imageNames.count; i++) {

            UIImageView *imageView = [[UIImageView alloc]init];

            imageView.contentMode = UIViewContentModeScaleAspectFill;

            imageView.clipsToBounds = YES;

            imageView.image = [UIImage imageNamed:pjx_imageNames[i]];

            [self.scrollView addSubview:imageView];

            

        }

        self.pageControl.numberOfPages = pjx_imageNames.count;

        

    }

    -(void)setPJX_currentColor:(UIColor *)pjx_currentColor{

        _PJX_currentColor = pjx_currentColor;

         self.pageControl.currentPageIndicatorTintColor = pjx_currentColor;

    }

    -(void)setPJX_otherColor:(UIColor *)pjx_otherColor{

        _PJX_otherColor = pjx_otherColor;

        self.pageControl.pageIndicatorTintColor = pjx_otherColor;

    }

    -(void)layoutSubviews{

        [super layoutSubviews];

        [self.scrollView setContentSize:CGSizeMake(self.scrollView.frame.size.width*self.PJX_imageNames.count, 0)];

        

        for (int i = 0; i < self.scrollView.subviews.count; i++) {

            UIImageView *imageView = self.scrollView.subviews[i];

            [imageView setFrame:CGRectMake(i*self.scrollView.frame.size.width, 0, self.scrollView.frame.size.width, self.scrollView.frame.size.height)];

        }

    }

    +(instancetype)pageView{

        //实例化自己

        PJXPageView *pageView =[[[NSBundle mainBundle]loadNibNamed:@"PJXPageView" owner:nil options:nil] firstObject];

        [pageView.scrollView setDelegate:pageView];

        return pageView;

    }

    #pragma mark -- UIScrollViewDelegate

    -(void)scrollViewDidScroll:(UIScrollView *)scrollView{

        NSUInteger index = (NSUInteger)(scrollView.contentOffset.x/scrollView.frame.size.width+0.5);

        self.pageControl.currentPage = index;

    }

    -(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{

        [self stopTimer];

    }

    -(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{

         self.timer =  [NSTimer scheduledTimerWithTimeInterval:1.5 target:self selector:@selector(nextPage:)  userInfo:nil repeats:YES];

    }

    -(void)stopTimer

    {

        [self.timer invalidate];

    }

    @end

    使用:

     PJXPageView *page = [[PJXPageView alloc]init];

        page.PJX_imageNames = @[@"001",@"002",@"003",@"004"];

        page.PJX_currentColor = [UIColor blueColor];

        page.PJX_otherColor = [UIColor grayColor];

        page.frame = CGRectMake(100, 100, 220, 220);

        [self.view addSubview:page];

  • 相关阅读:
    求给定数组中最大值和其在数组中的索引并输出
    多线程与多进程
    logging模块
    QWidget上下文菜单处理函数
    python中的yield关键字
    菜单栏(QMenuBar)与菜单(QMenu)
    PyQt5布局管理(1)
    QMainFrame类
    QTP11使用DOM XPath以及CSS识别元素对象
    C# 跨线程访问控件
  • 原文地址:https://www.cnblogs.com/PJXWang/p/5920709.html
Copyright © 2020-2023  润新知