码迷,mamicode.com
首页 > 其他好文 > 详细

UIScrollView和UIPageControl结合使用

时间:2016-05-23 19:00:40      阅读:126      评论:0      收藏:0      [点我收藏+]

标签:

@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结合使用

标签:

原文地址:http://www.cnblogs.com/happyEveryData/p/5520890.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!