项目介绍:
Navigation and the suspended animation
UISegmentControl控件的主要使用代码:
[Objective-C] 查看源文件 复制代码
#pragma mark - 监听分段控件的值的改变
-(void)indexChange:(UISegmentedControl *)sender{
// NSLog(@"%zd",sender.selectedSegmentIndex);
// 添加动画
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.8];
// 1、定义列数、行数
// 每行的列数
CGFloat columns = sender.selectedSegmentIndex + 2;
[self adjustImagePosWithColumns:columns add:NO];
// 结束动画
[UIView commitAnimations];
}
#pragma mark 调整图片的位置
-(void)adjustImagePosWithColumns:(int)columns add:(BOOL)add
{
// 1、定义列数、间距
// 每个表情之间的间距 = (控制器View的宽度 - 列数 * 表情的宽度)/ (列数 + 1)
CGFloat margin = (self.view.frame.size.width - columns * kImgWh) / (columns + 1);
// 2、定义第一个表情的位置
// 第一个表情的X值
CGFloat oneX = margin;
// 第一个表情的Y值
CGFloat oneY = CGRectGetMaxY(self.segment.frame) + 20;
// 3、创建所有的表情
for (int i = 0; i < kInitCount; i++) {
// i这个位置对应的列数
int col = i % columns;
// i这个位置对应的行数
int row = i / columns;
// 列数决定了X
CGFloat x = oneX + col * (margin + kImgWh);
// 行数决定了Y
CGFloat y = oneY + row * (margin + kImgWh);
if (add){ // 添加新的imageview
int no = i % 9;
// 拼接每个表情的名字
NSString *imageName = [NSString stringWithFormat:@"01%d.png",no];
[self addImg:imageName x:x y:y];
}else {
// 取出旧的imageview 设置x,y
// 取出i + 1位置对应的imageview, 设置x,y 的值
// + 1是为了跳过最前面的UISegmentControl
UIView *child = self.view.subviews[i + 1];
// 取出frame
CGRect tempF = child.frame;
// 修改x,y
tempF.origin = CGPointMake(x, y);
// 重新赋值
child.frame = tempF;
}
}
}
#pragma mark 添加表情,icon表情名
-(void)addImg:(NSString *)icon x:(CGFloat)x y:(CGFloat)y
{
UIImageView *one = [[UIImageView alloc] init];
one.image = [UIImage imageNamed:icon];
one.frame = CGRectMake(x, y, 40, 40);
[self.view addSubview:one];
}
DEMO直接下载:

xiao66guo-Navigation-and-the-suspended-animation-master.zip
(4.34 MB, 下载次数: 64, 售价: 10 金钱)
2016-7-18 10:35 上传
点击文件名下载附件
