益智游戏中的某些UIImageViews不会移动到新的CGPoint中心

时间:2016-04-22 20:04:27

标签: ios swift uiimageview cgpoint

我在9个等空间的框架中有8个UIImageViews。除了两个(顶行)之外的所有视图在被触摸时移动到emptySpot。无法弄清楚为什么。

(作为旁注,我无法弄清楚为什么我的blockquote中的前几行代码不像其他代码一样格式化/显示代码!?)

import UIKit  
import Foundation  
var tiledViewsStack = [UIImageView]()
var emptySpot = CGPoint()

class PhotoViewController: UIViewController,
UIImagePickerControllerDelegate, UINavigationControllerDelegate {

//Choose an image from Photo Library and display on screen in displayImageView

@IBOutlet weak var displayImageView: UIImageView!

@IBAction func choosePicFromLibrary(sender: AnyObject) {
    let imagePicker: UIImagePickerController = UIImagePickerController()

    imagePicker.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
    imagePicker.delegate = self
    imagePicker.modalPresentationStyle = UIModalPresentationStyle.Popover

    if (imagePicker.popoverPresentationController != nil) {
        imagePicker.popoverPresentationController!.sourceView = sender as! UIButton
        imagePicker.popoverPresentationController!.sourceRect = (sender as! UIButton).bounds
    }
    presentViewController(imagePicker, animated: true, completion: nil)
}

@IBAction func takePhoto(sender: AnyObject) {
    let imagePicker: UIImagePickerController = UIImagePickerController()

    imagePicker.sourceType = UIImagePickerControllerSourceType.Camera
    imagePicker.delegate = self
    imagePicker.modalPresentationStyle = UIModalPresentationStyle.Popover

    if (imagePicker.popoverPresentationController != nil) {
        imagePicker.popoverPresentationController!.sourceView = sender as! UIButton
        imagePicker.popoverPresentationController!.sourceRect = (sender as! UIButton).bounds
    }
    presentViewController(imagePicker, animated: true, completion: nil)
}

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
    dismissViewControllerAnimated(true, completion: nil)
    displayImageView.image = info[UIImagePickerControllerOriginalImage] as! UIImage!
}

func imagePickerControllerDidCancel(picker: UIImagePickerController) {
    dismissViewControllerAnimated(true, completion: nil)
}



//Cut photo into 9 tiles
    //Cut selected image into 9 pieces and add each cropped image to tileImageStack array

var tileImageStack = [AnyObject]()

var allCenters = [NSValue]()

@IBAction func randomize(sender: AnyObject) {

let selectedImageWidth = displayImageView.image!.size.width
let selectedImageHeight = displayImageView.image!.size.height

let tileSize = CGSizeMake(selectedImageWidth/3, selectedImageHeight/3)

    for (var colI = 0; colI < 3; colI++)
    {
        for (var rowI = 0; rowI < 3; rowI += 1)
        {
            let tileRect = CGRectMake(CGFloat(rowI) * tileSize.width, tileSize.height * CGFloat(colI), tileSize.width, tileSize.height)

            if let selectedImage = displayImageView.image
            {
                let tileImage = CGImageCreateWithImageInRect(selectedImage.CGImage, tileRect)
                let aUItile = UIImage(CGImage: tileImage!)
                tileImageStack.append(aUItile)
            }
        }
    }

    //Display tiles in order on screen, then mix them up randomly

    let frameWidth = self.view.frame.width
    let frameHeight = self.view.frame.height

    var xCen = (frameWidth/3)/2
    var yCen = (frameHeight/3)/2

    var pieceNumber = 0

    for (var v = 0; v < 3; v += 1)
    {
        for (var h = 0; h < 3; h += 1)
        {
            let tiledView = UIImageView(frame:CGRectMake(0, 0, frameWidth/3, (frameHeight)/3))
            var curCenter = CGPointMake(xCen, yCen)
            allCenters.append(NSValue(CGPoint: curCenter))
            //tiledView.backgroundColor = UIColor.redColor()
            tiledView.center = curCenter
            tiledView.image = tileImageStack[pieceNumber] as? UIImage
            tiledView.userInteractionEnabled = true
            tiledViewsStack.append(tiledView)
            self.view.addSubview(tiledView)
            xCen += (frameWidth/3)
            pieceNumber += 1
        }
        xCen = (frameWidth/3)/2
        yCen += (frameHeight/3)
    }

    tiledViewsStack[0].removeFromSuperview()
    tiledViewsStack.removeAtIndex(0)
    //Now there are 8 imageViews in the tiledViewsStack array, and 9 centers stored in allCenters array.        

    var centersCopy = allCenters
    var randLocInt = Int()
    var randLoc = CGPoint()

    for any in tiledViewsStack
    {
        randLocInt = Int(arc4random() % UInt32(centersCopy.count)) // 0, --- 8
        randLoc = centersCopy[randLocInt].CGPointValue()
        any.center = randLoc
        centersCopy.removeAtIndex(randLocInt)
    }       
    emptySpot = centersCopy[0].CGPointValue()
}

var tapCen = CGPoint();

var left = CGPoint();
var right = CGPoint();
var top = CGPoint();
var bottom = CGPoint();

var leftIsEmpty = false
var rightIsEmpty = false
var topIsEmpty = false
var bottomIsEmpty = false


override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?)
{
    let frameWidth = self.view.frame.width
    let frameHeight = self.view.frame.height

    let allTouches = event?.allTouches()
    let myTouch = allTouches?.first

    if myTouch!.view != self.view
    {
        tapCen = myTouch!.view!.center

        left = CGPointMake(tapCen.x - (frameWidth/3), tapCen.y)
        right = CGPointMake(tapCen.x + (frameWidth/3), tapCen.y)
        top = CGPointMake(tapCen.x, tapCen.y - (frameHeight/3))
        bottom = CGPointMake(tapCen.x, tapCen.y + (frameHeight/3))

        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.animateWithDuration(0.5, animations: myTouch!.view!.center = emptySpot, completion: true)

            UIView.animateWithDuration(0.5, delay: 0, options: UIViewAnimationOptions.TransitionNone, animations: { () -> Void in

                myTouch!.view!.center = emptySpot

                }, completion: { (finished: Bool) -> Void in
                })

            emptySpot = tapCen
            leftIsEmpty = false; rightIsEmpty = false; topIsEmpty = false; bottomIsEmpty = false;
        }
    }   
}

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
} }

1 个答案:

答案 0 :(得分:1)

如果使用自动布局,则移动视图的中心或框架通常无法按预期工作,因为约束会覆盖视图的位置。

(它会起作用,直到某些因素导致视图的布局更新。此时约束会覆盖您所做的更改。)

使用自动布局,您需要为约束添加插座,然后更改这些约束的常量值以移动视图。

至于你的代码,缩小为&gt; 4个空格标志着一段文本作为代码。你的前几行没有足够的缩进。 (我修好了。)

顺便说一句。没有理由在代码上使用块引号。选择代码,单击编辑器上的花括号代码按钮,然后将代码缩进所需的4个空格。

(我懒得在您的代码行中删除所有不需要的“&gt;”块引号前缀。您应该这样做。)