[英]how to point to a Button position based on tag value on horizontal UIScrollView
Hi fiends I'm new to ios app dev. 嗨,朋友们,我是iOS应用开发的新手。 I've struck with simple issue that in my app I added UIScrollView that scrolls horizontally.
我遇到了一个简单的问题,即在我的应用程序中添加了可水平滚动的UIScrollView。 I've added buttons to that scrollview that will be dynamically changes.
我在滚动视图中添加了将动态更改的按钮。 the code is
该代码是
Step 1:
scroll = [[UIScrollView alloc]init];
scroll.delegate=self;
scroll.frame = CGRectMake(0, 0, 320, (height+25));
[scroll setContentSize:CGSizeMake((width-30)*n, height+25)];
scroll.backgroundColor=[UIColor clearColor];
scroll.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"GP-BG.png"]];
scroll.showsHorizontalScrollIndicator=NO;
scroll.alwaysBounceHorizontal=NO;
for(int i=0;i<n;i++)
{
MODELRoom *room = [self.resultSet.dataObjectList objectAtIndex:i];
CGRect rectForTitleButton = CGRectMake(i*(width-30), 0, width-30, height+25);
int buttonTag =10000+i;
UIButton *titleButton = [self getRoomButton:room tag:buttonTag];
titleButton.tag=buttonTag;
[titleButton setFrame:rectForTitleButton];
titleButton.backgroundColor=[UIColor clearColor];
[scroll addSubview:titleButton];
[controlButtons addObject:titleButton];
}
}
step 2:
- (UIButton*)getRoomButton:(MODELRoom *)currenRoom tag:(int)tagValue{
UIButton *button=[[UIButton alloc] init];
[button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
UIImage *buttonImageNormal;
if(currenRoom.currentlySelected){
buttonImageNormal=[UIImage imageNamed:[NSString stringWithFormat:@"GP-BG-Slected Green.png"]];
}else{
buttonImageNormal=[UIImage imageNamed:[NSString stringWithFormat:@""]];
}
[button setBackgroundImage:buttonImageNormal forState:UIControlStateNormal];
[gridViewObjects addObject:button];
return button;
}
step 3:
- (void)buttonClicked:(UIButton *)sender{
if((int)[sender tag]>=10000 && (int)[sender tag]<20000){
currentRoom=((MODELRoom*)[resultSet.dataObjectList objectAtIndex:[sender tag]-10000]);
[self roomSelectionChanged:currentRoom];
currentRoom.currentlySelected=true;
scrollStrech=(width-30)*([sender tag]-10000);
scroll.contentOffset=CGPointMake(scrollStrech, 0);
NSLog(@"Scroll Strech=%d",scrollStrech);
for(int i=0;i<[resultSet.dataObjectList count];i++){
if(![[resultSet.dataObjectList objectAtIndex:i] isEqual:currentRoom]){
((MODELRoom*)[resultSet.dataObjectList objectAtIndex:i]).currentlySelected=false;
}
}
[self refresh];
}
step 4:
-(void)refresh{
if([self.scroll isDescendantOfView:self.view]){
[self.scroll removeFromSuperview];
}
}
Every thing is working fine but when I select the fifth or sixth button that is visible after scrolling and if I select that button the scroll view is rebouncing to first position. 一切正常,但是当我选择滚动后可见的第五个或第六个按钮时,并且如果选择了该按钮,则滚动视图会跳回到第一个位置。 I found that this is coz of removing from superview and loading it again.
我发现这是从超级视图中删除并再次加载它的原因。 if that the case also I need to show the scrollview region at that selected button region only.
如果是这种情况,我也只需要在该选定按钮区域显示滚动视图区域即可。
kindly suggest me how to overcome this.... 请给我建议如何克服这个问题。
Try this on buttonClicked 在buttonClicked上尝试
CGRect rect= CGRectMake(sender.frame.origin.x, sender.frame.origin.y , sender.frame.size.width, sender.frame.size.height);
[scrollView scrollRectToVisible:rect animated:YES];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.