码迷,mamicode.com
首页 > 其他好文 > 详细

继承UIView实现的简单UIScrollView

时间:2014-05-21 15:09:25      阅读:215      评论:0      收藏:0      [点我收藏+]

标签:style   c   ext   color   a   int   

#import <UIKit/UIKit.h>


@interface ScrollView : UIView

{

    UIView *contentView;

    CGSize contentSize;

    CGPoint contentOffset;

    CGRect contentFrame;

    

    

    BOOL scrollEnabled;

    BOOL pagingEnabled;


    float totalPage;

    float curPage;

    

    NSTimeInterval beginTime;

    NSTimeInterval endTime;

    

    CGPoint touchBeginLocation;

    CGPoint touchMoveLocation;

    CGPoint touchEndLocation;

}


@property (nonatomic, assign) CGSize contentSize;

@property (nonatomic, assign) CGPoint contentOffset;

@property (nonatomic, assign) BOOL pagingEnabled;

@property (nonatomic, assign) BOOL scrollEnabled;


@end



//

#import "ScrollView.h"

@interface ScrollView()


- (void) setFrameOfContentViewWithPage:(float)page;

- (float) getValidPage:(float)page;

@end




@implementation ScrollView

@synthesize contentSize,contentOffset;

@synthesize scrollEnabled,pagingEnabled;


static CGPoint beginOffset;

const float percent=0.4;    //用来表示拖动百分比可以造成翻页或者边界反弹

- (id)initWithFrame:(CGRect)frame

{

    self = [super initWithFrame:frame];

    if (self

    {

        scrollEnabled=YES//是否可以滚动

        pagingEnabled=NO;   //是否可以翻页

        contentOffset=CGPointZero//contentview原点距离self原点距离,这儿取的负值

        contentSize=frame.size;     //存放图片有效范围frame是(0,0,contentsize.width,contentsize.height

        contentFrame=frame;         //selfframe

        totalPage=1;            //总的页数

        curPage=1;

        

        contentView=[[UIView alloc] initWithFrame:CGRectMake(contentOffset.x, contentOffset.y, frame.size.width, frame.size.height)];

        [super addSubview:contentView];

        [contentView release];

    }

    return self;

}


- (void) setContentSize:(CGSize)_contentSize    //最多存储图片大小

{

    contentSize=_contentSize;

    contentView.frame=CGRectMake(0, 0, contentSize.width, contentSize.height);

    totalPage=contentSize.width/contentFrame.size.width;

}


- (void) addSubview:(UIView *)view

{

    [contentView addSubview:view];

}


- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

{

    UITouch *touch=[touches anyObject];

    touchBeginLocation=[touch locationInView:self];

    beginTime=[touch timestamp];

    beginOffset=contentOffset;

}


- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event

{

    UITouch *touch=[touches anyObject];

    touchMoveLocation=[touch locationInView:self];

    float offsetX=touchMoveLocation.x-touchBeginLocation.x;

    float offsetY=touchMoveLocation.y-touchBeginLocation.y;

    if(self.frame.size.width>=contentSize.width)    offsetX=0.0;    //表示在水平方向不可以移动

    if(self.frame.size.height>=contentSize.height)  offsetY=0.0;    //表示竖直方向不可以移动

    contentOffset=CGPointMake(beginOffset.x+offsetX, beginOffset.y+offsetY);

    

    if(contentOffset.x>=contentFrame.size.width*percent || contentOffset.x<=-(contentSize.width*(totalPage-1)+contentFrame.size.width*percent))    //滚动到边缘

    {

        if(contentOffset.x>0)   curPage=1.0;

        if(contentOffset.x<0)   curPage=totalPage;

        [self setFrameOfContentViewWithPage:curPage];

    }

    

    contentView.frame=CGRectMake(contentOffset.x, contentOffset.y, contentSize.width, contentSize.height);

}


- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event

{

    UITouch *touch=[touches anyObject];

    endTime=[touch timestamp];

    touchEndLocation=[touch locationInView:self];

    if(contentOffset.x>0 || contentOffset.x<-contentFrame.size.width*(totalPage-1)) //滚动到边缘

    {

        if(contentOffset.x>0)   curPage=1.0;

        if(contentOffset.x<0)   curPage=totalPage;

        [self setFrameOfContentViewWithPage:curPage];

    }

    

    float pastTime=endTime-beginTime;

    float offsetX=touchEndLocation.x-touchBeginLocation.x;

    if(pagingEnabled)   //可以翻页

    {

        if(pastTime<=0.3)

        {

            if(offsetX>0.1 || offsetX<-0.1)     //表示发生了移动

            {

                if(offsetX>0.1) curPage=[self getValidPage:curPage-1];  //向右移动,向前翻页

                if(offsetX<-0.1)    curPage=[self getValidPage:curPage+1];  //向左移动,向后翻页

            }

        }

        else

        {

            if(offsetX>=self.frame.size.width*percent || offsetX<=-self.frame.size.width*percent)   

            {

                if(offsetX>0)   curPage=[self getValidPage:curPage-1];

                if(offsetX<0)   curPage=[self getValidPage:curPage+1];

            }

        }

        [self setFrameOfContentViewWithPage:curPage];

    }

}


- (void) touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event

{

   

}


- (float) getValidPage:(float)page

{

    NSLog(@"%f",page);

    curPage=page;

    if(page<1 curPage=1;

    if(page>totalPagecurPage=totalPage;

    if(page==totalPage-1)   curPage=(int)totalPage;

    return curPage;

}


- (void) setFrameOfContentViewWithPage:(float)page

{

    curPage=page;

    [UIView beginAnimations:nil context:nil];

    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];

    [UIView setAnimationDuration:0.2];

    contentOffset=CGPointMake(-(page-1)*contentFrame.size.width, 0);

    contentView.frame=CGRectMake(contentOffset.x, contentOffset.y, contentSize.width, contentSize.height);

    [UIView commitAnimations];

}



@end

继承UIView实现的简单UIScrollView,布布扣,bubuko.com

继承UIView实现的简单UIScrollView

标签:style   c   ext   color   a   int   

原文地址:http://blog.csdn.net/qi_ruihua/article/details/26361171

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!