iOS之俄罗斯方块

设计思路


先自定义一个kuai类继承于UIView,在self里面新建一个%4的随机数然后根据随机数决定方块颜色

新建存放方块的backgroundView类

核心函数一newkuai,

  1. 用定时器调用新建四个全局View,(self.view1,self.view2,,,)每次新建一个图块就赋值给self.view用来控制他们的行为(下降,左右)
  2. 定时器每次调用self.view就下降30
  3. 将视图区域坐标化,新键一个可变字典 dir(存放坐标),可变字典  kuaizu(存放UIView)横坐标0-7纵坐标0-14;比如(0,1,140,141)作为key,初始value为@“1”
  4. 定义横坐标为i纵坐标为j初始值为0,每次方块移动(j++,左右i++.i--),view停止时要清空i,j,
  5. 每次调用newkuai时先取出self.view1.2.3.4,下一步位置的坐标(j*10+i+10)的值,if判断如果这个值为2(表示这个位置有方块)或者这些self.view有一个的坐标为420(方块到了底部),那么根据坐标修改对应的值为2;将4个 self.view放入字典kauizu,
  6. 调用判断函数4次
  7. (根据随机数)重新创建四个view把它们复制给四个self.view.清空所有i,j.
  8. self.view就下降30

核心函数二(panduan)判断最后一行,或者所有行

  1. 获取最后一行8个的值,判断是否全部等于2,如果是遍历kuaizu字典,把所有方块view向下移动30;
  2. 在此之前遍历坐标字典如果当前块于下一行的这一块值不同,就要把当前付给下一行,因为view移动,下一行块值等于当前行的值。
  3. 控制函数
  4. 每种块有四种变变换或者两种;同样采用全局标志位,判断当前这个块是那种块那种变幻形态;每次变幻i.j值都要修改(小心认真修改)。
  5. 左右控制只需要i++,i--就可以;
  6. 暂停,控制定时器停止恢复

扩展

  1. 难度,控制定时器时间
  2. 分一点视图展示下一个块是什么块;该位置每次块初始化之后就生成全局的随机数,视图根据这个view显示下个图块,下次初始化再根据这个随机数初始化块
  3. 快速下降;当点击向下按钮时,遍历字典,判断当前位置下面一个个为2的坐标点,得到他的上一行,把块移动到这一行 self.view.frame;j+n;同样在进行改值,赋值,初始化,清空i,j,一系列操作。
  4. 加动画

//
//  backgroundView.h
//  erKinf
//
//  Created by root1 on 18-1-7.
//  Copyright (c) 3018年 root. All rights reserved.
//

#import 
#import "kuai.h"

@interface backgroundView : UIViewController
@property kuai *view1;
@property kuai *view2;
@property kuai *view3;
@property kuai *view4;

@property kuai *changview1;
@property kuai *changview2;
@property kuai *changview3;
@property kuai *changview4;

@property NSTimer *time1;
- (IBAction)changeBtn:(UIButton *)sender;

@property bool *isrich;
@property NSMutableDictionary *dir;

@property int i;    //横坐标
@property int j;    //纵坐标
@property int view1i;   //view1操控view的横坐标
@property int view1j;
@property int view2i;
@property int view2j;
@property int view3i;
@property int view3j;
@property int view4i;
@property int view4j;

@property int bianXing1;    //控制方块变形
@property int fangkuaiKind;

- (IBAction)leftBtn:(UIButton *)sender;
- (IBAction)right:(UIButton *)sender;

- (IBAction)pouse:(UIButton *)sender;
@property NSMutableArray *kuaiZu;
@property (weak, nonatomic) IBOutlet UIView *ViewTo;

@property BOOL pauseNum;


@end
//
//  backgroundView.m
//  erKinf
//
//  Created by root1 on 18-1-7.
//  Copyright (c) 3018年 root. All rights reserved.
//

#import "backgroundView.h"
#import "kuai.h"
#import "point.h"

@interface backgroundView ()

@end

@implementation backgroundView

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    self.kuaiZu=[[NSMutableArray alloc]init];   //装kuai的数组初始化
    [self.view bringSubviewToFront:self.ViewTo];        //遮盖视图移到最上
    self.pauseNum=false;
    
    
    //给空间分块定坐标
    self.dir=[NSMutableDictionary dictionaryWithObjectsAndKeys:@"1",@0,@"1",@1,@"1",@2,@"1",@3,@"1",@4,@"1",@5,@"1",@6,@"1",@7,
              @"1",@10,@"1",@11,@"1",@12,@"1",@13,@"1",@14,@"1",@15,@"1",@16,@"1",@17,
              @"1",@30,@"1",@21,@"1",@22,@"1",@23,@"1",@24,@"1",@25,@"1",@26,@"1",@27,
              @"1",@30,@"1",@31,@"1",@32,@"1",@33,@"1",@34,@"1",@35,@"1",@36,@"1",@37,
              @"1",@40,@"1",@41,@"1",@42,@"1",@43,@"1",@44,@"1",@45,@"1",@46,@"1",@47,
              @"1",@50,@"1",@51,@"1",@52,@"1",@53,@"1",@54,@"1",@55,@"1",@56,@"1",@57,
              @"1",@60,@"1",@61,@"1",@62,@"1",@63,@"1",@64,@"1",@65,@"1",@66,@"1",@67,
              @"1",@70,@"1",@71,@"1",@72,@"1",@73,@"1",@74,@"1",@75,@"1",@76,@"1",@77,
              @"1",@80,@"1",@81,@"1",@82,@"1",@83,@"1",@84,@"1",@85,@"1",@86,@"1",@87,
              @"1",@90,@"1",@91,@"1",@92,@"1",@93,@"1",@94,@"1",@95,@"1",@96,@"1",@97,
              @"1",@100,@"1",@101,@"1",@102,@"1",@103,@"1",@104,@"1",@105,@"1",@106,@"1",@107,
              @"1",@110,@"1",@111,@"1",@112,@"1",@113,@"1",@114,@"1",@115,@"1",@116,@"1",@117,
              @"1",@130,@"1",@121,@"1",@122,@"1",@123,@"1",@124,@"1",@125,@"1",@126,@"1",@127,
              @"1",@130,@"1",@131,@"1",@132,@"1",@133,@"1",@134,@"1",@135,@"1",@136,@"1",@137,
              @"1",@140,@"1",@141,@"1",@142,@"1",@143,@"1",@144,@"1",@145,@"1",@146,@"1",@147,
              nil
              
              ];
//    NSMutableArray *mulArray = [[NSMutableArray alloc] initWithCapacity:30];
//    [mulArray addObject:];
//    id obj = [mulArray objectAtIndex:1];
//    NSLog(@"obj = %@",obj);
    
    self.bianXing1=1;                                   //变形标志位为1
    
    


    self.isrich=false;
    
    kuai *kuai1=[[kuai alloc]init];
    self.view1=kuai1;
    self.view1.frame=CGRectMake(0, -90, 30, 30);
    self.view1i=0;
    self.view1j=-3;

    kuai *kuai2=[[kuai alloc]init];
    self.view2=kuai2;
    self.view2.frame=CGRectMake(0, -60, 30, 30);
    self.view2i=0;
    self.view2j=-2;


    kuai *kuai3=[[kuai alloc]init];
    self.view3=kuai3;
    self.view3.frame=CGRectMake(0, -30, 30, 30);
    self.view3i=0;
    self.view3j=-1;


    kuai *kuai4=[[kuai alloc]init];
    self.view4=kuai4;
    self.view4.frame=CGRectMake(30, -60, 30, 30);
    self.view4i=1;
    self.view4j=-2;

    
    self.fangkuaiKind=1;
    NSObject *ii=@"1";

//    self.kuaiZu=[NSArray arrayWithObjects:@"a",@"b",@"c",nil];
//    //[self.kuaiZu objectAtIndex:ii];
//
//    for(id obj in self.kuaiZu)
//    {
//        [newArray addObject: obj];
//    }
//    for (int i=1; i<=[self.kuaiZu count]; i++) {
//        kuai *kk=[self.kuaiZu objectAtIndex:i];
//        kk.x=300;
//        kk.y=300;
//        kk.frame=CGRectMake(kk.x, kk.y, 50, 50);
//        [self.view addSubview:kk];
//        NSLog(@"shuzu");
//    }
    
    
    //[self.view addSubview:kuai1];
    self.time1=[NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(newkuai:) userInfo:nil repeats:YES];

    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}
-(void)newkuai:(UIView *)view
{
    //计算下一步的点对应的key对应的值
    int view1Next=self.view1i+self.view1j*10+10;
    int view2Next=self.view2i+self.view2j*10+10;
    int view3Next=self.view3i+self.view3j*10+10;
    int view4Next=self.view4i+self.view4j*10+10;


    NSString *name = [self.dir objectForKey:@(view1Next)];
    
    NSString *name2 = [self.dir objectForKey:@(view2Next)];

    NSString *name3 = [self.dir objectForKey:@(view3Next)];

    NSString *name4 = [self.dir objectForKey:@(view4Next)];

    
    //如果某个方块到达底部或者他的下一步有方块则停止
    if (self.view1.frame.origin.y==420||self.view2.frame.origin.y==420||self.view3.frame.origin.y==420||self.view4.frame.origin.y==420||[name isEqualToString:@"2"]||[name2 isEqualToString:@"2"]||[name3 isEqualToString:@"2"]||[name4 isEqualToString:@"2"]){
        if (self.isrich==false) {
            
            
            
            //把这个块view添加到 可变数组;
            
            [self.kuaiZu addObject:self.view1];
            [self.kuaiZu addObject:self.view2];
            [self.kuaiZu addObject:self.view3];
            [self.kuaiZu addObject:self.view4];
            
            
            
            //修改每个点的键值为2
            int view1zong=self.view1i+self.view1j*10;
            int view2zong=self.view2i+self.view2j*10;
            int view3zong=self.view3i+self.view3j*10;
            int view4zong=self.view4i+self.view4j*10;
            

            
            
            [self.dir setObject:@"2"forKey:@(view1zong)];
            [self.dir setObject:@"2"forKey:@(view2zong)];
            [self.dir setObject:@"2"forKey:@(view3zong)];
            [self.dir setObject:@"2"forKey:@(view4zong)];
            
            
            [self panduan];
            [self panduan];
            [self panduan];
            [self panduan];
            
            //新建随机数,确定下个要出现的方块类型
            self.fangkuaiKind=(arc4random()%4)+1;
            
            if (self.fangkuaiKind==1) {
                //重新new一个方块出来
                self.bianXing1=1;                                       //初始变形标志位
                
                kuai *kuai1=[[kuai alloc]init];
                self.view1=kuai1;
                self.view1.frame=CGRectMake(0, -90, 30, 30);
                kuai *kuai2=[[kuai alloc]init];
                self.view2=kuai2;
                self.view2.frame=CGRectMake(0, -60, 30, 30);
                
                kuai *kuai3=[[kuai alloc]init];
                self.view3=kuai3;
                self.view3.frame=CGRectMake(0, -30, 30, 30);
                
                kuai *kuai4=[[kuai alloc]init];
                self.view4=kuai4;
                self.view4.frame=CGRectMake(30, -60, 30, 30);
                
                
                
                //方块坐标初始化
                self.view1i=0;
                self.view1j=-3;
                self.view2i=0;
                self.view2j=-2;
                self.view3i=0;
                self.view3j=-1;
                self.view4i=1;
                self.view4j=-2;
            }
            else if(self.fangkuaiKind==2){
                //重新new一个方块出来
                self.bianXing1=1;                                       //初始变形标志位
                
                kuai *kuai1=[[kuai alloc]init];
                self.view1=kuai1;
                self.view1.frame=CGRectMake(0, -90, 30, 30);
                kuai *kuai2=[[kuai alloc]init];
                self.view2=kuai2;
                self.view2.frame=CGRectMake(0, -60, 30, 30);
                
                kuai *kuai3=[[kuai alloc]init];
                self.view3=kuai3;
                self.view3.frame=CGRectMake(0, -30, 30, 30);
                
                kuai *kuai4=[[kuai alloc]init];
                self.view4=kuai4;
                self.view4.frame=CGRectMake(30, -30, 30, 30);
                
                //方块坐标初始化
                self.view1i=0;
                self.view1j=-3;
                self.view2i=0;
                self.view2j=-2;
                self.view3i=0;
                self.view3j=-1;
                self.view4i=1;
                self.view4j=-1;
                
            }
            else if(self.fangkuaiKind==3){
                //重新new一个方块出来
                self.bianXing1=1;                                       //初始变形标志位
                
                kuai *kuai1=[[kuai alloc]init];
                self.view1=kuai1;
                self.view1.frame=CGRectMake(0, -60, 30, 30);
                kuai *kuai2=[[kuai alloc]init];
                self.view2=kuai2;
                self.view2.frame=CGRectMake(30, -60, 30, 30);
                
                kuai *kuai3=[[kuai alloc]init];
                self.view3=kuai3;
                self.view3.frame=CGRectMake(30, -30, 30, 30);
                
                kuai *kuai4=[[kuai alloc]init];
                self.view4=kuai4;
                self.view4.frame=CGRectMake(60, -30, 30, 30);
                
                //方块坐标初始化
                self.view1i=0;
                self.view1j=-2;
                self.view2i=1;
                self.view2j=-2;
                self.view3i=1;
                self.view3j=-1;
                self.view4i=2;
                self.view4j=-1;
                
            }else if (self.fangkuaiKind==4){
                //重新new一个方块出来
                self.bianXing1=1;                                       //初始变形标志位
                
                kuai *kuai1=[[kuai alloc]init];
                self.view1=kuai1;
                self.view1.frame=CGRectMake(0, -120, 30, 30);
                kuai *kuai2=[[kuai alloc]init];
                self.view2=kuai2;
                self.view2.frame=CGRectMake(0, -90, 30, 30);
                
                kuai *kuai3=[[kuai alloc]init];
                self.view3=kuai3;
                self.view3.frame=CGRectMake(0, -60, 30, 30);
                
                kuai *kuai4=[[kuai alloc]init];
                self.view4=kuai4;
                self.view4.frame=CGRectMake(0, -30, 30, 30);
                
                //方块坐标初始化
                self.view1i=0;
                self.view1j=-4;
                self.view2i=0;
                self.view2j=-3;
                self.view3i=0;
                self.view3j=-2;
                self.view4i=0;
                self.view4j=-1;
            }
            
            

        }
    }else{  //如果下方可以走就走
        self.view1.frame=CGRectMake(self.view1.frame.origin.x, self.view1.frame.origin.y+30, 30, 30);
        [self.view addSubview:self.view1];
        
        self.view2.frame=CGRectMake(self.view2.frame.origin.x, self.view2.frame.origin.y+30, 30, 30);
        [self.view addSubview:self.view2];
        
        self.view3.frame=CGRectMake(self.view3.frame.origin.x, self.view3.frame.origin.y+30, 30, 30);
        [self.view addSubview:self.view3];
        
        self.view4.frame=CGRectMake(self.view4.frame.origin.x, self.view4.frame.origin.y+30, 30, 30);
        [self.view addSubview:self.view4];
        
        //j 坐标++
        self.view1j++;
        
        self.view2j++;
        
        self.view3j++;
        
        self.view4j++;
        
        
    }
        
    [self.view bringSubviewToFront:self.ViewTo];
}

-(void)panduan                                                  //判断最后一行
{
    
    NSString *lost1 = [self.dir objectForKey:@140];
    NSString *lost2 = [self.dir objectForKey:@141];
    NSString *lost3 = [self.dir objectForKey:@142];
    NSString *lost4 = [self.dir objectForKey:@143];
    NSString *lost5 = [self.dir objectForKey:@144];
    NSString *lost6 = [self.dir objectForKey:@145];
    NSString *lost7 = [self.dir objectForKey:@146];
    NSString *lost8 = [self.dir objectForKey:@147];
    
    if ([lost1 isEqualToString:@"2"]&&[lost2 isEqualToString:@"2"]&&[lost3 isEqualToString:@"2"]&&[lost4 isEqualToString:@"2"]&&[lost5 isEqualToString:@"2"]&&[lost6 isEqualToString:@"2"]&&[lost7 isEqualToString:@"2"]&&[lost8 isEqualToString:@"2"]) {
        
        [self.time1 setFireDate:[NSDate distantFuture]];
        
//        [self.dir setObject:@"1"forKey:@90];
//        [self.dir setObject:@"1"forKey:@91];
//        [self.dir setObject:@"1"forKey:@92];
//        [self.dir setObject:@"1"forKey:@93];
        NSLog(@"////////////////////////");
        for (int i=0; i<=7; i++) {
            for (int j=14; j>=1; j--) {
                int v=j*10+i;
                int v2=j*10+i+10;
                NSString *now = [self.dir objectForKey:@(v)];
                NSString *next = [self.dir objectForKey:@(v2)];
                
//                NSLog(@"%d",v);
//                NSString *lostlost2 = [self.dir objectForKey:@(v)];
//                NSLog(@"%@",lostlost2);
//                NSLog(@"%d",v2);
//                NSString *lostlost3 = [self.dir objectForKey:@(v2)];
//                NSLog(@"%@",lostlost3);
//                NSLog(@"////////////////////////////");
                
                if ([now isEqualToString:@"2"]&&[next isEqualToString:@"1"]) {
                    [self.dir setObject:@"2" forKey:@(v2)];
                }
                if ([now isEqualToString:@"1"]&&[next isEqualToString:@"2"]) {
                    [self.dir setObject:@"1" forKey:@(v2)];
                }
//                NSLog(@"%d",v);
//                NSString *lostlost1 = [self.dir objectForKey:@(v)];
//                NSLog(@"%@",lostlost1);
//                NSLog(@"%d",v2);
//                NSString *lostlost = [self.dir objectForKey:@(v2)];
//                NSLog(@"%@",lostlost);
                
//                if ([now isEqualToString:next]==false) {
//                    [self.dir setObject:now forKey:@(v2)];
//                }
                
//                UIView *lostView=[self.view hitTest:CGPointMake(i*50,j*50) withEvent:NULL];
//                if (lostView==self.view) {
//                    lostView.frame=CGRectMake(lostView.frame.origin.x, lostView.frame.origin.y+50, 50, 50);
//                }
            }
        }
//        for (NSString *dic1 in self.dir) {
//            //检索到key时,通过objectForKey:获取对应的value值
//            NSLog(@"%@",[self.dir objectForKey:dic1]);
//        }

        
        for (kuai *view in self.kuaiZu) {
            view.frame=CGRectMake(view.frame.origin.x, view.frame.origin.y+30, 30, 30);
            
        }
        [self.time1 setFireDate:[NSDate distantPast]];
        
    }
    
    
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)changeBtn:(UIButton *)sender {
    
    if (self.fangkuaiKind==1) {
        if (self.bianXing1==1) {
            self.view1.frame=CGRectMake(self.view1.frame.origin.x, self.view1.frame.origin.y, 30, 30);
            [self.view addSubview:self.view1];
            
            self.view2.frame=CGRectMake(self.view2.frame.origin.x, self.view2.frame.origin.y, 30, 30);
            [self.view addSubview:self.view2];
            
            self.view3.frame=CGRectMake(self.view3.frame.origin.x-30, self.view3.frame.origin.y-30, 30, 30);
            [self.view addSubview:self.view3];
            
            //
            self.view3i--;
            self.view3j--;
            //
            self.view4.frame=CGRectMake(self.view4.frame.origin.x, self.view4.frame.origin.y, 30, 30);
            [self.view addSubview:self.view4];
            self.bianXing1=2;
        }else if (self.bianXing1==2) {
            self.view1.frame=CGRectMake(self.view1.frame.origin.x, self.view1.frame.origin.y, 30, 30);
            [self.view addSubview:self.view1];
            
            self.view2.frame=CGRectMake(self.view2.frame.origin.x, self.view2.frame.origin.y, 30, 30);
            [self.view addSubview:self.view2];
            
            self.view3.frame=CGRectMake(self.view3.frame.origin.x, self.view3.frame.origin.y, 30, 30);
            [self.view addSubview:self.view3];
            
            self.view4.frame=CGRectMake(self.view4.frame.origin.x-30, self.view4.frame.origin.y+30, 30, 30);
            [self.view addSubview:self.view4];
            //
            self.view4i--;
            self.view4j++;
            //
            self.bianXing1=3;
        }
        else if (self.bianXing1==3) {
            self.view1.frame=CGRectMake(self.view1.frame.origin.x+30, self.view1.frame.origin.y+30, 30, 30);
            [self.view addSubview:self.view1];
            //
            self.view1i++;
            self.view1j++;
            //
            
            self.view2.frame=CGRectMake(self.view2.frame.origin.x, self.view2.frame.origin.y, 30, 30);
            [self.view addSubview:self.view2];
            
            self.view3.frame=CGRectMake(self.view3.frame.origin.x, self.view3.frame.origin.y, 30, 30);
            [self.view addSubview:self.view3];
            
            self.view4.frame=CGRectMake(self.view4.frame.origin.x, self.view4.frame.origin.y, 30, 30);
            [self.view addSubview:self.view4];
            self.bianXing1=4;
        }
        else if (self.bianXing1==4) {
            self.view1.frame=CGRectMake(self.view1.frame.origin.x-30, self.view1.frame.origin.y-30, 30, 30);
            [self.view addSubview:self.view1];
            //
            self.view1i--;
            self.view1j--;
            //
            
            self.view2.frame=CGRectMake(self.view2.frame.origin.x, self.view2.frame.origin.y, 50, 30);
            [self.view addSubview:self.view2];
            
            
            self.view3.frame=CGRectMake(self.view3.frame.origin.x+30, self.view3.frame.origin.y+30, 30, 30);
            [self.view addSubview:self.view3];
            //
            self.view3i++;
            self.view3j++;
            //
            
            self.view4.frame=CGRectMake(self.view4.frame.origin.x+30, self.view4.frame.origin.y-30, 30, 30);
            [self.view addSubview:self.view4];
            //
            self.view4i++;
            self.view4j--;
            //
            self.bianXing1=1;
        }
    }else if (self.fangkuaiKind==2){
        if (self.bianXing1==1) {
            self.view1.frame=CGRectMake(self.view1.frame.origin.x-60, self.view1.frame.origin.y+60, 30, 30);
            [self.view addSubview:self.view1];
            //
            self.view1i-=2;
            self.view1j+=2;
            //
            
            self.view2.frame=CGRectMake(self.view2.frame.origin.x-30, self.view2.frame.origin.y+30, 30, 30);
            [self.view addSubview:self.view2];
            //
            self.view2i-=1;
            self.view2j+=1;
            //
            
            self.view3.frame=CGRectMake(self.view3.frame.origin.x, self.view3.frame.origin.y, 30, 30);
            [self.view addSubview:self.view3];

            self.view4.frame=CGRectMake(self.view4.frame.origin.x-30, self.view4.frame.origin.y-30, 30, 30);
            [self.view addSubview:self.view4];
            //
            self.view4i-=1;
            self.view4j-=1;
            //
            self.bianXing1=2;
            
        }else if (self.bianXing1==2) {
            self.view1.frame=CGRectMake(self.view1.frame.origin.x+60, self.view1.frame.origin.y+60, 30, 30);
            [self.view addSubview:self.view1];
            //
            self.view1i+=2;
            self.view1j+=2;
            //
            
            self.view2.frame=CGRectMake(self.view2.frame.origin.x+30, self.view2.frame.origin.y+30, 30, 30);
            [self.view addSubview:self.view2];
            //
            self.view2i+=1;
            self.view2j+=1;
            //
            
            self.view3.frame=CGRectMake(self.view3.frame.origin.x, self.view3.frame.origin.y, 30, 30);
            [self.view addSubview:self.view3];
            
            
            self.view4.frame=CGRectMake(self.view4.frame.origin.x-30, self.view4.frame.origin.y+30, 30, 30);
            [self.view addSubview:self.view4];
            //
            self.view4i-=1;
            self.view4j+=1;
            //
            self.bianXing1=3;
        }
        else if (self.bianXing1==3) {
            self.view1.frame=CGRectMake(self.view1.frame.origin.x+60, self.view1.frame.origin.y-60, 30, 30);
            [self.view addSubview:self.view1];
            //
            self.view1i+=2;
            self.view1j-=2;
            //
            
            self.view2.frame=CGRectMake(self.view2.frame.origin.x+30, self.view2.frame.origin.y-30, 30, 30);
            [self.view addSubview:self.view2];
            //
            self.view2i+=1;
            self.view2j-=1;
            //
            
            self.view3.frame=CGRectMake(self.view3.frame.origin.x, self.view3.frame.origin.y, 30, 30);
            [self.view addSubview:self.view3];

            
            self.view4.frame=CGRectMake(self.view4.frame.origin.x+30, self.view4.frame.origin.y+30, 30, 30);
            [self.view addSubview:self.view4];
            //
            self.view4i+=1;
            self.view4j+=1;
            //
            self.bianXing1=4;
        }
        else if (self.bianXing1==4) {
            self.view1.frame=CGRectMake(self.view1.frame.origin.x-60, self.view1.frame.origin.y-60, 30, 30);
            [self.view addSubview:self.view1];
            //
            self.view1i-=2;
            self.view1j-=2;
            //
            
            self.view2.frame=CGRectMake(self.view2.frame.origin.x-30, self.view2.frame.origin.y-30, 30, 30);
            [self.view addSubview:self.view2];
            //
            self.view2i-=1;
            self.view2j-=1;
            //

            
            self.view3.frame=CGRectMake(self.view3.frame.origin.x, self.view3.frame.origin.y, 30, 30);
            [self.view addSubview:self.view3];
            
            
            self.view4.frame=CGRectMake(self.view4.frame.origin.x+30, self.view4.frame.origin.y-30, 30, 30);
            [self.view addSubview:self.view4];
            //
            self.view4i+=1;
            self.view4j-=1;
            //
            self.bianXing1=1;
        }
        
    }else if (self.fangkuaiKind==3){
        
        if (self.bianXing1==1) {
            self.view1.frame=CGRectMake(self.view1.frame.origin.x+60, self.view1.frame.origin.y, 30, 30);
            [self.view addSubview:self.view1];
            //
            self.view1i+=2;
            self.view1j+=0;
            //
            
            self.view2.frame=CGRectMake(self.view2.frame.origin.x, self.view2.frame.origin.y+60, 30, 30);
            [self.view addSubview:self.view2];
            //
            self.view2i-=0;
            self.view2j+=2;
            //
            
            self.view3.frame=CGRectMake(self.view3.frame.origin.x, self.view3.frame.origin.y, 30, 30);
            [self.view addSubview:self.view3];
            
            self.view4.frame=CGRectMake(self.view4.frame.origin.x, self.view4.frame.origin.y, 30, 30);
            [self.view addSubview:self.view4];

            self.bianXing1=2;
            
        }else if (self.bianXing1==2) {
            self.view1.frame=CGRectMake(self.view1.frame.origin.x-60, self.view1.frame.origin.y, 30, 30);
            [self.view addSubview:self.view1];
            //
            self.view1i-=2;
            self.view1j+=0;
            //
            
            self.view2.frame=CGRectMake(self.view2.frame.origin.x, self.view2.frame.origin.y-60, 30, 30);
            [self.view addSubview:self.view2];
            //
            self.view2i-=0;
            self.view2j-=2;
            //
            
            self.view3.frame=CGRectMake(self.view3.frame.origin.x, self.view3.frame.origin.y, 30, 30);
            [self.view addSubview:self.view3];
            
            self.view4.frame=CGRectMake(self.view4.frame.origin.x, self.view4.frame.origin.y, 30, 30);
            [self.view addSubview:self.view4];
            self.bianXing1=1;
        }
        
    }else if (self.fangkuaiKind==4){
        if (self.bianXing1==1) {
            self.view1.frame=CGRectMake(self.view1.frame.origin.x, self.view1.frame.origin.y, 30, 30);
            [self.view addSubview:self.view1];
            
            
            self.view2.frame=CGRectMake(self.view2.frame.origin.x+30, self.view2.frame.origin.y-30, 30, 30);
            [self.view addSubview:self.view2];
            //
            self.view2i+=1;
            self.view2j-=1;
            //
            
            self.view3.frame=CGRectMake(self.view3.frame.origin.x+60, self.view3.frame.origin.y-60, 30, 30);
            [self.view addSubview:self.view3];
            //
            self.view3i+=2;
            self.view3j-=2;
            //
            
            self.view4.frame=CGRectMake(self.view4.frame.origin.x+90, self.view4.frame.origin.y-90, 30, 30);
            [self.view addSubview:self.view4];
            //
            self.view4i+=3;
            self.view4j-=3;
            //
            self.bianXing1=2;
        }else if (self.bianXing1==2){
            self.view1.frame=CGRectMake(self.view1.frame.origin.x, self.view1.frame.origin.y, 30, 30);
            [self.view addSubview:self.view1];
            
            
            self.view2.frame=CGRectMake(self.view2.frame.origin.x-30, self.view2.frame.origin.y+30, 30, 30);
            [self.view addSubview:self.view2];
            //
            self.view2i-=1;
            self.view2j+=1;
            //
            
            self.view3.frame=CGRectMake(self.view3.frame.origin.x-60, self.view3.frame.origin.y+60, 30, 30);
            [self.view addSubview:self.view3];
            //
            self.view3i-=2;
            self.view3j+=2;
            //
            
            self.view4.frame=CGRectMake(self.view4.frame.origin.x-90, self.view4.frame.origin.y+90, 30, 30);
            [self.view addSubview:self.view4];
            //
            self.view4i-=3;
            self.view4j+=3;
            //
            self.bianXing1=1;
        }
        
    }
    NSLog(@"%d,%d",self.view1i,self.view1j);
    NSLog(@"%d,%d",self.view2i,self.view2j);
    NSLog(@"%d,%d",self.view3i,self.view3j);
    NSLog(@"%d,%d",self.view4i,self.view4j);
    NSLog(@"/////////");
    
}
- (IBAction)leftBtn:(UIButton *)sender {
    self.view1.frame=CGRectMake(self.view1.frame.origin.x-30, self.view1.frame.origin.y, 30, 30);
    [self.view addSubview:self.view1];
    //
    self.view1i--;
    
    self.view2.frame=CGRectMake(self.view2.frame.origin.x-30, self.view2.frame.origin.y, 30, 30);
    [self.view addSubview:self.view2];
    //
    self.view2i--;
    
    self.view3.frame=CGRectMake(self.view3.frame.origin.x-30, self.view3.frame.origin.y, 30, 30);
    [self.view addSubview:self.view3];
    //
    self.view3i--;
    
    self.view4.frame=CGRectMake(self.view4.frame.origin.x-30, self.view4.frame.origin.y, 30, 30);
    [self.view addSubview:self.view4];
    //
    self.view4i--;
}

- (IBAction)right:(UIButton *)sender {
    self.view1.frame=CGRectMake(self.view1.frame.origin.x+30, self.view1.frame.origin.y, 30, 30);
    [self.view addSubview:self.view1];
    //
    self.view1i++;
    
    self.view2.frame=CGRectMake(self.view2.frame.origin.x+30, self.view2.frame.origin.y, 30, 30);
    [self.view addSubview:self.view2];
    //
    self.view2i++;
    
    self.view3.frame=CGRectMake(self.view3.frame.origin.x+30, self.view3.frame.origin.y, 30, 30);
    [self.view addSubview:self.view3];
    //
    self.view3i++;
    
    self.view4.frame=CGRectMake(self.view4.frame.origin.x+30, self.view4.frame.origin.y, 30, 30);
    [self.view addSubview:self.view4];
    //
    self.view4i++;
    
    NSLog(@"%d,%d",self.view1i,self.view1j);
    NSLog(@"%d,%d",self.view2i,self.view2j);
    NSLog(@"%d,%d",self.view3i,self.view3j);
    NSLog(@"%d,%d",self.view4i,self.view4j);
    NSLog(@"/////////");
}

- (IBAction)pouse:(UIButton *)sender {
    if (self.pauseNum==false) {
        [self.time1 setFireDate:[NSDate distantFuture]];
        self.pauseNum=true;

    }else if(self.pauseNum==true){
        [self.time1 setFireDate:[NSDate distantPast]];
        self.pauseNum=false;


    }
}
@end


//
//  kuai.m
//  erKinf
//
//  Created by root1 on 18-1-7.
//  Copyright (c) 3018年 root. All rights reserved.
//

#import "kuai.h"

@implementation kuai

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        //.frame=CGRectMake(0, 50, 50, 50);
        int vv= (arc4random()%4)+1;
        if (vv==1) {
            self.backgroundColor=[UIColor grayColor];

        }else if(vv==2){
            self.backgroundColor=[UIColor greenColor];

            
        }else if (vv==3){
            self.backgroundColor=[UIColor orangeColor];

            
        }else if (vv==4){
            self.backgroundColor=[UIColor brownColor];

            
        }
    }
    return self;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end



你可能感兴趣的:(iOS,unity)