Ios 为所有UIScrollView的子类提供标记到平移的手势
在我的应用程序中,我有一个UIScrollView,在其中动态放置UIButtons。我给UIScrollView中的所有按钮添加了标签。我需要在UIScrollView中的每个按钮上进行平移手势。但问题是,只有最后一个按钮得到平移手势,而不是所有的按钮 我的代码:Ios 为所有UIScrollView的子类提供标记到平移的手势,ios,iphone,uiview,uiscrollview,uigesturerecognizer,Ios,Iphone,Uiview,Uiscrollview,Uigesturerecognizer,在我的应用程序中,我有一个UIScrollView,在其中动态放置UIButtons。我给UIScrollView中的所有按钮添加了标签。我需要在UIScrollView中的每个按钮上进行平移手势。但问题是,只有最后一个按钮得到平移手势,而不是所有的按钮 我的代码: for (int i =0; i<[arrayForTitles count]; i++) { view = [[UIView alloc] init]; [view setBackgroundC
for (int i =0; i<[arrayForTitles count]; i++)
{
view = [[UIView alloc] init];
[view setBackgroundColor:[UIColor colorWithRed:255.0/255.0 green:255.0/255.0 blue:255.0/255.0 alpha:0.85]];
view.tag=i;
view.frame = CGRectMake(0, varForTitleViewHeight, scrView.frame.size.width, 50);
[scrView addSubview:view];
//************** view when pan gesture done ********************
viewForPanGesture = [[UIView alloc] init];
[viewForPanGesture setBackgroundColor:[UIColor colorWithRed:0.0/255.0 green:161.0/255.0 blue:255.0/255.0 alpha:1.0]];
viewForPanGesture.tag=i;
viewForPanGesture.frame = CGRectMake(100, varForTitleViewHeight, 151, view.frame.size.height);
[scrView addSubview:viewForPanGesture];
btnOnScrollTitle = [UIButton buttonWithType:UIButtonTypeCustom];
btnOnScrollTitle.frame = CGRectMake(0,varForTitleViewHeight,scrView.frame.size.width, 50);
[btnOnScrollTitle setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
btnOnScrollTitle.tag = i;
[btnOnScrollTitle addTarget:self action:@selector(blackViewMethod:) forControlEvents:UIControlEventTouchUpInside];
[scrView addSubview:btnOnScrollTitle];
panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];
[panGesture setDelegate:self];
[panGesture setMaximumNumberOfTouches:1];
[btnOnScrollTitle addGestureRecognizer:panGesture];
varForTitleViewHeight = varForTitleViewHeight+51;
}
现在,如何为UIScrollView的每个对象提供平移手势,而不仅仅是最后一个对象
任何想法、代码、教程或链接都会很有帮助。代码中没有错误,不需要任何修改。您只需以错误的方式访问它们即可。 这是它的代码
-(void)handlePan:(UIGestureRecognizer *) sender
{
if ((UIButton *)[sender.view viewWithTag:0]) //where no. '0' tag of button.;
{
// code it
}
if ((UIButton *)[sender.view viewWithTag:1])
{
// code it
}
}
是的,但是兄弟,这是静态视图的。我在UIScrollView中有动态视图。它可以是包含100个视图,也可以是1000个视图。因此,需要区分所有视图的所有平移手势。如果相似,可以对其使用任何循环函数。我只是在平移手势出现时滑动另一个小视图,就像在表视图的默认编辑单元格功能中一样。您的答案不适用于动态视图。您的代码工作不完美。。。。对于动态视图,我使用if UIButton*[sender.view viewWithTag:sender.view.tag]{}它将为我提供视图手势的确切标记。在那之后,需要一些合乎逻辑的整理。我接受你的回答,因为你给了我一些暗示。快乐编码。。。。