温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

UIScrollView pagingEnabled自定义翻页宽度

发布时间:2020-07-18 20:41:43 来源:网络 阅读:5639 作者:森林的守护 栏目:开发技术

PagingEnabled只能翻过整页,下面几个简单的设置即可实现

技术点:

1. 创建一个继承UIView的视图,并设置clipsToBounds= YES

2. 添加一个UIscrollView控件,将其宽度设置为自定义翻页的宽度

3. 设置UIScrollview 的clipsToBounds= NO

4. 确保本View的宽度大于UIScrollView的宽度用于显示预览内容

5. 重写本View的hittest方法,为了确保用户滑动UIscrollview以外的空间时也可以触发UIscrollview滑动


ok! 下面是代码,为了方便,使用图片作为显示的每一页



#define kLJItemWidth 240


@implementation MyScrollview    {


    UIScrollView *scrollview;

}


- (id)initWithFrame:(CGRect)frame {

    self = [super initWithFrame:frame];

    if (self) {

          scrollview = ({

              UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(40, 0, kLJItemWidth, frame.size.height)];

              scroll.pagingEnabled = YES;

              scroll.clipsToBounds = NO;

              scroll;

          })                                                                                      ;


        [self addSubview:scrollview];

                            self.clipsToBounds = YES;

    }


    return self;

}


-(void)loadImages:(NSArray *)array{

    int index = 0;

    [scrollview.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];


    for(NSString * name in array){

        UIImageView *iv = [[UIImageView alloc] initWithImage:[UIImage p_w_picpathNamed:name]];

        iv.contentMode = UIViewContentModeScaleToFill;

        CGRect fra = iv.frame;

        fra.size.width = kLJItemWidth;

        fra.origin.x = index * kLJItemWidth;

        iv.frame = fra;


        [scrollview addSubview:iv];

        index++;

    }

    scrollview.contentSize = CGSizeMake(scrollview.frame.size.width*index, scrollview.frame.size.height);

}


- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event

{

    UIView *view = [super hitTest:point withEvent:event];

    if ([view isEqual:self])

    {

        for (UIView *subview in scrollview.subviews)

        {

            CGPoint offset = 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 = [subview hitTest:offset withEvent:event]))

            {

                return view;

            }

        }

        return scrollview;

    }

    return view;

}


@end


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI