gpt4 book ai didi

objective-c - 从 super View 中删除存储在数组中的 UIView

转载 作者:行者123 更新时间:2023-11-30 13:33:36 26 4
gpt4 key购买 nike

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

我正在尝试从 super View 中删除存储在数组中的 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

最佳答案

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

dispatch_async(dispatch_get_main_queue(),{ 如果 self.view.subviews.count > 0 { self.view.subviews[0].removeFromSuperview() }})

关于objective-c - 从 super View 中删除存储在数组中的 UIView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36320407/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com