我想创建可拖动的标尺 - 当拖动增加上面的计数器时。
我知道我需要使用UIScrollView。但是我不确定当滚动视图向左拖动时如何增加计数器,并在向右拖动时减少计数器?有没有办法衡量这个?
在美学上,我还希望UIScrollView成为"卷尺样式"拖动时会不断重复。从理论上讲,我需要不断重复一个图像,但在概念上我并不确定。可能有更好的方法吗?
答案 0 :(得分:1)
要知道您所在的页面,增加或减少计数器,您可以使用以下方式知道页码:
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
CGFloat ratio = scrollView.contentOffset.x/scrollView.frame.size.width;
self.pageControl.currentPage = (int)floor(ratio); // page number
}
要检测滚动方向,您可以使用:
//somewhere in the header
@property (nonatomic, assign) CGFloat lastContentOffset;
@property (nonatomic, assign) int counter;
//
- (void)scrollViewDidScroll:(UIScrollView *)sender
{
ScrollDirection scrollDirection;
if (self.lastContentOffset > scrollView.contentOffset.x)
scrollDirection = ScrollDirectionRight;
else if (self.lastContentOffset < scrollView.contentOffset.x)
scrollDirection = ScrollDirectionLeft;
self.lastContentOffset = scrollView.contentOffset.x;
// do whatever you need to with scrollDirection here.
self.counter = (scrollDirection == ScrollDirectionRight) ? self.counter++ : self.counter--;
}
见post