如何保存Imageview的原始位置并将其与同一Imageview的新位置进行比较?

时间:2014-10-10 08:21:35

标签: ios swift

我知道这是个愚蠢的问题。 我正在开发一个益智游戏,代码就是:

import UIKit


class ViewController: UIViewController {

    var allImageViews : [UIImageView] = []
    var allCenters : NSMutableArray = NSMutableArray()
    var emptySpot : CGPoint = CGPoint()
    var originalCenters : NSMutableArray = NSMutableArray()

    override func viewDidLoad() {
        super.viewDidLoad()

        // Center point for piece of image for iPhone 5S simulator
        var xCen : CGFloat = 53
        var yCen : CGFloat = 53

        // arrangement of 3*3 piece of images
        for v in 0...2{

            for h in 0...2{
                var pieces = [[0, 1, 2], [3, 4, 5], [6, 7, 8]]
                let filename = String(format: "Smile_%02i.gif", h+v*3)
                let Image = UIImage(named: filename)
                let myImageView = UIImageView(image: Image)
                var curCenter : CGPoint = CGPointMake(xCen, yCen)

                //adding centerpoint to allCenter array
                allCenters.addObject(NSValue(CGPoint : curCenter))
                myImageView.frame = CGRectMake(0, 0, 106, 106)
                myImageView.center = curCenter
                myImageView.userInteractionEnabled = true

                //adding all Images in array allImageviews
                allImageViews.append(myImageView)
                self.view.addSubview(myImageView)

                //Increment on X-Axis
                xCen += 106
            }
            //again sex X centerpoint to 53
            xCen = 53
            //Increment on Y-Axis
            yCen += 106
        }
        // romove one piece at index 0
        allImageViews.removeAtIndex(0).removeFromSuperview()
        // we have all 8 imageview and all 9 centerpoints
        self.randomizeBlocks()

    }
    // placing the piece of images randomly
    func randomizeBlocks(){

        // new array that copy centerpoint from allCenters
        var centersCopy: NSMutableArray = allCenters.mutableCopy() as NSMutableArray

        var randLocInt : Int
        var randLoc : CGPoint

        // loop for all peice of images saved in allImageViews
        for i in allImageViews {
            //Place Images randomly
            randLocInt = Int(arc4random()) % centersCopy.count
            randLoc  = centersCopy.objectAtIndex(randLocInt).CGPointValue()
            i.center = randLoc
            centersCopy.removeObjectAtIndex(randLocInt)

        }

        // for finding empyblock which is at index 0
        emptySpot = centersCopy.objectAtIndex(0).CGPointValue()

    }
    var leftisEmpty : Bool = Bool()
    var rightisEmpty : Bool = Bool()
    var topisEmpty : Bool = Bool()
    var bottomisEmpty : Bool = Bool()


    // touch event
    override func touchesEnded(touches: NSSet, withEvent event: UIEvent) {
        var topCen : CGPoint
        var left : CGPoint
        var right : CGPoint
        var top : CGPoint
        var bottom : CGPoint

        let touches = touches.allObjects as [UITouch]
        var myTouch = touches.first

        if let myTouch = myTouch{

            topCen = myTouch.view.center

            // movement to empty block
            left = CGPointMake(topCen.x - 106, topCen.y)
            right = CGPointMake(topCen.x + 106, topCen.y)
            top = CGPointMake(topCen.x, topCen.y+106)
            bottom = CGPointMake(topCen.x, topCen.y-106)

            // Deciding which side is empty
            if emptySpot  == left{
                leftisEmpty = true
            }
            if emptySpot  == right{
                rightisEmpty = true
            }
            if emptySpot  == top{
                topisEmpty = true
            }
            if emptySpot  == bottom{
                bottomisEmpty = true
            }

            if leftisEmpty || rightisEmpty || topisEmpty || bottomisEmpty{


                UIView.beginAnimations(nil, context:nil)

                UIView.setAnimationDuration(0.5)
                myTouch.view.center = emptySpot
                UIView.commitAnimations()
                emptySpot = topCen

                leftisEmpty = false
                rightisEmpty = false
                topisEmpty = false
                bottomisEmpty = false
            }
        }
    }
}

现在我被困在用户完成整个画面的时候我怎么能比较所有图像的位置,所以应该出现警告信息并说'#34;游戏已经完成"。我已经没有'我知道怎么能这样做。

请帮我完成这个游戏。

提前致谢。

3 个答案:

答案 0 :(得分:1)

你应该有一个3x3阵列,它保存每个位置当前拼图的编号。最初,这将是

var pieces = [[0, 1, 2], [3, 4, 5], [6, 7, 8]]

其中0表示空白点。每次移动拼图时更新该阵列,例如, 当#3件向上移动时

pieces = [[3, 1, 2], [0, 4, 5], [6, 7, 8]]

如果pieces再次等于初始数组,游戏就完成了。

答案 1 :(得分:1)

您的代码有变更和解释: -

override func viewDidLoad() {
    super.viewDidLoad()
            ........YOUR CODE......


     //READ below.......
     var tagValue: NSInteger = 0  //change it as me typing directly in post.
     var imageDict: Dictionary<String, String> = [:]
     var posStateDict: Dictionary<String, String> = [:] //Change it as per swift format.

     var frame: CGPoint = CGPointZero

    for v in 0...2{

        for h in 0...2{

            ........YOUR CODE......

            //......UPDATED.......
            //Now you should also add a tag value to your imageView

            myImageView.tag = tagValue
             myImageView.userInteractionEnabled = true

             frame = CGPointMake(xCen, yCen)
             imageDict.setValue(NSStringFromCGPoint(frame), forKey: NSString.stringWithString("/(tagValue)"));    

             posStateDict[NSString.stringWithString("(d)")] = ["false"]                
             tagValue = tagValue + 1;    //Incrementing tagValue.

        }
                       ........YOUR CODE......
   }
                  ........YOUR CODE......
}

// touch event
override func touchesEnded(touches: NSSet, withEvent event: UIEvent) {

                  ........YOUR CODE......

        if leftisEmpty || rightisEmpty || topisEmpty || bottomisEmpty{

                  ........YOUR CODE......


            //READ below.......Rest code here.
            //Here you need to get that imageView object which is moved.
            //From that imageView you need to get tag value. 
            //With the tag value you could search it's orignal frame from the imageDict and compare the frame of it.
            //If frame match then set their respective object true in posStateDict dictionary.
            //Now check this dictionary if all true and no false then game is over.
        }
    }
}

}

很抱歉,由于有一些工作,我无法为该部件编码。希望这个解释能帮到你。

答案 2 :(得分:0)

也许将原始imageView中心保存在一个数组中,然后将其与新位置进行比较?