Ios 如何根据UIScrollView中的按钮帧大小水平滚动UIScrollView
最初,我需要在Ios 如何根据UIScrollView中的按钮帧大小水平滚动UIScrollView,ios,cocoa-touch,uiscrollview,uibutton,frame,Ios,Cocoa Touch,Uiscrollview,Uibutton,Frame,最初,我需要在滚动视图上添加一个默认按钮 然后,如果我在滚动视图上再添加一个按钮,第一个默认按钮应该向左移动。 我正在使用以下代码: int x = 0; for (int i = 0; i < pullscount ; i++) { NSString *myString = [pullsarray objectAtIndex:i]; CGSize stringsize = [myString sizeWithFont:[UIFont systemFontOfSize:1
滚动视图上添加一个默认按钮
然后,如果我在滚动视图
上再添加一个按钮,第一个默认按钮应该向左移动。
我正在使用以下代码:
int x = 0;
for (int i = 0; i < pullscount ; i++) {
NSString *myString = [pullsarray objectAtIndex:i];
CGSize stringsize = [myString sizeWithFont:[UIFont systemFontOfSize:16]];
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(x, 5, 100, 40)];
[button setTitle:[pullsarray objectAtIndex:i] forState:UIControlStateNormal];
[button setFont:[UIFont fontWithName:@"SegoeWP-Semibold" size:15.0]];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[button addTarget:self
action:@selector(buttonAction:)
forControlEvents:UIControlEventTouchUpInside];
[button setTag:190+i];
[topScrollView addSubview:button];
x += button.frame.size.width+30;
}
NSLog(@"%d",x);
topScrollView.contentSize = CGSizeMake(x+200, topScrollView.frame.size.height);
topScrollView.backgroundColor = [UIColor clearColor];
我的要求是在表格视图中滚动ui按钮
s`headerView
因此,我采用了滚动视图
,并动态地放置了ui按钮
s
当我水平滚动scrollView
时,它必须根据按钮的宽度水平移动
当我滚动时,我还需要在scrollview上的某个点突出显示按钮标题。如果您只想将滚动视图
滚动100px的宽度,请检查以下情况:
- (void)viewDidLoad {
//...
UIButton *btnGoRight = [UIButton buttonWithType:UIButtonTypeRoundedRect];\
[btnGoRight setTitle:@"Scroll Right" forState:UIControlStateNormal];
[btnGoRight addTarget:self
action:@selector(scrollMyScrollView)
forControlEvents:UIControlEventTouchUpInside];
[btnGoRight setFrame:CGRectMake(0, 0, self.view.frame.size.width, 30)];
[self.view addSubview:btnGoRight];
}
- (void)scrollMyScrollView {
if((topScrollView.contentOffset.x + topScrollView.frame.size.width) <= topScrollView.contentSize.width) {
CGPoint tempContentOffset = topScrollView.contentOffset;
tempContentOffset.x += 100; //to go right
[topScrollView setContentOffset: tempContentOffset animated:YES];
}
}
基本上,我们创建了两个按钮(在滚动视图
之外),其任务是将滚动视图
向右/向左滚动100px。
这是一个基本的概念,你可以在此基础上得到你想要的
PS:你的问题很模糊(至少对我来说),不清楚你打算如何滚动(在滑动或点击按钮或其他东西时),或者在滚动视图中(大致)有多少个按钮,所以。。。您尝试执行的操作似乎有多个场景。
因此,我写了这个。。。这不是对您问题的完整回答,但希望能帮助您走上正确的道路。您希望gajendra在ScrolView上添加水平滚动,并删除垂直滚动我的要求是在uitableheader视图上滚动uibuttons。为此,我采用了我动态放置的滚动视图按钮。当我水平滚动scrollview时,它必须根据按钮宽度水平移动。当我滚动时,我需要在scrollview上的某个点突出显示按钮标题…所以,基本上。。。你想要1。滚动视图中的许多按钮(水平放置)2。水平滚动3。所有这些都作为一个UITableView
的headerView
4。突出显示/加粗按钮标题。对吗?如何根据按钮文本宽度水平移动scrollview。为什么要在scrollViewDidScroll:
中执行这些操作?还有,你到底想要什么。对不起,我不明白你的意思。
- (void)viewDidLoad {
//...
UIButton *btnGoRight = [UIButton buttonWithType:UIButtonTypeRoundedRect];\
[btnGoRight setTitle:@"Scroll Right" forState:UIControlStateNormal];
[btnGoRight addTarget:self
action:@selector(scrollMyScrollView)
forControlEvents:UIControlEventTouchUpInside];
[btnGoRight setFrame:CGRectMake(0, 0, self.view.frame.size.width, 30)];
[self.view addSubview:btnGoRight];
}
- (void)scrollMyScrollView {
if((topScrollView.contentOffset.x + topScrollView.frame.size.width) <= topScrollView.contentSize.width) {
CGPoint tempContentOffset = topScrollView.contentOffset;
tempContentOffset.x += 100; //to go right
[topScrollView setContentOffset: tempContentOffset animated:YES];
}
}
if((topScrollView.contentOffset.x + topScrollView.frame.size.width) >= 100) {
//...
tempContentOffset.x -= 100; //to go left
//...
}