继承UIView实现的简单UIScrollView

#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;         //self的frame

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>totalPage) 
curPage=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

时间: 2024-12-20 22:36:08

继承UIView实现的简单UIScrollView的相关文章

ios 继承UIView实现自定义视图——实现画图

主要的原理包括: 继承UIView ,重载drawrect和重载触摸事件 待实现的功能还有,路径数组保存等. 用可变数据保存path路径 画曲线是通过二次贝塞尔曲线实现的 这里可以得到画图的UIImage对象 UIGraphicsBeginImageContext(self.bounds.size); [self.layer renderInContext:UIGraphicsGetCurrentContext()]; UIImage *result=UIGraphicsGetImageFrom

c语言中继承和多态的简单实现

C语言本身是不支持继承和多态的,但其实在 C 的世界里,有一套非常有名的面向对象的框架,用的也非常广,那就是 GObject,它是整个图形界面开发库 GTK 的基石,在IBM developerWorks上有一篇很好的文章介绍 GObject<GObject对象系统>.另外,在 Linux 内核里面也大量使用了面向对象的思想,比如虚拟文件系统,设备驱动等模块,在 lwn上有两篇文章就讲到了内核中的面向对象,详细请看:<Object oriented design patterns in

Swift - 继承UIView实现自定义可视化组件(附记分牌样例)

在iOS开发中,如果创建一个自定义的组件通常可以通过继承UIView来实现.下面以一个记分牌组件为例,演示了组件的创建和使用,以及枚举.协议等相关知识的学习. 效果图如下: 组件代码:ScoreView.swift 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 5

工程日记之HelloSlide(1):Swift自定义可视化组件的方法(继承UIView和在StoryBoard中设置)

需求描述 HelloSlide是把文本自动转化成幻灯片的软件,在幻灯片中我们有SmartArt:各种各样的几何形状,并且可以自定义大小和颜色,放在幻灯片不同的位置. 为了在我们的软件中实现类似的效果,我封装了一些自定义的组件,因为暂时只需要几何形状,我通过直接继承UIView来实现 代码 class ArcView:UIView{ var mystrokecolor:UIColor //设置笔触颜色 var color : UIColor //设置填充颜色 init(frame:CGRect,c

php类的封装、继承和多态的简单理解

.面象对向的三大特点:封装性.继承性.多态性 首先简单理解一下抽象: 我们在前面定义一个类的时候,实际上就是把一类事物共有的属性和行为提取出来,形成一个物理模型(模版),这种研究问题的方法称为抽象 一.封装性  封装就是把抽取出来的数据和对数据的操作封装在一起,数据被保护在内部,程序的其他部分只有被授权的操作(方法)才能对数据进行操作.  php提供了三种访问控制修饰符  public 表示全局,本类内部,类外部,子类都可以访问  protected 表示受保护的,只有本类或子类可以访问  pr

[js高手之路]面向对象+设计模式+继承一步步改造简单的四则运算

到目前为止,我已经写完了面向对象完整的一个系列知识,前面基本属于理论,原理的理解,接下来,我们就用学到的知识来实战下吧. 看看理解原理和理论是否重要?例子从简单到复杂 一.单体(字面量)封装加减乘除 var Oper = {             add : function( n1, n2 ){                 return n1 + n2;             },             sbb : function( n1, n2 ){               

继承UIView写自定义button

#import <UIKit/UIKit.h> @interface LPButton : UIView @property (nonatomic,strong) id target; @property (nonatomic,assign) SEL action; - (void)addTarget:(id)target action:(SEL)action; @end // // LPButton.m // loopdiner // // Created by yl on 15/12/8.

JavaSE8基础 继承一个抽象类的简单示例

os :windows7 x64    jdk:jdk-8u131-windows-x64    ide:Eclipse Oxygen Release (4.7.0)        代码: abstract class Person { //姓名 private String name; //年龄 private int age; public void setName(String name) { this.name = name; } public String getName() { re

iOS 8:一种自定义键盘的实现方法

关键字:自定义键盘 类似QQ那种带表情键盘的实现思路为,继承UIView,然后嵌入UIScrollView或UICollectionView作分区,每个分区视图加入UIButton,UIButton图片设置成相应的表情图片.最后将派生类实例赋值给UITextField的inputView属性. 总结:内容太少,不总结.