ios - 如何根据 UIScrollview 内的按钮框架大小水平滚动 UIScrollView

标签 ios cocoa-touch uiscrollview uibutton frame

最初我需要在 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委托(delegate)方法中:

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 所以...您尝试执行的操作似乎有多种情况。
因此,我写这篇文章...并不是作为对您问题的完整回答,而是希望能帮助您走上正轨。

关于ios - 如何根据 UIScrollview 内的按钮框架大小水平滚动 UIScrollView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20917950/

相关文章:

ios - 当 UITableView 开始滚动时动画 View

iphone - 将 UIScrollView 嵌套在 UIScrollView Cocoa Touch 中

iphone - 手机的当前 IP 地址,无论是通过 WiFi 还是 3G 连接

iphone - 如何更高效地使用 UIPickerView?

ios - 单击 UITableViewCell 中的 UIbuttion 如何更改 uilabel 文本

ios - dequeueReusableCellWithIdentifier 为不同的 indexPath 返回相同的单元格

ios - 在 UICollectionViewCell 中处理 ScrollView 大小

iphone - 已将iPhone AdMob集成到已启动的App中

ios - subview Controller 中的导航堆栈

swift - 基于按下按钮在同一个 TableView Controller 中加载不同的数组