2

我希望中间按钮比其他按钮高 5 px

就像假设当我滚动图库时,中间的按钮将比另一个按钮向上移动 5px 所以无论中间出现什么按钮,它都会比其他按钮向上移动一点点。所以请帮助我

        scrlview=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 350, 320, 130)];
        [scrlview setContentSize:CGSizeMake(600, 0)];
        scrlview.delegate=self;

            scrlview.backgroundColor=[UIColor blueColor];
            scrlview.scrollsToTop=NO;
            [self.view addSubview:scrlview];

            btn1=[UIButton buttonWithType:UIButtonTypeRoundedRect];
            btn1.frame=CGRectMake(10, 10, 55, 50);
            [btn1 setTitle:@"Btn1" forState:UIControlStateNormal];
            [scrlview addSubview:btn1];

            btn2=[UIButton buttonWithType:UIButtonTypeRoundedRect];
            btn2.frame=CGRectMake(70, 10, 55, 50);
            [btn2 setTitle:@"Btn2" forState:UIControlStateNormal];
            [scrlview addSubview:btn2];

            btn3=[UIButton buttonWithType:UIButtonTypeRoundedRect];
            btn3.frame=CGRectMake(130, 10, 55, 50);
            [btn3 setTitle:@"Btn3" forState:UIControlStateNormal];
            [scrlview addSubview:btn3];

            btn4=[UIButton buttonWithType:UIButtonTypeRoundedRect];
            btn4.frame=CGRectMake(190, 10, 55, 50);
            [btn4 setTitle:@"Btn4" forState:UIControlStateNormal];
            [scrlview addSubview:btn4];

            btn5=[UIButton buttonWithType:UIButtonTypeRoundedRect];
            btn5.frame=CGRectMake(250, 10, 55, 50);
            [btn5 setTitle:@"Btn5" forState:UIControlStateNormal];
            [scrlview addSubview:btn5];

            btn6=[UIButton buttonWithType:UIButtonTypeRoundedRect];
            btn6.frame=CGRectMake(310, 10, 55, 50);
            [btn6 setTitle:@"Btn6" forState:UIControlStateNormal];
            [scrlview addSubview:btn6];

            btn7=[UIButton buttonWithType:UIButtonTypeRoundedRect];
            btn7.frame=CGRectMake(370, 10, 55, 50);
            [btn7 setTitle:@"Btn7" forState:UIControlStateNormal];
            [scrlview addSubview:btn7];

            btn8=[UIButton buttonWithType:UIButtonTypeRoundedRect];
            btn8.frame=CGRectMake(430, 10, 55, 50);
            [btn8 setTitle:@"Btn8" forState:UIControlStateNormal];
            [scrlview addSubview:btn8];

            btn9=[UIButton buttonWithType:UIButtonTypeRoundedRect];
            btn9.frame=CGRectMake(490,10, 55, 50);
            [btn9 setTitle:@"Btn9" forState:UIControlStateNormal];
            [scrlview addSubview:btn9];
4

3 回答 3

4

我已经尝试过你的要求。我假设按钮之间的偏移量和它们的宽度是相同的。

 - (void)viewDidLoad
{
    [super viewDidLoad];

    self.scrollView.delegate = self;
    [self addButtonsToScrollView];
}

- (void)addButtonsToScrollView
{
    NSInteger numberOfButtons = 15;
    //NSInteger visibleButtons = 5;

    //CGFloat totalWidth = self.scrollView.frame.size.width;
    CGFloat offset = 5.0f;
    CGFloat buttonWidth = 54.0f;//(totalWidth - ((visibleButtons+1)*offset))/visibleButtons;
    CGFloat buttonHeight = 44.0f;

    CGPoint origin = CGPointMake(offset, 10.0f);

    for (int idx = 1; idx<=numberOfButtons; idx++) {

        UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        CGRect buttonFrame = CGRectZero;
        buttonFrame.origin = origin;
        buttonFrame.size = CGSizeMake(buttonWidth, buttonHeight);
        button.frame = buttonFrame;

        NSString *titleString = [NSString stringWithFormat:@"Title%d",idx+1];
        [button setTitle:titleString forState:UIControlStateNormal];
        button.tag = idx;

        [self.scrollView addSubview:button];

        origin.x+=buttonWidth+offset;
    }

    [self.scrollView setContentSize:CGSizeMake(origin.x, 200.0f)];
}
- (void)adjustCenterButton
{
    //content point is set in such a way that there would be exaclty 5 button visible all the time
    CGPoint contentOffset = self.scrollView.contentOffset;

    NSUInteger units = floorf(contentOffset.x/59.0f); //buttowidth+offset
    contentOffset.x = units*59.0f;
    [self.scrollView setContentOffset:contentOffset animated:YES];

    NSArray *subViews = [self.scrollView subviews];

    //Finds the tag of button which would be the center button
    //since the visible buttons are 5, 3 is added, 
    NSInteger selecteButtonIndex = floorf(contentOffset.x/(59.0f))+3;

    for (UIView *button in subViews)
    {
        //Just a bypass to remove the scroll indicator
        if (button.tag!=0)
        {
            CGRect frame = button.frame;
            frame.origin.y = (button.tag == selecteButtonIndex)?5.0f:10.0f;
            button.frame = frame;
        }
    }

    CGSize contentSize = self.scrollView.contentSize;
    contentSize.height = 200.0f;//self.scrollView.frame.size.width;
    self.scrollView.contentSize = contentSize;
}

#pragma mark - UIScrollViewDelegate

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    /*The update is only called when the scrollView is not decelerating.
    The method is an required if the user is scrolling very slowly,
    the other scrollViewDidEndDecelerating: will not be invoked*/
    if(!decelerate)
    {
        [self adjustCenterButton];
    }
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    [self adjustCenterButton];
}
于 2013-03-04T07:10:02.107 回答
4

您可以使用循环,例如 for 循环或 while 循环。让我给你一些想法。

在这里,我用示例代码更新了答案,更清楚

NSArray *yourArray = [[NSArray alloc] initWithObjects:@"btn1",@"btn2",@"btn3",@"btn4",@"btn5",@"btn6",@"btn7", nil];

int xPos = 10;
for (int i = 0 ; i < yourArray.count; i ++) {

    int yPos = 30;
    if (i == (yourArray.count /2)) {
        yPos = 25;
    }

    UIButton *button =[UIButton buttonWithType:UIButtonTypeRoundedRect];
    button.frame=CGRectMake(xPos, yPos, 55, 50);
    button.tag = i;
    [button setTitle:[yourArray objectAtIndex:i] forState:UIControlStateNormal];
    [scrollView addSubview:button];
    xPos+=55;
}

[scrollView setContentSize:CGSizeMake(xPos+10, 55)];

// To get Center Button

UIButton *centerButton = (UIButton *)[scrlview viewWithTag:(yourArray.count /2)];

可能这会对你有所帮助。一切顺利 !!!

于 2013-03-04T05:00:24.533 回答
2

Try this,

for(UIButton *subview in yourScrollView.subviews)
{
    CGRect subviewRect = [yourScrollView convertRect:subview.frame fromView:subview];

    if(CGRectContainsPoint(subviewRect,middlePointOfScrollView))
    {
        // change y position of button
        break;
    }
}
于 2013-03-04T05:41:51.050 回答