iOSUI简单的页面轮转

#define WIDTH [UIScreen mainScreen].bounds.size.width

#define HEIGHT [UIScreen mainScreen].bounds.size.height

#import "ViewController.h"

#import "MyScrollView.h"

@interface ViewController ()


@end


@implementation ViewController


- (void)viewDidLoad {

    [super viewDidLoad];

     NSArray *array = @[@"sc4.jpg",@"sc0.jpg",@"sc1.jpg",@"sc2.jpg",@"sc3.jpg",@"sc4.jpg",@"sc0.jpg"];

    MyScrollView *myScrol = [[MyScrollView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT) andContentSize:CGSizeMake(WIDTH*7, HEIGHT) andHorizontal:NO andVertical:NO andBounces:NO andpagingEnabled:YES andWidth:WIDTH andHeight:HEIGHT andImageArray:array];

    [self.view addSubview:myScrol];

    [myScrol setPage:CGRectMake(40, HEIGHT-60, WIDTH-80, 30) andNumberOfPage:5 andCurrentPage:0 andCurrentColor:[UIColor yellowColor] andPageColor:[UIColor redColor]];

    [self.view addSubview:myScrol.page];

}


- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


@end




#import <UIKit/UIKit.h>


@interface MyScrollView : UIScrollView<UIScrollViewDelegate>

{

    NSTimer *_timer;

}

@property(nonatomic ,copy)NSArray *imageArray;//轮转图片名的数组

@property(nonatomic,strong)UIPageControl *page;//图片上的圆点

@property(nonatomic,assign)CGFloat width;//页面的宽

@property(nonatomic,assign)CGFloat height;//页面的高

-(id)initWithFrame:(CGRect)frame andContentSize:(CGSize)size andHorizontal:(BOOL)h andVertical:(BOOL)v andBounces:(BOOL)b andpagingEnabled:(BOOL)p andWidth:(CGFloat)width andHeight:(CGFloat)height andImageArray:(NSArray *)array;//初始化ScrollView


-(void)setPage:(CGRect)frame andNumberOfPage:(NSInteger) num andCurrentPage:(NSInteger)current andCurrentColor:(UIColor*)ccolor andPageColor:(UIColor *)pcolor;//初始化page



 @end


#import "MyScrollView.h"


@implementation MyScrollView

-(id)initWithFrame:(CGRect)frame andContentSize:(CGSize)size andHorizontal:(BOOL)h andVertical:(BOOL)v andBounces:(BOOL)b andpagingEnabled:(BOOL)p andWidth:(CGFloat)width andHeight:(CGFloat)height andImageArray:(NSArray *)array{

    if (self = [super initWithFrame:frame]) {//设置画布大小

         //widthheight 同时小于画布大小的时候 ,控件无法滑动 ,有其中一个大于画布 则决定滑动的方向 同时大于则左右上下都可滑动

        //设置画布中内容的大小

        self.contentSize = size;

        //横向滑动的条去掉

        self.showsHorizontalScrollIndicator = h;

        //上下的滑动的条去掉

        self.showsVerticalScrollIndicator = v;

        //反弹

        self.bounces = b;

        //翻页效果

        self.pagingEnabled = p;

        self.delegate = self;

        _width = width;

        _height = height;

        _imageArray = array;

        //设置timer

        _timer = [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(changeoffSet) userInfo:nil repeats:YES];

        [self addImage];

    }

    return self;

}

-(void)addImage{

    //将图片加入scrollView里面

    for (int i=0; i<7; i++) {

        UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(_width*i, 0, _width, _height)];

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

        [self addSubview:imageView];

    }

    

}


//设置pageControl

-(void)setPage:(CGRect)frame andNumberOfPage:(NSInteger)num andCurrentPage:(NSInteger)current andCurrentColor:(UIColor *)ccolor andPageColor:(UIColor *)pcolor{

    _page =[[UIPageControl alloc]initWithFrame:frame];

    _page.numberOfPages = num;

    _page.currentPage = current;

    _page.currentPageIndicatorTintColor = ccolor;

    _page.pageIndicatorTintColor = pcolor;

}


//timer要执行的方法 //自动翻转

-(void)changeoffSet{

    

    [UIView animateWithDuration:1 animations:^{

        self.contentOffset = CGPointMake(self.contentOffset.x+_width, 0);//contentOffSet 偏移量

    }completion:^(BOOL finished) {

        if (self.contentOffset.x == 6*_width) {

            self.contentOffset = CGPointMake(_width, 0);

        }

        _page.currentPage = self.contentOffset.x/_width-1;

    }];

    

}

//代理执行方法  //手动翻转

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

    //0 1 2 3 4 5 6

    //4 0 1 2 3 4 0

    if (scrollView.contentOffset.x == _width*6) {

        scrollView.contentOffset = CGPointMake(_width, 0);

        NSLog(@"1");

    }

    else if (scrollView.contentOffset.x ==0) {

        scrollView.contentOffset = CGPointMake(_width*5, 0);

    }

    _page.currentPage = self.contentOffset.x/_width-1;

  

    

}



@end



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值