[英]How to scroll a UIScrollView horizontally based on the buttons frame size inside the UIScrollview
最初我需要在scrollView
上添加一個默認按鈕。
然后,如果我在scrollView
上再添加一個按鈕,則第一個默認按鈕應該向左移動。
我正在使用此代碼:
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];
在scrollViewDidScroll
委托方法中:
NSLog(@"%f",button.frame.size.width);
CGFloat pageWidth = button.frame.size.width;
NSLog(@"%f",pageWidth);
if (topScrollView.contentSize.width>pageWidth) {
float stopx = 100.0f;
if (topScrollView.contentSize.width >= stopx) {
CGPoint stop = topScrollView.contentOffset;
NSLog(@"Value – %@", NSStringFromCGPoint(stop));
stop.x = stopx;
[topScrollView setContentOffset:stop animated:YES];
}
}
else if (topScrollView.contentOffset.x >= 110.0f && topScrollView.contentOffset.x <= 220.0f) {
float stopx = 210.0f;
CGPoint stop1 = topScrollView.contentOffset;
stop1.x = stopx;
[topScrollView setContentOffset:stop1 animated:YES];
topScrollView.contentOffset = stop1;
}
}
我的要求是我想在tableView
的`headerView
滾動UIButton
。
因此我采用了scrollView
並動態放置了UIButton
。
當我水平滾動scrollView
,它必須按照按鈕的寬度水平移動。
當我滾動時,我還需要在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];
}
}
使用另一個按鈕(例如, *btnGoLeft
)和此條件語句:
if((topScrollView.contentOffset.x + topScrollView.frame.size.width) >= 100) {
//...
tempContentOffset.x -= 100; //to go left
//...
}
基本上,我們創建了2個按鈕( 在scrollView
之外 ),其任務是簡單地向左/向左滾動scrollView
100px。
這是基本概念,您可以在此基礎上獲得您想要的東西
PS:你的問題很模糊(至少對我來說)並且不清楚你打算如何滾動(在滑動或點擊按鈕或其他東西上)或滾動scrollView
你將有多少按鈕(大致)...你想要做的事情似乎有多種情況。
因此,我寫了這個......並不是對你的問題的完整答案,但希望能幫助你走上正軌。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.