Created
October 31, 2015 10:56
-
-
Save syxc/461a84377fd544974cb3 to your computer and use it in GitHub Desktop.
一个布局的例子,类似 QQ 表情面板
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// ViewController.m | |
// FaceDemo | |
// | |
// Created by syxc on 15/10/31. | |
// Copyright © 2015年 syxc. All rights reserved. | |
// | |
#import "ViewController.h" | |
@interface ViewController () | |
@end | |
@implementation ViewController | |
- (void)viewDidLoad { | |
[super viewDidLoad]; | |
UILabel *lblTitle = [[UILabel alloc] initWithFrame:CGRectMake(0, 20, self.view.frame.size.width, 40)]; | |
lblTitle.text = @"QQ表情面板"; | |
lblTitle.textAlignment = NSTextAlignmentCenter; | |
lblTitle.font = [UIFont systemFontOfSize:20]; | |
[self.view addSubview:lblTitle]; | |
UIView *faceView = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(lblTitle.frame), | |
self.view.frame.size.width, | |
self.view.frame.size.height - lblTitle.frame.size.height + 20)]; | |
faceView.backgroundColor = [UIColor blackColor]; | |
[self.view addSubview:faceView]; | |
int faceCount = 105; // 表情总数 | |
int count = 0; // 列 | |
int row = 0; // 行 | |
float spacing = 8.0; // 间距 | |
int maxPerRow = floor(faceView.frame.size.width / (24 + spacing)); // 容器允许多少列合适 | |
for (int i = 0; i < faceCount; i++) { | |
UIImageView *icon = [[UIImageView alloc] init]; | |
icon.image = [UIImage imageNamed:[NSString stringWithFormat:@"%d", i]]; | |
CGRect rect = CGRectMake(count * (spacing + 24), row * (spacing + 24), 24, 24); | |
icon.frame = rect; | |
if(count % maxPerRow == maxPerRow - 1) { | |
count = 0; | |
row++; | |
} else { | |
count++; | |
} | |
[faceView addSubview:icon]; | |
NSLog(@"face index:%d", i); | |
} | |
} | |
- (void)didReceiveMemoryWarning { | |
[super didReceiveMemoryWarning]; | |
// Dispose of any resources that can be recreated. | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment