iOS_4_表情排列

终于效果图:



BeyondViewController.h

//
//  BeyondViewController.h
//  04_表情排列
//
//  Created by beyond on 14-7-22.
//  Copyright (c) 2014年 com.beyond. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface BeyondViewController : UIViewController
@property (weak, nonatomic) IBOutlet UISegmentedControl *segment;
- (IBAction)segmentValueChanged:(UISegmentedControl *)sender;

@end






BeyondViewController.m

//
//  BeyondViewController.m
//  04_表情排列
//
//  Created by beyond on 14-7-22.
//  Copyright (c) 2014年 com.beyond. All rights reserved.
//

#import "BeyondViewController.h"
// 加入button的tag,由于segment值改变时,要将button置于数组最后面
#define kAddButton_Tag 99
@interface BeyondViewController ()

@end

@implementation BeyondViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSLog(@"view did load");
    
    
    int colsNum = _segment.selectedSegmentIndex + 2;
    // 调用自己定义方法  加入images
    [self alinmentWithCols:colsNum];
    // 加入一个 button放到最后面,目的是:点击button就能够加入一个imageView到最后
    [self addBtn];
}

// 加入一个 button放到最后面,目的是:点击button就能够加入一个imageView到最后
- (void)addBtn
{
    // sg_btn 代码生成button 1,实例化
    UIButton *btn = [[UIButton alloc]init];
    // 2,设定详情
    // 正常状态
    btn.tag = kAddButton_Tag;
    [btn setTitle:@"normal" forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    
    [btn setBackgroundImage:[UIImage imageNamed:@"sub_black_add.png"] forState:UIControlStateNormal];
    // 点击时高亮状态
    [btn setTitle:@"highlighted" forState:UIControlStateHighlighted];
    [btn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    
    [btn setBackgroundImage:[UIImage imageNamed:@"sub_blue_add.png"] forState:UIControlStateHighlighted];
    // 为button加入点击事件
    [btn addTarget:self action:@selector(addBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    
#warning 以下全是通过i 计算i的排列坐标
    NSArray *array = self.view.subviews;
    // button的排号为i,通过 i 算出行号,列号,坐标x y
    int i = array.count-1;
    // 调用自己定义的方法,通过控件在格子中的索引即排号,算出其应该在哪一行,哪一列,从而得到其坐标
    CGPoint btn_pos = [self getPositionByIndexInCell:i];
    btn.frame = CGRectMake(btn_pos.x, btn_pos.y, 60, 60);
    
    // 3,加入button到self.view
    [self.view addSubview:btn];
}
// 通过控件位于格子里面的i,即排号,得到它位于哪一行哪一列,从而返回其x,y
- (CGPoint)getPositionByIndexInCell:(int)i
{
    int colsNum = _segment.selectedSegmentIndex + 2;
    //NSLog(@"在格子里面的排号i为 %d",i);
    // 第0个即排头的表情的y
    int firstImgY = 60;
    // 表情之间的净间距
    int imgMargin = (self.view.frame.size.width-60*colsNum)/(colsNum+1);
    // 第0个即排头的表情的x
    int firstImgX = imgMargin;
    
    // 第i个表情(这儿是button) 所在的行号
    int row = i/colsNum;
    // 第i个表情(这儿是button) 所在的列号
    int cols = i%colsNum;
    int x = cols * (60+imgMargin)+firstImgX;
    int y = row * (60+imgMargin)+firstImgY;
    return CGPointMake(x, y);
}
// 当点击 + 号button的时候,创建 一个随机的imgView,并加入到self.view里面,注意imgView位置,以及btn的位置
- (void)addBtnClick:(UIButton *)sender
{
    // 1,类方法创建控件
    UIImageView *imgView = [[UIImageView alloc]init];
    // 2,控件细节
    int rand = arc4random_uniform(9);
    NSString *imgName = [NSString stringWithFormat:@"01%d.png",rand];
    imgView.image = [UIImage imageNamed:imgName];
    // 屏幕宽高
    CGSize winSize = self.view.bounds.size;
    int orgin_x = arc4random_uniform(winSize.width);
    int orgin_y = arc4random_uniform(winSize.height);
    imgView.frame = CGRectMake(orgin_x, orgin_y, 0, 0);
    // 3,将控件加入到当前控制器的view
    [self.view addSubview:imgView];
    [UIView animateWithDuration:1 animations:^{
        // 动画实现2个要求:1,为新加入的imgView设置x y 2,为button又一次设置x y
        NSArray *array = [self.view subviews];
        // 排号为i,通过 i 算出行号,列号,坐标x y
        int imgView_i = array.count-3;
        int sender_i = array.count-2;
        // 调用自己定义的方法,通过控件在格子中的索引即排号,算出其应该在哪一行,哪一列,从而得到其坐标
        CGPoint imgView_pos = [self getPositionByIndexInCell:imgView_i];
        CGPoint sender_pos = [self getPositionByIndexInCell:sender_i];
        
        
        // 又一次设置新加入的imgview的位置 以及 加入button的位置
        imgView.frame = CGRectMake(imgView_pos.x, imgView_pos.y, 60, 60);
        sender.frame = CGRectMake(sender_pos.x, sender_pos.y, 60, 60);
    }];
}
// 自己定义方法,參数:图片名 010.png ~ 018.png x坐标 y坐标
- (void) addImg:(NSString *)imgName x:(CGFloat)x y:(CGFloat)y
{
    // 1,实例化
    UIImageView *imgView = [[UIImageView alloc]init];
    // 2,设置细节
    UIImage *img = [UIImage imageNamed:imgName];
    imgView.image = img;
    imgView.frame = CGRectMake(x, y, 60, 60);
    // 3,加入到self的view里面,以便显示
    [self.view addSubview:imgView];
    
}

- (IBAction)segmentValueChanged:(UISegmentedControl *)sender {
    // 索引的值为0 1 2 3 -------2列 3列 4列 5列
    // NSLog(@"%d",sender.selectedSegmentIndex);
    int segmentIndex = sender.selectedSegmentIndex;
    int colsNum = segmentIndex + 2;
    
    // 在调整位置之前,先要在self.view.subViews数组里面,将加入button放到最一个位置
    // 通过tag取到addButton
    UIButton *addBtn = [self.view viewWithTag:kAddButton_Tag];
    // 为了能够始终让addBtn在self.view的最后一个,先将其移出父控件,再加到父控件
    [addBtn removeFromSuperview];
    [self.view addSubview:addBtn];
    // 调用自己定义方法  调整原来的位置
    [self alinmentWithCols:colsNum];
        
   

}
// 抽取出来的方法,分为加入和取出又一次调整2种处理方式,推断方法是 view中的子控件数目是否仅仅有一个segmentControl
- (void) alinmentWithCols:(int)colsNum
{
    // 假设子控件个数为 1 即仅仅有 segmentControl这一个控件,那么加入,否则,又一次调整位置
    int subViewsCount = [[self.view subviews] count];
    //NSLog(@"子控件个数:%d---",subViewsCount);
    // 抽取出来的共同代码
    
    int n = subViewsCount - 1;
    // 假设 n = 0,说明仅仅有一个segment,此时要加入3个imageView
    // 否则,取出全部的子控件,又一次 设置位置
    for (int i=0; i<=(n==0?3:n-1); i++) {
        
        // 调用自己定义的方法,通过控件在格子中的索引即排号,算出其应该在哪一行,哪一列,从而得到其坐标
        CGPoint pos = [self getPositionByIndexInCell:i];
        
        
        // 不同处理方式
        if (subViewsCount == 1) {
            NSString *imgName = [NSString stringWithFormat:@"01%d.png",i];
            [self addImg:imgName x:pos.x y:pos.y];
        } else {            // 当点击 segment的时候,不应该再次加入imageView,而是应该取出,又一次设置xy
            // [self addImg:imgName x:x y:y];
            NSArray *array = [self.view subviews];
            NSLog(@"正在又一次设置%@的位置",[array[i+1] class]);
            UIView *view = array[i+1];
            [UIView animateWithDuration:1 animations:^{
                // 以下三步为OC标准代码,由于OC中不同意直接修该对象中结构体属性的成员的值,要通过中间的暂时结构体变量
                CGRect frame = view.frame;
                frame.origin = CGPointMake(pos.x, pos.y);
                view.frame=frame;
            }];
        } // end of if else
  } // end  of  for 循环
    
} // end of function

@end

效果图1


效果图2

















posted @ 2017-05-15 12:07  zsychanpin  阅读(169)  评论(0编辑  收藏  举报