溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

UIScrollView pagingEnabled自定義翻頁寬度

發(fā)布時間:2020-07-18 20:41:43 來源:網(wǎng)絡(luò) 閱讀:5659 作者:森林的守護(hù) 欄目:開發(fā)技術(shù)

PagingEnabled只能翻過整頁,下面幾個簡單的設(shè)置即可實現(xiàn)

技術(shù)點:

1. 創(chuàng)建一個繼承UIView的視圖,并設(shè)置clipsToBounds= YES

2. 添加一個UIscrollView控件,將其寬度設(shè)置為自定義翻頁的寬度

3. 設(shè)置UIScrollview 的clipsToBounds= NO

4. 確保本View的寬度大于UIScrollView的寬度用于顯示預(yù)覽內(nèi)容

5. 重寫本View的hittest方法,為了確保用戶滑動UIscrollview以外的空間時也可以觸發(fā)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問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI