易佰
程序员文章站
2022-07-02 18:55:24
...
ViewController.m
//
// ViewController.m
// Yibai
//
// Created by chenshunyi on 2017/12/14.
// Copyright © 2017年 house365. All rights reserved.
//
#import "ViewController.h"
#import "CSButton.h"
@interface ViewController (){
CGSize _size ;
NSMutableArray *_bigArr;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
_size = [UIScreen mainScreen].bounds.size;
UIImageView *backImgVew = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bg.jpg"]];
backImgVew.frame =CGRectMake(0, 0, _size.width, _size.height);
[self.view addSubview:backImgVew];
//创建大图
[self creatBigPicture];
//创建小图
[self creatSmallPicture];
}
//创建上部大图片
-(void)creatBigPicture{
_bigArr = [NSMutableArray array];
NSMutableArray *numArr = [NSMutableArray array];
//创建一个包含0~9 10个数字的数组
for (int j = 0; j<10; j++) {
//把int类型的数字i 转化成字符串类型(数组里面只能放对象)
NSString *numStr = [NSString stringWithFormat:@"%d",j];
[numArr addObject:numStr];
}
CGFloat leftValue = 25;
CGFloat width_height = 55;
//间隔
CGFloat interval = (_size.width-leftValue*2-width_height*4)/3;
for (int i = 0; i< 4; i++) {
CSButton *button = [CSButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(leftValue+i*(width_height+interval), 80, width_height, width_height);
//随机生成一个数字
NSInteger num = arc4random()%numArr.count;
//从数组中找到下表为num的对象(数字)
NSString *numStr1 = [numArr objectAtIndex:num];
//把这次取出来的数从数组中移除 以防下次还会取到相同的数字
[numArr removeObject:numStr1];
button.isUsed = NO;
//根据从数组中找到的数字 拼接成图片名 (用来找内存中的图片)
[button setImage:[UIImage imageNamed:[NSString stringWithFormat:@"b_%@.png",numStr1]] forState:UIControlStateNormal];
[self.view addSubview:button];
[_bigArr addObject:button];
}
}
//创建小图片
-(void)creatSmallPicture{
CGFloat leftValue = 25;
CGFloat width_height = 45;
CGFloat interval = (_size.width-2*leftValue-5*width_height)/4;
for (int i = 0; i<10; i++) {
CSButton *button = [CSButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(leftValue+(i%5)*(width_height+interval), 280+(i/5)*(width_height+interval), width_height, width_height);
[button setImage:[UIImage imageNamed:[NSString stringWithFormat:@"s_%d.png",i]] forState:UIControlStateNormal];
[button addTarget:self action:@selector(smallButtonAction:) forControlEvents:UIControlEventTouchUpInside];
button.isTop = NO;//表示都在下边
button.originCenter = button.center;//把当前的中心坐标记录到button的属性中
[self.view addSubview:button];
}
}
-(void)smallButtonAction:(CSButton *)sender{
if (sender.isTop == YES) {//按钮在上部
CGPoint point = sender.originCenter;
sender.center = point;
sender.isTop = NO;
}else{//按钮在下部
for (int i = 0; i<_bigArr.count; i++) {
CSButton *button = [_bigArr objectAtIndex:i];
if (!button.isUsed) {//如果大数组中的button没有被占用
button.isUsed = YES;
CGPoint point = button.center;
sender.center = point;
sender.isTop = YES;//坐标变为上面大图片的坐标 (移到了上面 所以isTop属性改为YES)
break;//跳出循环
}
}
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
CSButton.h
//
// CSButton.h
// Yibai
//
// Created by chenshunyi on 2017/12/14.
// Copyright © 2017年 house365. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface CSButton : UIButton
//小按钮用的属性
@property (nonatomic,assign) CGPoint originCenter;//原中心坐标
@property (nonatomic,assign) BOOL isTop;//是否在上边
//给大按钮用的属性
@property (nonatomic,assign) BOOL isUsed;//是否被占用
@end
上一篇: AngularJS
下一篇: vue中的$event