第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定

swift初級(jí)入門--拼圖游戲

標(biāo)簽:
iOS
PuzzleGame

swift拼图

源码链接:https://github.com/HelloYeah/PuzzleGame

最近在自学swift,工作一直用OC,一接触swift,语法上还是有很大的差异。用起来相当不适应。在百度大神的助力下艰辛的把这个拼图demo写出来。

欢迎各路大神斧正。欢迎各位新学swift的一起交流。

效果图如下

2.gif

思路解析

每张可移动的纸片设计为Puzzle类。 Puzzle 有行号,列号,图片三个属性 和 移动方法 func move(direction : Direction)

import UIKit

enum Direction {
    case DirectionUp
    case DirectionDown
    case DirectionLeft
    case DirectionRight
}

class Puzzle: NSObject {

    var  col : NSInteger!
    var  row : NSInteger!
    private(set) var  image : UIImage!

    init(image: UIImage, withCol col: NSInteger, withRow row: NSInteger){
        super.init()
        self.col = col
        self.row = row
        self.image = image
    }

    func move(direction : Direction) {

        switch direction {
        case .DirectionUp:
            row = row - 1
        case .DirectionDown:
            row = row + 1
        case .DirectionLeft:
            col = col - 1
        case .DirectionRight:
            col = col + 1
        }
    }
}

PuzzleView 用于展示Puzzle的视图。每个PuzzleView 拥有一个puzzle属性。

    import UIKit

    class PuzzleView: UIImageView {

        var puzzle : Puzzle?{

            didSet{
                self.image = puzzle?.image
            }
        }

        override init(frame: CGRect) {
            super.init(frame: frame)
            self.layer.borderWidth = 1.0/UIScreen.main.scale
            self.layer.borderColor = UIColor.lightGray.cgColor
            self.isUserInteractionEnabled = true
        }

        required init?(coder aDecoder: NSCoder) {
            fatalError("init(coder:) has not been implemented")
        }
    }

控制器中添加并布局好9个PuzzleView,每个PuzzleView添加点击手势。
emptyPuzzle空白的PuzzleView。每次移动时,通过这个PuzzleView 和点击的PuzzleView对比位置,相邻则可以移动。否则不做任何处理

//emptyPuzzle空白的PuzzleView。每次移动时,通过这个PuzzleView 和点击的PuzzleView对比位置,相邻则可以移动。否则不做任何处理
let emptyPuzzle : Puzzle = Puzzle.init(image: UIImage.init(), withCol: 2, withRow: 2)

func click(tap: UITapGestureRecognizer) {
    let puzzleView = tap.view as! PuzzleView
    let puzzle = puzzleView.puzzle!

    let col = puzzle.col
    let row = puzzle.row

    if col == emptyPuzzle.col  {
        if row! - emptyPuzzle.row == 1 {
            puzzle.move(direction: .DirectionUp)
            puzzleView.frame.origin.y -= height
            emptyPuzzle.move(direction: .DirectionDown)
        }
        if row! - emptyPuzzle.row == -1 {
            puzzle.move(direction: .DirectionDown)
            puzzleView.frame.origin.y += height
            emptyPuzzle.move(direction: .DirectionUp)
        }
    }

    if row == emptyPuzzle.row  {
        if col! - emptyPuzzle.col == 1 {
            puzzle.move(direction: .DirectionLeft)
            puzzleView.frame.origin.x -= width
            emptyPuzzle.move(direction: .DirectionRight)
        }
        if col! - emptyPuzzle.col == -1 {
            puzzle.move(direction: .DirectionRight)
            puzzleView.frame.origin.x += width
            emptyPuzzle.move(direction: .DirectionLeft)
        }
    }
}

总结:有其他好的思路大家有更好的,可以分享一下。

點(diǎn)擊查看更多內(nèi)容
2人點(diǎn)贊

若覺得本文不錯(cuò),就分享一下吧!

評(píng)論

作者其他優(yōu)質(zhì)文章

正在加載中
感謝您的支持,我會(huì)繼續(xù)努力的~
掃碼打賞,你說多少就多少
贊賞金額會(huì)直接到老師賬戶
支付方式
打開微信掃一掃,即可進(jìn)行掃碼打賞哦
今天注冊(cè)有機(jī)會(huì)得

100積分直接送

付費(fèi)專欄免費(fèi)學(xué)

大額優(yōu)惠券免費(fèi)領(lǐng)

立即參與 放棄機(jī)會(huì)
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)

舉報(bào)

0/150
提交
取消