如何利用uiscrollview的contentoffset属性计算出当前页
1个回答
推荐于2018-04-20 · 知道合伙人软件行家
关注
展开全部
#pragma mark - UIScrollView delegate
- (NSInteger)_getCurrentPageWithScrollView:(UIScrollView *)scrollView {
// 得到每页宽度
CGFloat pageWidth = scrollView.frame.size.width;
// 根据当前的x坐标和页宽度计算出当前页数
int currentPage = floor((scrollView.contentOffset.x - pageWidth/ 2) / pageWidth)+ 1;
return currentPage;
}
- (void)_showBackButton:(BOOL)show {
[UIView animateWithDuration:0.1 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
self.backButton.alpha = (show ? 1.0 : 0.0);
} completion:^(BOOL finished) {
}];
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
if (self.backButton.alpha) {
if ([self _getCurrentPageWithScrollView:scrollView] != 3) {
[self _showBackButton:NO];
}
}
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
if ([self _getCurrentPageWithScrollView:scrollView] == 3) {
[self _showBackButton:YES];
}
}
- (NSInteger)_getCurrentPageWithScrollView:(UIScrollView *)scrollView {
// 得到每页宽度
CGFloat pageWidth = scrollView.frame.size.width;
// 根据当前的x坐标和页宽度计算出当前页数
int currentPage = floor((scrollView.contentOffset.x - pageWidth/ 2) / pageWidth)+ 1;
return currentPage;
}
- (void)_showBackButton:(BOOL)show {
[UIView animateWithDuration:0.1 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
self.backButton.alpha = (show ? 1.0 : 0.0);
} completion:^(BOOL finished) {
}];
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
if (self.backButton.alpha) {
if ([self _getCurrentPageWithScrollView:scrollView] != 3) {
[self _showBackButton:NO];
}
}
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
if ([self _getCurrentPageWithScrollView:scrollView] == 3) {
[self _showBackButton:YES];
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询