zoukankan      html  css  js  c++  java
  • 拼图

     

      

    #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;

                    }];

                }

            }

        }

        

     

    }

     

    让明天,不后悔今天的所作所为
  • 相关阅读:
    BZOJ 1911: [Apio2010]特别行动队
    BZOJ 1096: [ZJOI2007]仓库建设(动态规划+斜率优化)
    BZOJ 2243: [SDOI2011]染色(树链剖分)
    BZOJ 1834: [ZJOI2010]network 网络扩容(网络流+费用流)
    BZOJ 1036: [ZJOI2008]树的统计Count(树链剖分)
    BZOJ 1875: [SDOI2009]HH去散步(矩阵乘法)
    BZOJ 1898: [Zjoi2004]Swamp 沼泽鳄鱼(矩阵乘法)
    BZOJ 2463: [中山市选2009]谁能赢呢?(博弈论)
    BZOJ 2879: [Noi2012]美食节
    BZOJ 1070: [SCOI2007]修车(费用流)
  • 原文地址:https://www.cnblogs.com/-yun/p/4367011.html
Copyright © 2011-2022 走看看