单击时将 UiScrollView 元素置于中心

Bringing UiScrollView element to center when clicked

我有的是 UIScrollView,里面有不同大小的按钮。

- (void)viewDidLoad {
    [super viewDidLoad];

    myScrollView.frame = CGRectMake(0, 100, 375, 50.0);

    int totalButtonsToAdd = 20;

    startX = 0;
    int buttonWidth =  60;
    for (int i=0;i<totalButtonsToAdd;i++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button addTarget:self
                   action:@selector(aMethod:)
         forControlEvents:UIControlEventTouchUpInside];
        [button setTitle:[NSString stringWithFormat:@"Btn-%d", i] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
        button.accessibilityValue = [NSString stringWithFormat:@"%d", i+1];
        button.frame = CGRectMake(startX, 0, buttonWidth, 50.0);
        [[button layer] setBorderWidth:2.0f];
        [[button layer] setBorderColor:[UIColor greenColor].CGColor];
        button.layer.cornerRadius = 5;
        startX = startX + buttonWidth + 5;
        buttonWidth = buttonWidth + 5;
        [myScrollView addSubview:button];
    }

    NSLog(@"startX===%d", startX);

    myScrollView.contentSize = CGSizeMake(startX, 50.0);
}

-(IBAction)aMethod:(id)sender {
    UIButton *mButton = (UIButton *)sender;
    NSLog(@"clicked button index is %@", mButton.accessibilityValue);
}

我想做的是当我点击任何按钮时,我想把那个按钮放在中间。为此,我相信我需要将滚动视图滚动到特定位置。

为此我所做的如下。

- (void)viewDidLoad {
    [super viewDidLoad];

    myScrollView.frame = CGRectMake(0, 100, 375, 50.0);

    int totalButtonsToAdd = 20;

    startX = 0;
    int buttonWidth =  60;
    for (int i=0;i<totalButtonsToAdd;i++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button addTarget:self
                   action:@selector(aMethod:)
         forControlEvents:UIControlEventTouchUpInside];
        [button setTitle:[NSString stringWithFormat:@"Btn-%d", i] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
        button.accessibilityValue = [NSString stringWithFormat:@"%d", i+1];
        button.frame = CGRectMake(startX, 0, buttonWidth, 50.0);
        [[button layer] setBorderWidth:2.0f];
        [[button layer] setBorderColor:[UIColor greenColor].CGColor];
        button.layer.cornerRadius = 5;
        startX = startX + buttonWidth + 5;
        NSLog(@"startX===%d==%d", i, startX);
        buttonWidth = buttonWidth + 5;
        [myScrollView addSubview:button];
    }

    NSLog(@"startX final===%d", startX);

    myScrollView.contentSize = CGSizeMake(startX, 50.0);
}

-(IBAction)aMethod:(id)sender {
    UIButton *mButton = (UIButton *)sender;
    NSLog(@"clicked button index is %@", mButton.accessibilityValue);
    focusPosition = 0;
    int totalButtonsToAdd = 20;
    int buttonWidth = 60;
    startX = 0;
    for (int i=0;i<totalButtonsToAdd;i++) {
        startX = startX + buttonWidth + 5;
        buttonWidth = buttonWidth + 5;

        if (i==[mButton.accessibilityValue intValue]) {
            focusPosition = startX;
        }
    }

    NSLog(@"focusPosition===%d", focusPosition);

    CGRect frame = CGRectMake(focusPosition, 0, buttonWidth, 50); //wherever you want to scroll
    [myScrollView scrollRectToVisible:frame animated:NO];


}

但是这并没有给我我想要的东西。滚动视图是否有任何 属性 可以将该按钮置于中心?

Project with above code

注意:这只是 iPhone 6 的示例。所以请 运行 仅在 iPhone 6 模拟器上进行项目

UIScrollView函数 - (void)scrollRectToVisible:(CGRect)rect 动画:(BOOL)动画 几乎做到了。

参考说:"This method scrolls the content view so that the area defined by rect is just visible inside the scroll view. If the area is already visible, the method does nothing."

抱歉,刚看到你已经找到那个功能了。

这是一个示例项目的 link,它的作用与您想要的相同。 这包括一个名为 PagedFlowView 的 class。 sample project to center a view in scrollview

UICollectionView 可能是比 UIScrollView 更好的选择,因为它添加了一种方法来实现您想要的 (scrollToItemAtIndexPath:atScrollPosition:animated:)。

如果您坚持使用 UIScrollView,则需要改用 setContentOffset:animated:,并计算所需的偏移量,例如:

-(IBAction)aMethod:(UIButton *)button {
    CGPoint contentOffset = CGPointMake(CGRectGetMidX(button.frame) - (CGRectGetWidth(self.scrollView.frame) / 2.0),0.0);

    [myScrollView setContentOffset:contentOffset animated:YES];
}