gpt4 book ai didi

ios - 单击时将 UiScrollView 元素置于中心

转载 作者:行者123 更新时间:2023-11-29 01:47:54 25 4
gpt4 key购买 nike

我有的是 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);
}

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

为此我所做的如下。

- (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];


}

然而,这并没有给我我想要的东西。 scrollview 是否有任何属性可以将该按钮置于中心?

Project with above code

注意:此示例仅适用于 iPhone 6。因此,请仅在 iPhone 6 模拟器上运行项目

最佳答案

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

关于ios - 单击时将 UiScrollView 元素置于中心,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31701348/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com