iOSUI简单的页面轮转

来源:互联网 发布:淘宝男装牛仔衣服 编辑:程序博客网 时间:2024/06/15 14:20

#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 {

    [superviewDidLoad];

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

    MyScrollView *myScrol = [[MyScrollViewalloc]initWithFrame:CGRectMake(0,0, WIDTH,HEIGHT) andContentSize:CGSizeMake(WIDTH*7,HEIGHT) andHorizontal:NOandVertical:NOandBounces:NOandpagingEnabled:YESandWidth:WIDTHandHeight:HEIGHTandImageArray:array];

    [self.viewaddSubview:myScrol];

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

    [self.viewaddSubview:myScrol.page];

}


- (void)didReceiveMemoryWarning {

    [superdidReceiveMemoryWarning];

    // 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 = [superinitWithFrame: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 = [NSTimerscheduledTimerWithTimeInterval:3target:selfselector:@selector(changeoffSet)userInfo:nilrepeats:YES];

        [self addImage];

    }

    return self;

}

-(void)addImage{

    //将图片加入scrollView里面

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

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

        imageView.image = [UIImageimageNamed:_imageArray[i]];

        [self addSubview:imageView];

    }

    

}


//设置pageControl

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

    _page =[[UIPageControlalloc]initWithFrame:frame];

    _page.numberOfPages = num;

    _page.currentPage = current;

    _page.currentPageIndicatorTintColor = ccolor;

    _page.pageIndicatorTintColor = pcolor;

}


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

-(void)changeoffSet{

    

    [UIViewanimateWithDuration:1animations:^{

        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
原创粉丝点击