从superview中删除存储在数组中的UIView

时间:2016-03-30 21:51:53

标签: objective-c swift uiview uiimageview swift2

我知道我需要的代码是[[Array objectAtIndex:index] removeFromSuperview];,但这在Swift 2中不起作用。

我正在尝试从superview中删除存储在数组中的UIView。我知道我想删除索引0处的对象。想要在for循环结束时删除它。 什么代码可以在Swift中运行?

导入UIKit 进口基金会

类PhotoViewController:UIViewController,UIImagePickerControllerDelegate,UINavigationControllerDelegate {

var tileStack = [AnyObject]()
let num = 0


//Beginning of simple image selection and display

@IBOutlet weak var displayImageView: UIImageView!
@IBOutlet weak var tiledView: 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)
}



//Beginning of function to cut photo into 9 tiles, then display them randomly.

//cut selected image into 9 pieces and add each cropped image to tileStack array

@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!)
                tileStack.append(aUItile)
            }

        }
    }


    //display tiles in order on screen, remove 1 tile (top left, index 0), (eventually mix them up)
    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)
        {
            var tiledView : UIImageView
            tiledView = UIImageView(frame:CGRectMake(0, 0, frameWidth/3, (frameHeight)/3))
            //tiledView.backgroundColor = UIColor.redColor()
            tiledView.center = CGPointMake(xCen, yCen)
            tiledView.image = tileStack[pieceNumber] as? UIImage
            tiledView.userInteractionEnabled = true
            self.view.addSubview(tiledView)
            xCen += (frameWidth/3)
            pieceNumber += 1
        }
        xCen = (frameWidth/3)/2
        yCen += (frameHeight/3)
    }
    tileStack[0].removeFromSuperview()
    tileStack.removeAtIndex(0)
}

enter image description here

1 个答案:

答案 0 :(得分:0)

在Swift 2.0中,您需要从要删除的对象中调用removeFromSubview。在您的情况下,您需要以下内容:

dispatch_async(dispatch_get_main_queue(), {     if self.view.subviews.count&gt; 0     {       self.view.subviews [0] .removeFromSuperview()     } })