gpt4 book ai didi

ios - 动态UIButton定位?

转载 作者:行者123 更新时间:2023-11-29 04:16:18 24 4
gpt4 key购买 nike

我有用户可以在我的游戏中获得的奖励。现在只有解锁后才会显示。他们可以获得 4 种不同的奖励。现在他们可能解锁了 1 个奖励,或者 2 个、3 个或 4 个奖励。但是我希望图像能够动态定位,如下图所示。在下图中,每行看起来都应与解锁奖励的数量相同。 enter image description here

现在,我将如何继续这样做? (注意:图像在 Y 轴上不会发生变化,它们只是在 X 轴上动态定位,所以不要被图像愚弄了)

谢谢!

最佳答案

我将创建一个自定义 UIView 子类来执行此操作,以便您可以在可以使用 View 的任何地方使用它。

用法:

MyFlexibleSpacedButtons *myButtons = [[MyFlexibleSpacedButtons alloc] initWithFrame:CGRectMake(50, 50, 250, 60)];
[myButtons setButtonAtIndex:2 hidden:NO];
[myButtons setButtonAtIndex:0 hidden:NO];
[self.view addSubview:myButtons];

这是示例类:

MyFlexibleSpacedButtons.h:

#import <UIKit/UIKit.h>

@interface MyFlexibleSpacedButtons : UIView

@property (nonatomic, readonly) NSArray *allButtons;

- (void)setButtonAtIndex:(NSUInteger)index hidden:(BOOL)hidden;

@end

MyFlexibleSpacedButtons.m:

#import "MyFlexibleSpacedButtons.h"

const NSUInteger maxButtons = 9;
const NSUInteger buttonsPerRow = 3; // This can not be 0.
const CGFloat buttonHeight = 50;
const CGFloat buttonWidth = 50;
const CGFloat spaceBetweenButtons = 10.0;

@interface MyFlexibleSpacedButtons ()
@property (nonatomic, readwrite) NSArray *allButtons;
@end

@implementation MyFlexibleSpacedButtons

@synthesize allButtons;

- (id)initWithFrame:(CGRect)frame
{
NSUInteger numberOfRows = ceil((double)maxButtons / (double)buttonsPerRow);
CGFloat minHeight = buttonHeight * numberOfRows + spaceBetweenButtons * (numberOfRows - 1);
if (frame.size.height < minHeight)
{
frame.size.height = minHeight;
}

CGFloat minWidth = buttonWidth * maxButtons + spaceBetweenButtons * (maxButtons-1);
if (frame.size.width < minWidth)
{
frame.size.width = minWidth;
}

self = [super initWithFrame:frame];
if (self) {
// Defaults
// Uncomment the following line if needed for debugging:
// self.backgroundColor = [UIColor grayColor];

// Create the buttons and add them to the array. Default to hidden buttons
self.allButtons = [NSArray new];
for (int i = 0; i < maxButtons; i++)
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.hidden = YES;
[button setTitle:[NSString stringWithFormat:@"%d", i] forState:UIControlStateNormal];
[self addSubview:button];
self.allButtons = [self.allButtons arrayByAddingObject:button];
}

[self setButtonFrames];
}
return self;
}

- (void)dealloc
{
allButtons = nil;
}

- (void)setButtonFrames
{
CGFloat viewHeight = self.bounds.size.height;
CGFloat viewWidth = self.bounds.size.width;

NSUInteger buttonCount = [self visibleButtonsCount];
NSUInteger numberOfRows = ceil((double)maxButtons / (double)buttonsPerRow);
CGFloat buttonY = (viewHeight - buttonHeight * numberOfRows - spaceBetweenButtons * (numberOfRows - 1)) / 2;
CGFloat buttonGroupTotalWidth = buttonCount * buttonWidth + (buttonCount - 1) * spaceBetweenButtons;
CGFloat buttonGroupStartingX = (viewWidth - buttonGroupTotalWidth) / 2;

// Set frames of buttons
NSUInteger visibleButtonIndex = 0;
for (int i = 0; i < maxButtons; i++)
{
UIButton *button = [self.allButtons objectAtIndex:i];

if (!button.hidden)
{
CGFloat buttonX = buttonGroupStartingX + visibleButtonIndex % buttonsPerRow * (buttonWidth + spaceBetweenButtons);
button.frame = CGRectMake(buttonX, buttonY, buttonWidth, buttonHeight);
visibleButtonIndex++;

if (visibleButtonIndex % buttonsPerRow == 0)
{
buttonY = buttonY + buttonHeight + spaceBetweenButtons;
}
}
}
}

- (void)setButtonAtIndex:(NSUInteger)index hidden:(BOOL)hidden
{
if (index > maxButtons - 1)
{
return;
}

UIButton *button = [self.allButtons objectAtIndex:index];
button.hidden = hidden;

[self setButtonFrames];
}

- (NSUInteger)visibleButtonsCount
{
NSUInteger buttonCount = 0;

for (UIButton *button in self.allButtons)
{
if (!button.hidden)
{
buttonCount++;
}
}
return buttonCount;
}

@end

关于ios - 动态UIButton定位?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13655550/

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