UIScrollView和UIPageControl结合使用

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了UIScrollView和UIPageControl结合使用相关的知识,希望对你有一定的参考价值。

@interface YSWeiBoExpressionView ()<UIScrollViewDelegate>
/** 表情scrollecView */
@property (nonatomic, weak) UIScrollView *expressionScrollView;
/** pageControll */
@property (nonatomic, weak) UIPageControl *expressionPageControl;
@end

 

#pragma mark - Controll
- (UIScrollView *)expressionScrollView
{
    if (!_expressionScrollView)
    {
        UIScrollView *expressionScrollView = [[UIScrollView alloc] init];

        expressionScrollView.backgroundColor = [UIColor grayColor];
        
        expressionScrollView.opaque = YES;
        expressionScrollView.pagingEnabled = YES;
        expressionScrollView.showsHorizontalScrollIndicator = NO;
        expressionScrollView.showsVerticalScrollIndicator = NO;
        expressionScrollView.delegate = self;

        self.expressionScrollView = expressionScrollView;
        [self addSubview:expressionScrollView];
    }
    return _expressionScrollView;
}

- (UIPageControl *)expressionPageControl
{
    if (!_expressionPageControl)
    {
        UIPageControl *expressionPageControl = [[UIPageControl alloc] init];

        expressionPageControl.backgroundColor = [UIColor yellowColor];
        expressionPageControl.pageIndicatorTintColor = [UIColor whiteColor];
        expressionPageControl.currentPageIndicatorTintColor = [UIColor orangeColor];

        expressionPageControl.userInteractionEnabled = NO;
        expressionPageControl.hidesForSinglePage = YES; // 单页隐藏
        
        self.expressionPageControl = expressionPageControl;
        [self addSubview:expressionPageControl];
    }
    return _expressionPageControl;
}

 

#pragma mark - Layouts
- (void)layoutSubviews
{
    [super layoutSubviews];
    
    self.expressionPageControl.width = self.width;
    self.expressionPageControl.height = 35;
    self.expressionPageControl.x = 0;
    self.expressionPageControl.y = self.height - self.expressionPageControl.height;
    
    self.expressionScrollView.width = self.width;
    self.expressionScrollView.height = self.expressionPageControl.y;
    self.x = 0;
    self.y = 0;
    
    NSInteger pageCount = self.expressionScrollView.subviews.count;
    
    for (int i = 0; i < pageCount; i++)
    {
        YSWeiBoExpressionEmojiView *emotionsPageView = self.expressionScrollView.subviews[i];
        
        emotionsPageView.width = self.expressionScrollView.width;
        emotionsPageView.height = self.expressionScrollView.height;
        emotionsPageView.x = emotionsPageView.width * i;
        emotionsPageView.y = 0;
    }
    
    self.expressionScrollView.contentSize = CGSizeMake(pageCount * self.expressionScrollView.width, 0);
    

}

#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    double pageNumber = scrollView.contentOffset.x / scrollView.frame.size.width;
    self.expressionPageControl.currentPage = (int)(pageNumber + 0.5);
}

以上是关于UIScrollView和UIPageControl结合使用的主要内容,如果未能解决你的问题,请参考以下文章

UIScrollView

UIScrollView 内 UIScrollView 内 UIScrollView

ios: uiscrollview 和 uiview 自动布局

如何使在Swift中使用UIScrollView进行滚动和缩放

UICollectionViewCell 和 UIScrollView

UIScrollView和UIPageControl结合使用