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

时间:2014-01-04 06:47:36

标签: 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委托方法:

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;
    }
}

我的要求是我要在UIButton s tableView中滚动`headerView。 因此,我采用了scrollView并动态放置UIButton

当我水平滚动scrollView时,它必须按照按钮的宽度水平移动。

当我滚动时,我还需要在scrollview上的某个点突出显示按钮标题。

1 个答案:

答案 0 :(得分:2)

如果您只想将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所以...似乎有多种场景与你想要做的事情 因此,我写了这个......不是对你的问题的完整答案,而是希望能帮助你走上正确的轨道。