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

}];

}

}

}

}

转自:http://www.cnblogs.com/-yun/p/4367011.html

iOS拼图的更多相关文章

  1. iOS 滑块拼图游戏(Puzzle8)

    代码地址如下:http://www.demodashi.com/demo/11505.html 一.准备工作 先了解一个定义和定理 定义:在一个1,2,...,n的排列中,如果一对数的前后位置与大小顺 ...

  2. iOS学习笔记-精华整理

    iOS学习笔记总结整理 一.内存管理情况 1- autorelease,当用户的代码在持续运行时,自动释放池是不会被销毁的,这段时间内用户可以安全地使用自动释放的对象.当用户的代码运行告一段 落,开始 ...

  3. iOS学习笔记总结整理

    来源:http://mobile.51cto.com/iphone-386851_all.htm 学习IOS开发这对于一个初学者来说,是一件非常挠头的事情.其实学习IOS开发无外乎平时的积累与总结.下 ...

  4. iOS网络加载图片缓存策略之ASIDownloadCache缓存优化

    iOS网络加载图片缓存策略之ASIDownloadCache缓存优化   在我们实际工程中,很多情况需要从网络上加载图片,然后将图片在imageview中显示出来,但每次都要从网络上请求,会严重影响用 ...

  5. iOS小结

    一.内存管理情况 1- autorelease,当用户的代码在持续运行时,自动释放池是不会被销毁的,这段时间内用户可以安全地使用自动释放的对象.当用户的代码运行告一段 落,开始等待用户的操作,自动释放 ...

  6. ios的一些知识点

    ios的一些知识点 一 非ARC的内存管理情况 1-autorelease,当用户的代码在持续运行时,自动释放池是不会被销毁的,这段时间内用户可以安全地使用自动释放的对象.当用户的代码运行告一段落,开 ...

  7. (转载)ios的一些知识点

    ios的一些知识点 一 非ARC的内存管理情况  1-autorelease,当用户的代码在持续运行时,自动释放池是不会被销毁的,这段时间内用户可以安全地使用自动释放的对象.当用户的代码运行告一 段落 ...

  8. CallKit iOS 教程

    原文:CallKit Tutorial for iOS 作者:József Vesza 译者:kmyhy 对 VoIP App 开发者来说,iOS 的支持并不友好.尤其是它的通知发送这一块,太糙了.你 ...

  9. iOS审核拒绝苹果官方原因详解

    1.1不当内容应用程序不应该包括攻击性,敏感,令人不悦,侮辱或者品味低下的内容.例如: 1.1.1 诽谤或者人格侮辱的内容,包括引用或者评论宗教.种族.性取向.性别或者其他目标群体的内容,特别是该应用 ...

随机推荐

  1. 【遍历二叉树】03二叉树的后序遍历【Binary Tree Postorder Traversal】

    ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 给定一个二叉树,返回他的后序遍历的 ...

  2. poj2395

      #include<iostream> #include<cstdio> #include<algorithm> #include<cstdlib> ...

  3. Maven: 自动远程部署

    1. 在settings.xml中的Servers节点中增加Server的登录信息: <server> <id>deploy_server_65</id> < ...

  4. BZOJ3064:CPU监控

    浅谈区间最值操作和历史最值问题:https://www.cnblogs.com/AKMer/p/10225100.html 题目传送门:https://lydsy.com/JudgeOnline/pr ...

  5. POJ2559:Largest Rectangle in a Histogram

    浅谈栈:https://www.cnblogs.com/AKMer/p/10278222.html 题目传送门:http://poj.org/problem?id=2559 贪心的想,最大的子矩阵顶部 ...

  6. EventLoop 与 Channel 的关联

    Netty 中, 每个 Channel 都有且仅有一个 EventLoop 与之关联, 它们的关联过程如下: 从上图中我们可以看到, 当调用了 AbstractChannel#AbstractUnsa ...

  7. mariadb复制

    实验环境:CentOS7 #安装mariadb-server #主数据库: [root@~ localhost]#vim /etc/my.cnf.d/server.cnf [mysqld] skip_ ...

  8. [.net]手机APP与IIS服务器联调配置

    前端时间写过一段时间接口,在后期的时候,出现了一些无法通过查看日志来找出问题所在的bug.于是,将手机APP连接到IIS服务器上进行调试,下面是配置的具体步骤 1. 配置IIS  添加网站,将物理路径 ...

  9. <正则吃饺子> :关于mybatis中使用的问题(一)

    在公司项目开始之前,根据springboot .mybatis.Swagger2 整合了一个demo,在测试时候,遇到的问题,简单记录.之前在使用mybatis时候,没有注意到这一点. 1.错误:Th ...

  10. 0011_练习题d1

    __author__ = 'qq593' #!/usr/bin/env python #-*- coding:utf-8 -*- #使用while循环输入1 2 3 4 5 6 8 9 10 a=1 ...