UIScrollView pagingEnabled自定义翻页宽度
PagingEnabled只能翻过整页,下面几个简单的设置即可实现
技术点:
1. 创建一个继承UIView的视图,并设置clipsToBounds= YES
2. 添加一个UIscrollView控件,将其宽度设置为自定义翻页的宽度
3. 设置UIScrollview 的clipsToBounds= NO
4. 确保本View的宽度大于UIScrollView的宽度用于显示预览内容
5. 重写本View的hittest方法,为了确保用户滑动UIscrollview以外的空间时也可以触发UIscrollview滑动
ok! 下面是代码,为了方便,使用图片作为显示的每一页
#definekLJItemWidth240
@implementationMyScrollview {
UIScrollView*scrollview;
}
-(id)initWithFrame:(CGRect)frame{
self=[superinitWithFrame:frame];
if(self){
scrollview=({
UIScrollView*scroll=[[UIScrollViewalloc]initWithFrame:CGRectMake(40,0,kLJItemWidth,frame.size.height)];
scroll.pagingEnabled=YES;
scroll.clipsToBounds=NO;
scroll;
}) ;
[selfaddSubview:scrollview];
self.clipsToBounds=YES;
}
returnself;
}
-(void)loadImages:(NSArray*)array{
intindex=0;
[scrollview.subviewsmakeObjectsPerformSelector:@selector(removeFromSuperview)];
for(NSString*nameinarray){
UIImageView*iv=[[UIImageViewalloc]initWithImage:[UIImagep_w_picpathNamed:name]];
iv.contentMode=UIViewContentModeScaleToFill;
CGRectfra=iv.frame;
fra.size.width=kLJItemWidth;
fra.origin.x=index*kLJItemWidth;
iv.frame=fra;
[scrollviewaddSubview:iv];
index++;
}
scrollview.contentSize=CGSizeMake(scrollview.frame.size.width*index,scrollview.frame.size.height);
}
-(UIView*)hitTest:(CGPoint)pointwithEvent:(UIEvent*)event
{
UIView*view=[superhitTest:pointwithEvent:event];
if([viewisEqual:self])
{
for(UIView*subviewinscrollview.subviews)
{
CGPointoffset=CGPointMake(point.x-scrollview.frame.origin.x+scrollview.contentOffset.x-subview.frame.origin.x,
point.y-scrollview.frame.origin.y+scrollview.contentOffset.y-subview.frame.origin.y);
if((view=[subviewhitTest:offsetwithEvent:event]))
{
returnview;
}
}
returnscrollview;
}
returnview;
}
@end
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。