cuốn sách gpt4 ai đã làm

ios - Cách tạo Chế độ xem dưới dạng lưới (ví dụ: lưới 9*9, lưới 15*15) bằng vòng lặp for trong Objective-C

In lại Tác giả: Taklimakan Thời gian cập nhật: 2023-11-02 21:11:30 27 4
mua khóa gpt4 Nike

Tôi muốn vẽ lưới 15 * 15 trong Objective-C. Màu lưới có màu xanh lam, giống như bảng được sử dụng trong trò chơi Snake của Nokia.

Tôi đã thử sử dụng vòng lặp for để tạo chế độ xem phụ nhưng có vẻ như nó không hoạt động, tôi đã kiểm tra trên internet và hầu hết mọi người đang sử dụng UIImageView để tạo lưới, có giải pháp nào để tạo lưới 15 * 15 bằng cách sử dụng for vòng lặp hoặc tôi phải sử dụng UIImageView phương pháp?

- (void)viewDidLoad {
[super viewDidLoad];

int i;
hàng int = 0;
int col = 0;
int firstColor=0;

if ([[UIDevice currentDevice] userInterfaceIdiom]== UIUserInterfaceIdiomPhone){
chiều rộng ô = 23;
CellHeight= chiều rộng ô;
topY=230-4*CellHeight;
leftX=110-4*cellWidth;
}khác{

cellWidth=46;
CellHeight=cellWidth;
topY=500-4*CellHeight;
leftX=384-4*cellWidth;
}


UIView *viewA = [[UIView alloc]initWithFrame:CGRectMake(0, 30, 414, 434)];
viewA.backgroundColor = [UIColor blackColor];
[self.view addSubview:viewA];


for (int row = 0; row < row; row = row++ ){
for(int r = 0; r <= 414; r = r + 23){
UIView *viewB = [[UIView alloc]initWithFrame:CGRectMake(r,5,23,23)];
viewB.backgroundColor = [UIColor blueColor];

[viewA addSubview:viewB];}}





col=col+1;
firstColor=1-firstColor;
nếu (col>18){
hàng=hàng+1;
firstColor=1-firstColor;
col=0;
}

câu trả lời hay nhất

Nếu bạn đang sử dụng iOS 9, các chế độ xem xếp chồng sẽ xử lý bố cục của lưới chế độ xem một cách độc đáo:

NSInteger n = 15;

UIStackView *rowView = [[UIStackView alloc] init];
rowView.translatesAutoresizingMaskIntoConstraints = false;
rowView.axis = UILayoutConstraintAxisHorizontal;
rowView.distribution = UIStackViewDistributionFillEqually;
[self.view addSubview:rowView];

for (NSInteger row = 0; row < n; row++) {
UIStackView *cộtView = [[UIStackView phân bổ] init];
cộtView.translatesAutoresizingMaskIntoConstraints = false;
cộtView.axis = UILayoutConstraintAxisVertical;
cộtView.distribution = UIStackViewDistributionFillEqually;
[rowView addArrangedSubview:columnView];
cho (NSInteger col = 0; col < n; col++) {
UIView *view = [[UIView alloc] init];
view.backgroundColor = [UIColor colorWithRed:arc4random_uniform(256) / 255.0
màu xanh lá cây:arc4random_uniform(256) / 255.0
màu xanh:arc4random_uniform(256) / 255.0
alpha:1.0];
view.translatesAutoresizingMaskIntoConstraints = false;
view.layer.borderColor = [UIColor blackColor].CGColor;
view.layer.borderWidth = 0,5;

[columnView addArrangedSubview:view];

// đặt nó bằng chiều rộng của chế độ xem ngăn xếp cột và 1/n chiều cao của chế độ xem ngăn xếp cột

[cộtView addConstraint:[NSLayoutConstraint ràng buộcWithItem:view thuộc tính:NSLayoutAttributionWidth liên quanBy:NSLayoutRelationEqual toItem:columnView thuộc tính:NSLayoutAttributionWidth multiplier:1 constant:0]];
[columnView addConstraint:[NSLayoutConstraint ràng buộcWithItem:view thuộc tính:NSLayoutAttributionHeight liên quanBy:NSLayoutRelationEqual toItem:columnView thuộc tính:NSLayoutAttributionHeight hệ số nhân:1.0 / (CGFloat)n hằng số:0]];
}
}

//làm cho nó vuông

[self.view addConstraint:[NSLayoutConstraint ràng buộcWithItem:rowView thuộc tính:NSLayoutAttributionWidth liên quanBy:NSLayoutRelationEqual toItem:rowView thuộc tính:NSLayoutAttributionHeight hệ số nhân:1 hằng số:0]];

// căn giữa nó

[self.view addConstraint:[NSLayoutConstraint ràng buộcWithItem:rowView thuộc tính:NSLayoutAttributionCenterX liên quanBy:NSLayoutRelationEqual toItem:self.view thuộc tính:NSLayoutAttributionCenterX hệ số nhân:1 hằng số:0]];
[self.view addConstraint:[NSLayoutConstraint ràng buộcWithItem:rowView thuộc tính:NSLayoutAttributionCenterY liên quanBy:NSLayoutRelationEqual toItem:self.view thuộc tính:NSLayoutAttributionCenterY hệ số nhân:1 hằng số:0]];

// đảm bảo nó không bao giờ vượt quá 80% chiều rộng hoặc chiều cao của super view

NSLayoutConstraint *maxWidthConstraint = [NSLayoutConstraint ràng buộcWithItem:rowView thuộc tính:NSLayoutAttributionWidth liên quanBy:NSLayoutRelationLessThanOrEqual toItem:self.view thuộc tính:NSLayoutAttributionWidth multiplier:0,8 constant:0];
NSLayoutConstraint *maxHeightConstraint = [NSLayoutConstraint ràng buộcWithItem:rowView thuộc tính:NSLayoutAttributionHeight liên quanBy:NSLayoutRelationLessThanOrEqual toItem:self.view thuộc tính:NSLayoutAttributionHeight hệ số nhân:0,8 hằng số:0];
[self.view addConstraints:@[maxWidthConstraint, maxHeightConstraint]];

// Nhưng nó chiếm tới 80% chiều rộng hoặc chiều cao của super view

NSLayoutConstraint *preferredWidthConstraint = [NSLayoutConstraint ràng buộcWithItem:rowView thuộc tính:NSLayoutAttributionWidth liên quanBy:NSLayoutRelationEqual toItem:self.view thuộc tính:NSLayoutAttributionWidth multiplier:0,8 constant:0];
ưa thíchWidthConstraint.priority = 900;
NSLayoutConstraint *preferredHeightConstraint = [NSLayoutConstraint ràng buộcWithItem:rowView thuộc tính:NSLayoutAttributionHeight liên quanBy:NSLayoutRelationEqual toItem:self.view thuộc tính:NSLayoutAttributionHeight hệ số nhân:0,8 hằng số:0];
ưa thíchHeightConstraint.priority = 900;
[self.view addConstraints:@[preferredWidthConstraint, PreferredHeightConstraint]];

产生:

nhập mô tả hình ảnh ở đây

Nếu nhắm mục tiêu các phiên bản iOS cũ hơn, bạn có thể tự thêm lưới xây dựng, nhưng điều này đòi hỏi phải thêm nhiều ràng buộc hơn để bố trí mọi thứ một cách chính xác.

Về ios - cách sử dụng vòng lặp for trong Objective-C để tạo Chế độ xem dưới dạng lưới (chẳng hạn như lưới 9*9, lưới 15*15), chúng tôi đã tìm thấy một câu hỏi tương tự trên Stack Overflow: https://stackoverflow.com/questions/33612583/

27 4 0
Chứng chỉ ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com
Xem sitemap của VNExpress