且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

UIScrollView-像UIPickerView这样的选择

更新时间:2023-12-04 12:39:04

您的问题太大了,无法给出详细的答案,但是,是的,您正在谈论的所有内容都可以通过非常简单的方式解决.

Your question is too wide to make a detailed answer, but yes, everything of what you're talking about can be solved in a quite easy way.

问题n.1是-如何获取用户点击的电话号码?

The problem n.1 is - How do I get the number which the user tapped?

如果您想知道此时UIScrollView选择了哪个元素,那么这不是问题:UIScrollView始终知道其位置(contentOffset),这使您可以轻松定义选择哪个对象(哪个对象)您正在与之合作).

If you mean to know what element is chosen by your UIScrollView at the moment, then it's not a problem: UIScrollView always knows its position (contentOffset), which gives you the easy possibility to define, which object is chosen (which object you're working with) now.

如果您想知道用户何时用手指轻按选择器视图"(UIScrollView)的元素之一,那么我想说的答案取决于您实际表示数据的方式(例如,如果您一次在屏幕上看到一个或几个scrollView元素).但是无论如何,您都可以使用UITapGestureRecognizer轻松解决此问题.像这样:

If you mean to know when the user tapped by his finger one of the elements of your "picker view" (UIScrollView), then I would say that the answer to this depends on how you actually represent your data (like if you see one or several elements of your scrollView at a time on your screen). But in any case you can easily solve this by using UITapGestureRecognizer. Smth like:

// add gesture recognizers to the scroll view 
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];
[singleTap setNumberOfTapsRequired:1];
[self.yourScrollView addGestureRecognizer:singleTap];
[singleTap release];

然后在选择器中以编程方式滚动它,就像:

And then to scroll it programmatically in your selector you do smth like:

- (void)handleSingleTap:(UIGestureRecognizer *)gestureRecognizer
{
    CGPoint pointOfTouch = [gestureRecognizer locationInView:self.view];
    if ( (pointOfTouch.x > self.rightArrowMin) && (pointOfTouch.x < self.rightArrowMax) ) {
        [scrollView setContentOffset:CGPointMake(lastOffset + self.lengthOfLabel, 0) animated:YES];
    } else if ( (pointOfTouch.x > self.leftArrowMin) && (pointOfTouch.x < self.leftArrowMax) ) {
        [scrollView setContentOffset:CGPointMake(lastOffset - self.lengthOfLabel, 0) animated:YES];
    }
}

问题n.2是-如何使滚动视图不断旋转-永无止境?

The problem n.2 is - How do I make the scrollview to go round and round - never-ending?

如果您知道在scrollView的元素序列中计算下一个/上一个或随机元素的算法,就可以解决.基本上,我在自己的一个项目(appStore中的免费应用程序"IJCAI11")中为自己解决了同一件事,在datePicker的工作中,您可以看到所选会议日期的详细信息:应用了无限算法滚动视图,我以后仅从设计角度考虑的限制).我在这里使用的方案很简单,尽管可能有点怪异(在继续阅读之前,请注意,在我的情况下,当scrollView处于非滚动状态时,我在屏幕上仅看到一个scrollView元素):

It can be solved if you know the algorithm of calculating the next/previous or random element in the sequence of elements of your scrollView. Basically, I solved this same thing for myself in one of my projects (free app "IJCAI11" in the appStore, there you can see at the work of a datePicker in the details of any chosen conference's day: there is applied the algorithm of infinite scrolling view, the limits I applied later only from design point of view). The scheme I use there is simple, though perhaps a bit weird (before reading ahead, note that in my case I see only one element of scrollView on the screen, when the scrollView is in nonscrolling state):

  1. 我用3个标签创建UIScrollView(以防您一次看到多个元素,根据您的情况,它们可能像5、7,...,2N + 1);
  2. 激活第二个(中间)标签.
  3. 为所有3个(2N + 1)标签分配适当的值.在此步骤中,您的第二个(中间)标签将包含(将显示)默认值的数据.
  4. 当用户向左/向右滚动一步(到位置M_pos)时,您将以标准方式进行操作,因为与中心标签相邻的地方包含正确的数据(M).
  5. 然后,您将这个新的有效值(M)应用于中间标签(该标签将在后台显示,因此用户将不会在屏幕上看到此标签),然后重新计算所有其他标签()...,M-1,M,M + 1,...),包括您的M_pos位置(!).
  6. 在"animated:NO"模式下,您可以将scrollView移到中心位置.在这种情况下,用户什么也看不到,而实际上他的scrollView已从位置M_pos移动到中心位置.所有相邻标签都已经过重新计算,您可以随时重复第4点,从而使用户产生强烈的感觉,他可以使用带有大量元素的scrollView(实际上,您只需要使用3(2N + 1)个标签,每次移动后更改其内容.

一个问题-是否可以做一些选择指标"?

A question - Is it possible to make some "selection indicator"?

如果我正确理解了这个问题,那么就考虑一下View/Subview,尤其是View类的addSubview函数;)

If I understand this question correctly, then just consider the View/Subview thing and in particular the function addSubview of View class ;)

希望这会有所帮助!