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;

                    }];

                }

            }

        }

        

     

    }

     

    让明天,不后悔今天的所作所为
  • 相关阅读:
    推荐文章:深入浅出REST
    推荐Fowler作序的新书《xUnit Test Patterns》
    测试替身:Test Double
    踢毽也能治胃病,适当的运动带来健康,健康带来快乐
    10分钟入门AOP:用PostSharp普及一下AOP
    推荐一本新的英文版算法书和一本讲debug的书
    emacs开发rails的演示
    转:一个土木工程师在四川地震灾后的思考
    多语言多范型编程PPP
    巧用editplus学习正则表达式
  • 原文地址:https://www.cnblogs.com/-yun/p/4367011.html
Copyright © 2011-2022 走看看