91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

拼圖游戲之 IOS 版(用swift語言開發的)

發布時間:2020-06-27 10:46:32 來源:網絡 閱讀:2584 作者:切你小JJ 欄目:移動開發

因為公司項目需要,最近在學習IOS開發,為了鞏固我的學習,我想著能不能把以前用C# 開發的拼圖游戲移植到 Iphone 上呢,然后我就開始了這一移植的歷程,因為這是第二次做這個游戲了,所以想的也就比較深入,對以前的算法進行了優化,借助系統提供的API又節省了不少代碼。個人感覺這個游戲還是挺成功的。

  界面設計:

拼圖游戲之 IOS 版(用swift語言開發的)


開始之后:

拼圖游戲之 IOS 版(用swift語言開發的)

算法:

  1. 自定義一個控件,有兩個屬性 ImageTag 和ImageIndex,ImageTag用來存儲控件的位置,ImageIndex 用來存儲圖片的序號,tag 的取值范圍是0  - 15

  2. 一張圖片分割成4 *4 =16 張小圖,從 0 開始標號,初始的時候,tag 和index相等的,

  3. 當向下滑動時,它與它的ImageTag +4的圖片互換,

  4. 當向上的時間 ,取p_w_picpathTag-4,

  5. 當向左的時候,取ImageTag-1, 

  6. 當向右的時候,取p_w_picpathTag+1,


  7. 改變圖片后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
            //dicImages.objectForKey(String(myImageView.p_w_picpathIndex)) as UIImage
            v_img.p_w_picpathView.p_w_picpath=tempImage
            v_img.p_w_picpathIndex=myImageView.p_w_picpathIndex
            myImageView.p_w_picpathIndex=0
            myImageView.p_w_picpathView.p_w_picpath=nil
        }
        
        //up -4 >=0
        //down +4 <=15
        //left -1>=0
        //right +1<=15
        
        //檢測是否完成
        var indexValue=0
        var isComplate:Bool = false
        
        while (indexValue < self.v_allImageViews.subviews.count)
        {
            var tempView = self.v_allImageViews.subviews[myTag + num]  as PImageView
            if !tempView.checkTagAndIndexValueIsSame()
            {
                return
            }
            indexValue++
        }
        //彈出消息,完成拼圖
        
        NSLog("p_w_picpath Tag:%d ,direction:%@", myImageView.p_w_picpathTag,directionStr)
    
    }
}

每移動一步就檢測一次.當遇到第一個沒復位的就反回,如果所有都 復位了則游戲完成了.


代碼請看附件


附件:http://down.51cto.com/data/2365039
向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

大荔县| 西峡县| 景洪市| 宽城| 宁蒗| 乌什县| 竹溪县| 洮南市| 北票市| 乐山市| 霸州市| 奉节县| 成安县| 商丘市| 湘潭市| 洛扎县| 读书| 怀柔区| 苍南县| 万源市| 靖远县| 鄂托克旗| 平阴县| 永新县| 灵石县| 肇州县| 石河子市| 芒康县| 江津市| 松原市| 霸州市| 卢氏县| 海兴县| 南雄市| 崇文区| 施甸县| 明水县| 利津县| 叙永县| 金平| 任丘市|