【移动应用开发技术】拼图游戏之 IOS 版(用swift语言开发的)_第1页
【移动应用开发技术】拼图游戏之 IOS 版(用swift语言开发的)_第2页
【移动应用开发技术】拼图游戏之 IOS 版(用swift语言开发的)_第3页
【移动应用开发技术】拼图游戏之 IOS 版(用swift语言开发的)_第4页
【移动应用开发技术】拼图游戏之 IOS 版(用swift语言开发的)_第5页
已阅读5页,还剩9页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

【移动应用开发技术】拼图游戏之IOS版(用swift语言开发的)

因为公司项目需要,最近在学习IOS开发,为了巩固我的学习,我想着能不能把以前用C#开发的拼图游戏移植到Iphone上呢,然后我就开始了这一移植的历程,因为这是第二次做这个游戏了,所以想的也就比较深入,对以前的算法进行了优化,借助系统提供的API又节省了不少代码。个人感觉这个游戏还是挺成功的。

界面设计:开始之后:算法:自定义一个控件,有两个属性ImageTag和ImageIndex,ImageTag用来存储控件的位置,ImageIndex用来存储图片的序号,tag的取值范围是0

-15一张图片分割成4*4=16张小图,从0开始标号,初始的时候,tag和index相等的,当向下滑动时,它与它的ImageTag+4的图片互换,当向上的时间,取p_w_picpathTag-4,当向左的时候,取ImageTag-1,

当向右的时候,取p_w_picpathTag+1,改变图片后index也跟着改变,tag不变,用tag==index来判断图片是否复位,当所有的图片都复位了,拼图也就完成了我的想法是能不能利用系统提供的手势来移动UIImageView控件呢,然后我就写了一个测试程序,发现手势操作只能控制View的移动,不能移动UIImageView,所以要改变一下思路,把一个UIImageView控件加到一个View中,做成一个自定义控件,问题解决:import

Foundation

import

UIKit

class

PImageView:UIView

{

//

init(p_w_picpath:

UIImage!)

//

{

//

//

super.init(p_w_picpath:p_w_picpath)

//

}

var

parentView:ViewController!

var

p_w_picpathView:UIImageView!

init(frame:

CGRect)

{

super.init(frame:

frame)

var

temp=frame

temp.origin=CGPointMake(0,

0)

p_w_picpathView=UIImageView(frame:temp)

self.addSubview(p_w_picpathView)

var

recognizer1=UISwipeGestureRecognizer()

recognizer1.addTarget(self,

action:"handleSwipeView:")

recognizer1.direction=UISwipeGestureRecognizerDirection.Right

var

recognizer2=UISwipeGestureRecognizer()

recognizer2.addTarget(self,

action:"handleSwipeView:")

recognizer2.direction=UISwipeGestureRecognizerDirection.Left

var

recognizer3=UISwipeGestureRecognizer()

recognizer3.addTarget(self,

action:"handleSwipeView:")

recognizer3.direction=UISwipeGestureRecognizerDirection.Up

var

recognizer4=UISwipeGestureRecognizer()

recognizer4.addTarget(self,

action:"handleSwipeView:")

recognizer4.direction=UISwipeGestureRecognizerDirection.Down

self.addGestureRecognizer(recognizer1)

self.addGestureRecognizer(recognizer2)

self.addGestureRecognizer(recognizer3)

self.addGestureRecognizer(recognizer4)

}

func

handleSwipeView(recognizer:UISwipeGestureRecognizer!)

{

var

dir:String!

if

recognizer.direction.value==UISwipeGestureRecognizerDirection.Down.value

{

dir="Down"

NSLog("move

Down")

}

if

recognizer.direction.value==UISwipeGestureRecognizerDirection.Up.value

{

dir="Up"

NSLog("move

Up")

}

if

recognizer.direction.value==UISwipeGestureRecognizerDirection.Left.value

{

dir="Left"

NSLog("move

Left")

}

if

recognizer.direction.value==UISwipeGestureRecognizerDirection.Right.value

{

dir="Right"

NSLog("move

Right")

}

//NSLog("tag:%d",

self.p_w_picpathTag)

parentView.moveImages(self,

directionStr:

dir)

}

func

initWithTagAndIndex(myTag:Int!

,myIndex:Int!)

{

self.p_w_picpathIndex=myIndex

self.p_w_picpathTag=myTag

}

func

checkTagAndIndexValueIsSame()->Bool

{

return

self.p_w_picpathIndex==self.p_w_picpathTag

}

var

p_w_picpathTag:Int!

var

p_w_picpathIndex:Int!

}这个是自定义View的代码,初始化的时候设置View的iamgeIndex和p_w_picpathTag,设置图片,注册系统手势操作调用它的checkTagAndIndexValueIsSame()方法来检测图片是否复位.我们再看看主界面代码:

import

UIKit

class

ViewController:

UIViewController

{

@IBOutlet

var

v_allImageViews

:

UIView

var

screenWidth:CGFloat!

var

screenHeight:CGFloat!

var

dicImages:NSMutableDictionary!

var

dicIndexValues:NSMutableDictionary!

var

LocationX:Int

=

0

var

locationY:Int=0

override

func

viewDidLoad()

{

super.viewDidLoad()

//

Do

any

additional

setup

after

loading

the

view,

typically

from

a

nib.

//var

p_w_picpathView:UIImageView

//var

p_w_picpath:UIImage

var

myImage:UIImage

self.screenWidth=self.view.frame.size.width

self.screenHeight=self.view.frame.size.height

var

colWidth=(self.screenWidth-24)/4

var

rowHeight=(self.screenHeight-184)/4

myImage=UIImage(named:"7.jpg")

dicImages=NSMutableDictionary()

var

p_w_picpathW=myImage.size.width/4

var

p_w_picpathH=myImage.size.height/4

var

num=0

while(num<16)

{

var

row

=

Float(

num/4)

var

col

=

Float(num%4)

NSLog("row:%.2f

col:%.2f\r\n",

row,col)

var

rect

=

CGRectMake(p_w_picpathW

*

col,

p_w_picpathH

*

row,

p_w_picpathW,

p_w_picpathH)

var

tem=CGRectMake(10+colWidth

*

col+col,

40+rowHeight

*

row+row,

colWidth,

rowHeight)

var

sv=PImageView(frame:

tem)

//

sv.frame.origin=CGPointMake(10+colWidth

*

col+col,

40+rowHeight

*

row+row)

//

sv.frame.size=CGSizeMake(colWidth,

rowHeight)

sv.backgroundColor=UIColor.lightGrayColor()

//

var

frm=CGRect()

//

var

p_w_picpathView=PImageView(frame:frm)

//

p_w_picpathView.frame.origin=CGPointMake(0,

0)

//

p_w_picpathView.frame.size=CGSizeMake(colWidth,

rowHeight)

//

p_w_picpathView.backgroundColor=UIColor.redColor()

sv.initWithTagAndIndex(num,myIndex:

num)

var

p_w_picpath=getImage(myImage,rect:

rect)

if(num==0){

sv.p_w_picpathView.p_w_picpath=p_w_picpath

}

else

{

//var

data

=

UIImagePNGRepresentation(p_w_picpath)

dicImages.setObject(p_w_picpath,

forKey:String(

num))

sv.p_w_picpathView.p_w_picpath=p_w_picpath

}

sv.parentView=self

self.v_allImageViews.addSubview(sv)

//self.view.addSubview(p_w_picpathView)

num++

}

NSLog("dic

count:

%d

",

dicImages.count)

}

override

func

didReceiveMemoryWarning()

{

super.didReceiveMemoryWarning()

//

Dispose

of

any

resources

that

can

be

recreated.

}

func

getImage(img:UIImage,

rect:CGRect)->UIImage

{

var

im:UIImage

var

p_w_picpathPartRef:CGImageRef

var

p_w_picpathRef:CGImageRef

p_w_picpathRef=img.CGImage

p_w_picpathPartRef=CGImageCreateWithImageInRect(p_w_picpathRef,

rect)

im=UIImage(CGImage:

p_w_picpathPartRef)

//CGImageRelease(p_w_picpathPartRef)

return

im

}

@IBOutlet

var

btnStart

:

UIButton

//开始游戏

@IBAction

func

start(sender

:

AnyObject)

{

var

vFrist

=

self.v_allImageViews.subviews[0]

as

PImageView

vFrist.p_w_picpathView.p_w_picpath=nil

dicIndexValues=NSMutableDictionary()

var

num=1

dicIndexValues.setObject(0,

forKey:

0)

var

arr:NSArray

=

["0"]

while(num<self.v_allImageViews.subviews.count)

{

var

myindex=arc4random()%15+1;

//if(!dicIndexValues.allValues.(String(myindex)))

//var

obj

=

arr.indexOfObject(String(myindex))

if

(!arr.containsObject(String(myindex)))

{

arr

=

arr.arrayByAddingObject(String(myindex))

dicIndexValues.setObject(String(

myindex),

forKey:

num)

NSLog("key

:%d

value:

%@\r\n",num,

String(

myindex))

//var

data

=

dicImages.objectForKey(String(

myindex))

var

v_img

=

self.v_allImageViews.subviews[num]

as

PImageView

v_img.p_w_picpathView.p_w_picpath

=

dicImages.objectForKey(String(myindex))

as

UIImage

v_img.p_w_picpathIndex=Int(

myindex)

num++

//

NSLog("tag:%d

index:%d",

v_img.p_w_picpathTag,v_img.p_w_picpathIndex)

}

}

}

//动画切换图片(没有加入动画,只是简单的移动)

func

moveImages(myImageView:PImageView,directionStr:String!

)

{

var

myTag=myImageView.p_w_picpathTag

let

anotherCharacter:String=directionStr

var

num=0

switch

anotherCharacter

{

case

"Up":

num

=

-4

NSLog("up")

case

"Down":

num

=

4

NSLog("Down")

case

"Left":

num

=

-1

case

"Right":

num

=

1

default:

NSLog("default")

}

//边界检查

if

(myTag

+

num)>=0

&&

(myTag

+

num)

<=

15

{

var

v_img

=

self.v_allImageViews.subviews[myTag

+

num]

as

PImageView

//判断目标位置是否为空

if

v_img.p_w_picpathIndex

!=

0

{

return

}

var

tempImage=myImageView.p_w_picpathView.p_w_picpath

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论