gpt4 book ai didi

iphone - UIScrollView - 选择像 UIPickerView

转载 作者:行者123 更新时间:2023-11-28 23:10:38 26 4
gpt4 key购买 nike


我的 UIScrollView 有两个小问题。
我设法创建了 ScrollView ,它类似于选择器 View ,只是它是水平的。

问题 n.1 是 - 如何获取用户点击的号码?
问题 n.2 是 - 如何让 ScrollView 不停地旋转 - 永无止境?
一个问题 - 是否可以做一些“选择指标”?

这是我的代码:

numberArray = [NSArray arrayWithObjects:@"1", @"2", @"3", @"4", @"5", @"6", @"7", @"8", @"9", @"10", @"11", @"12", @"13", @"14", @"15", @"16", @"17", @"18", @"19", @"20", @"21", @"22", @"23", @"24", @"25", @"26", @"27", @"28", @"29", @"30", @"31", @"32", @"33", @"34", @"35", @"36", @"37", @"38", @"39", @"40", @"41", @"42", @"43", @"44", @"45", @"46", @"47", @"48", @"49", @"50", @"51", @"52", @"53", @"54", @"55", @"56", @"57", @"58", @"59", @"60", @"61", @"62", @"63", @"64", @"65", @"66", @"67", @"68", @"69", @"70", @"71", @"72", @"73", @"74", @"75", @"76", @"77", @"78", @"79", @"80", @"81", @"82", @"83", @"84", @"85", @"86", @"87", @"88", @"89", @"90", @"91", @"92", @"93", @"94", @"95", @"96", @"97", @"98", @"99", @"100", nil];

masterDividerView = [[UIView alloc] initWithFrame:CGRectMake(0, 480, 320, 44)];

morePeople = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 53)];
morePeople.backgroundColor = [UIColor scrollViewTexturedBackgroundColor];
morePeople.delegate = self;
[morePeople setBackgroundColor:[UIColor whiteColor]];

[morePeople setCanCancelContentTouches:NO];

morePeople.showsHorizontalScrollIndicator = NO;
morePeople.showsVerticalScrollIndicator = NO;
morePeople.clipsToBounds = NO;
morePeople.scrollEnabled = YES;
morePeople.pagingEnabled = NO;

NSUInteger nimages = 0;
NSInteger tot=0;
CGFloat cx = 0;
for (; ; nimages++) {
NSString *label = [numberArray objectAtIndex:nimages];

if (tot==99) {
break;
}
if (99==nimages) {
nimages=0;
}

UILabel *labelView = [[UILabel alloc] init];
labelView.text = label;
labelView.lineBreakMode = UILineBreakModeWordWrap;
labelView.numberOfLines = 0;
[labelView sizeToFit];
labelView.backgroundColor = [UIColor scrollViewTexturedBackgroundColor];


CGRect rect = labelView.frame;
rect.size.height = 53;
rect.size.width = 40;
rect.origin.x = cx;
rect.origin.y = 0;

labelView.frame = rect;

[morePeople addSubview:labelView];

cx += labelView.frame.size.width+5;
tot++;
}


self.pageControl.numberOfPages = nimages;
[morePeople setContentSize:CGSizeMake(cx, [morePeople bounds].size.height)];

[masterDividerView addSubview:morePeople];
[self.view addSubview:masterDividerView];

如果有人知道解决这个问题的好方法,我会很高兴!!!! :))

最佳答案

您的问题太宽泛,无法给出详细的答案,但是,是的,您所谈论的一切都可以通过非常简单的方式解决。

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

如果你想知道此时你的 UIScrollView 选择了什么元素,那么这不是问题:UIScrollView 总是知道它的位置(contentOffset),这让你很容易定义,选择了哪个对象(which object你现在正在使用)。

如果您想知道用户何时用他的手指点击您的“选择器 View ”(UIScrollView) 的元素之一,那么我会说这个问题的答案取决于您实际表示数据的方式(比如如果您在屏幕上一次查看 scrollView 的一个或多个元素)。但在任何情况下,您都可以使用 UITapGestureRecognizer 轻松解决这个问题。像:

// 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];

然后在您的选择器中以编程方式滚动它,您可以这样做:

- (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];
}
}

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

如果你知道计算scrollView元素序列中的下一个/上一个或随机元素的算法,就可以解决这个问题。基本上,我在我的一个项目中为自己解决了同样的问题(appStore 中的免费应用程序“IJCAI11”,您可以在 datePicker 的工作中看到任何选定 session 日期的详细信息:应用了无限算法 ScrollView ,我后来仅从设计角度应用的限制)。我在那里使用的方案很简单,虽然可能有点奇怪(在继续阅读之前,请注意,在我的例子中,当 scrollView 处于非滚动状态时,我在屏幕上只看到一个 scrollView 元素):

  1. 我创建了带有 3 个标签的 UIScrollView(如果您一次看到超过 1 个元素,根据您的具体情况,这可能是 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 ;)

希望这对您有所帮助!

关于iphone - UIScrollView - 选择像 UIPickerView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8374907/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com