• 拼图


     

      

    #import "ViewController.h"

     

    @interface ViewController ()

     

    @end

     

    @implementation ViewController

     

    - (UIImage *)clipImage:(UIImage *)image inRect:(CGRect)rect

    {//返回image中rect范围内的图片

        CGImageRef imageRef = CGImageCreateWithImageInRect(image.CGImage, rect);

        UIImage *subImage = [UIImage imageWithCGImage:imageRef];

        return subImage;

    }

     

     

     

    - (void)viewDidLoad

    {

        [super viewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

        self.view.backgroundColor =[UIColor cyanColor];

     

        UIImageView *iv=[[UIImageView alloc]initWithFrame:CGRectMake(8, self.view.bounds.size.height-180, 150, 150)];

        [iv setImage:[UIImage imageNamed:@"king2"]];

        [self.view addSubview:iv];

        

        UIButton *btn=[UIButton buttonWithType:UIButtonTypeSystem];

        btn.frame=CGRectMake(200, 400, 100, 50);

        [btn setTitle:@"恢复" forState:UIControlStateNormal];

        btn.backgroundColor=[UIColor orangeColor];

        [self.view addSubview:btn];

        [btn addTarget:self action:@selector(nc) forControlEvents:UIControlEventTouchUpInside];

       

        

        

        [self addpic];

        

        

        

    }

    -(void)nc

    {

        for (int i=0; i<9; i++) {

            UIImageView *iv=(UIImageView *)[self.view viewWithTag:100+i];

            [UIImageView animateWithDuration:1 animations:^{

                iv.frame=CGRectMake((i%3)*102+2, (i/3)*102+2, 100, 100);

            }];

        }

    }

     

    -(void)addpic

    {

        UIView *view=[[UIView alloc]initWithFrame:CGRectMake(6, 60, 308, 308)];

        view.backgroundColor=[UIColor orangeColor];

        [self.view addSubview:view];

        

        for (int i=0; i<9; i++) {

            UIImageView *a=[[UIImageView alloc]initWithFrame:CGRectMake((i%3)*102+2, (i/3)*102+2, 100, 100)];

            a.image=[self clipImage:[UIImage imageNamed:@"king4"] inRect:CGRectMake((i%3)*100, (i/3)*100, 100, 100)];

            [view addSubview:a];

            

        }

        

        

        for (int i=0; i<9; i++) {

            UIImageView *qview=[[UIImageView alloc]initWithFrame:CGRectMake((i%3)*102+2, (i/3)*102+2, 100, 100)];

            qview.image=[self clipImage:[UIImage imageNamed:@"king2"] inRect:CGRectMake((i%3)*100, (i/3)*100, 100, 100)];

            qview.userInteractionEnabled=YES;

            qview.tag=100+i;

            

            if (i==8) {

                qview.image =  [UIImage imageNamed:@"king3"];

                

            }

            

            [view addSubview:qview];

     

     

        }

        

        

    }

     

    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

    {

        //获取触摸视图

        UITouch *touch =[touches anyObject];

        //NSLog(@"22222");

        if ([touch.view isKindOfClass:[UIImageView class]]) {

            UIImageView *kong = (UIImageView *)[self.view viewWithTag:108];

            //NSLog(@"%@",NSStringFromCGPoint(kong));

            CGPoint temp = touch.view.center;

            if (kong.center.y == touch.view.center.y) {

                if (kong.center.x - touch.view.center.x ==102 ||kong.center.x - touch.view.center.x ==-102 ) {

                    [UIView animateWithDuration:1 animations:^{

                        touch.view.center = kong.center;

                        kong.center = temp;

                                        }];

                }

            }

            if (kong.center.x == touch.view.center.x) {

                if (kong.center.y - touch.view.center.y ==102 ||kong.center.y - touch.view.center.y ==-102 ) {

                    [UIView animateWithDuration:1 animations:^{

                        touch.view.center = kong.center;

                        kong.center = temp;

                    }];

                }

            }

        }

        

     

    }

     

    让明天,不后悔今天的所作所为
  • 相关阅读:
    SQL SERVER DBCC命令参考
    Sqlserver 死锁问题
    收集面试题目DB
    收集面试题目Net
    [转]Virtual PC 2007虚拟机上安装Ubuntu 8.10桌面版
    Tcl/tk基础-2
    【转】内存详解
    [转]C# P2P与NAT技术之二
    泛型跟KeyNotFoundException
    用InstallAware 9制作BDE安装程序
  • 原文地址:https://www.cnblogs.com/-yun/p/4367011.html
Copyright © 2020-2023  润新知