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;

                    }];

                }

            }

        }

        

     

    }

     

    让明天,不后悔今天的所作所为
  • 相关阅读:
    苹果推送APNS自己总结
    Windows下LDAP服务器配置
    service mysql start出错,mysql启动不了,解决mysql: unrecognized service错误
    PHP 中的 9 个魔术方法
    使用 Equinox 开发 OSGi 应用程序
    使用 Equinox 框架进行 OSGi 环境下的 Web 开发
    探索 OSGi 框架的组件运行机制
    基于 OSGi 和 Spring 开发 Web 应用
    Equinox OSGI ServletBridge 原理与实践
    利用 Eclipse 开发基于 OSGi 的 Bundle 应用
  • 原文地址:https://www.cnblogs.com/-yun/p/4367011.html
Copyright © 2011-2022 走看看