ios ui控件-UIScrollView封装

来源:互联网 发布:手机网络远程监控系统 编辑:程序博客网 时间:2024/05/17 22:46

大部分内容参考自http://code4app.com/category

需要点击实现跳转,把UIImageView换成UIButton即刻。

#import <UIKit/UIKit.h>

@interface ScrollViewModule :UIView <UIScrollViewDelegate>

@property (strong,nonatomic)UIScrollView *myScroll;

@property (nonatomic,strong)UIPageControl *pageControl;

@property (nonatomic,strong)NSTimer *timer;

@property (nonatomic,strong)NSArray *imageArray;

-(void)setup;

@end

#import "ScrollViewModule.h"

@implementation ScrollViewModule

@synthesize myScroll;

@synthesize imageArray;

@synthesize pageControl;

@synthesize timer;

- (id)initWithFrame:(CGRect)frame

{

    self = [super initWithFrame:frame];

    if (self) {

        self.frame =CGRectMake(0,0,320, 175);

    }

    return self;

}

-(void)setup

{

    [myScrollsetAutoresizesSubviews:NO];

    [pageControlsetAutoresizesSubviews:NO];

    if (IOS7_OR_LATER) {

        CGRect frame = CGRectMake(0.0,64.0,320.0,175.0);

        myScroll = [[UIScrollViewalloc]initWithFrame:frame];

    }else{

    CGRect frame = CGRectMake(0.0,0.0,320.0,175.0);

        myScroll = [[UIScrollViewalloc]initWithFrame:frame];

        myScroll.backgroundColor = [UIColorredColor];

    }

//    myScroll = [[UIScrollView alloc]initWithFrame:frame];

    myScroll.pagingEnabled=YES;

    myScroll.showsVerticalScrollIndicator=NO;

    myScroll.showsHorizontalScrollIndicator=NO;

    myScroll.contentSize =CGSizeMake(imageArray.count *320,175);

    myScroll.delegate=self;

    UIImageView *firstView = [[UIImageViewalloc]initWithImage:[UIImageimageNamed:[imageArraylastObject]]];

    CGFloat Width = myScroll.frame.size.width;

    CGFloat Height = myScroll.frame.size.height;

    firstView.frame = CGRectMake(0,0, Width, Height);

    [ myScroll addSubview:firstView];

    for (int i = 0; i<[imageArray count]; i ++) {

        UIImageView *subViews = [[UIImageViewalloc]initWithImage:[UIImageimageNamed:[imageArrayobjectAtIndex:i]]];

        subViews.frame = CGRectMake(Width*(i+1),0, Width, Height);

        [self.myScrolladdSubview: subViews];

    }

    UIImageView *lastView = [[UIImageViewalloc]initWithImage:[UIImageimageNamed:[imageArrayobjectAtIndex:0]]];

    lastView.frame = CGRectMake(Width*(imageArray.count+1),0, Width, Height);

    [self.myScroll addSubview:lastView];

    [self.myScroll setContentSize:CGSizeMake(Width*(imageArray.count+2), Height)];

    [self  addSubview:self.myScroll];

    [self.myScrollscrollRectToVisible:CGRectMake(Width,0, Width, Height) animated:NO];

    CGRect pageControlFrame = CGRectMake(261,210, 39,37);

    pageControl = [[UIPageControlalloc]initWithFrame:pageControlFrame];

    pageControl.numberOfPages =imageArray.count;

    pageControl.currentPage =0;

    pageControl.enabled =YES;

    [selfaddSubview:pageControl];

    NSDictionary *views =NSDictionaryOfVariableBindings(myScroll,pageControl);

    [selfaddConstraints:[NSLayoutConstraintconstraintsWithVisualFormat:@"H:|-0.0-[myScroll]-0.0-|"options:0metrics:nilviews:views]];

    if (IOS7_OR_LATER) {

            [selfaddConstraints:[NSLayoutConstraintconstraintsWithVisualFormat:@"V:|-64-[myScroll(==175.0)]"options:0metrics:nilviews:views]];

    }else{

         [selfaddConstraints:[NSLayoutConstraintconstraintsWithVisualFormat:@"V:|-0.0-[myScroll(==175.0)]"options:0metrics:nilviews:views]];

    }

    [selfaddConstraints:[NSLayoutConstraintconstraintsWithVisualFormat:@"H:|-261.0-[pageControl]-15.0-|"options:0metrics:nilviews:views]];

    if (IOS7_OR_LATER) {

          [selfaddConstraints:[NSLayoutConstraintconstraintsWithVisualFormat:@"V:|-144.0-[pageControl]"options:0metrics:nilviews:views]];

    }else{

    [selfaddConstraints:[NSLayoutConstraintconstraintsWithVisualFormat:@"V:|-80.0-[pageControl]"options:0metrics:nilviews:views]];

    }

    timer =[NSTimerscheduledTimerWithTimeInterval:2.0ftarget:selfselector:@selector(scrollToNextPage:)userInfo:nilrepeats:YES];

    

}


#pragma mark - UIScrollViewDelegate


-(void)scrollToNextPage:(id)sender

{

    int pageNum = pageControl.currentPage;

    CGSize viewSize = self.myScroll.frame.size;

    CGRect rect = CGRectMake((pageNum+2)*viewSize.width,0, viewSize.width, viewSize.height);

    [self.myScrollscrollRectToVisible:rectanimated:NO];

    pageNum++;

    if (pageNum == imageArray.count) {

        CGRect newRect = CGRectMake(viewSize.width,0, viewSize.width, viewSize.height);

        [self.myScrollscrollRectToVisible:newRectanimated:NO];

    }

}


-(void)scrollViewDidScroll:(UIScrollView *)scrollView

{

    CGFloat pageWidth = self.myScroll.frame.size.width;

    int currentPage = floor((self.myScroll.contentOffset.x - pageWidth/2)/pageWidth) + 1;

    if (currentPage == 0) {

        pageControl.currentPage=imageArray.count-1;

    }else if(currentPage ==imageArray.count+1){

        pageControl.currentPage =0;

    }

    pageControl.currentPage = currentPage-1;

    

}


-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView

{

    [timerinvalidate];

}


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

{

    timer = [NSTimerscheduledTimerWithTimeInterval:2.0ftarget:selfselector:@selector(scrollToNextPage:)userInfo:nilrepeats:YES];

}


-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView

{

    CGFloat pageWidth = self.myScroll.frame.size.width;

    CGFloat pageHeigth = self.myScroll.frame.size.height;

    int currentPage = floor((self.myScroll.contentOffset.x-pageWidth/2)/pageWidth)+1;

    if (currentPage == 0) {

        [self.myScrollscrollRectToVisible:CGRectMake(pageWidth*imageArray.count,0, pageWidth, pageHeigth) animated:NO];

        pageControl.currentPage=imageArray.count-1;

        NSLog(@"the last image");

        return;

    }else  if(currentPage == [imageArraycount]+1){

        [self.myScrollscrollRectToVisible:CGRectMake(pageWidth,0, pageWidth, pageHeigth)animated:NO];

        pageControl.currentPage =0;

        return;

    }

    pageControl.currentPage = currentPage-1;

    

}

@end


0 0
原创粉丝点击