Swift 4 - 如何在循环

时间:2018-05-10 14:23:58

标签: ios swift

我创建了这个progressView:

progress = UIProgressView(progressViewStyle: .default)
progress.center = view.center
progress.setProgress(0.5, animated: true)
view.addSubview(progress)

在我的viewDidLoad方法中,我调用了getLandGradingImages,在里面我做了一个循环,在里面我为getLandGradingImages中的每个结果调用了getLandGradingImage,这是更新我在整个过程中创建的progressView的最佳方法:

getLandGradingImages(jobNo: jobNo) { result in
    //Define axis variables
    var x = 25
    var y = 80

    //Define image counter variable
    var counterImages = 0
    var actualImageCounter = 0

    //For each Lot Image
    for item in result
    {
        self.getLandGradingImage(image: item["imageBytes"] as! String) { data in
            //Create Image from Data
            let image = UIImage(data: data)

            //Add Image to Image View
            let imageView = UIImageView(image: image!)

            //Set Image View Frame
            imageView.frame = CGRect(x: x, y: y, width: 100, height: 100)

            //Define UITapGestureRecognizer
            let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(self.imageTapped(tapGestureRecognizer:)))

            //Enable user interaction
            imageView.isUserInteractionEnabled = true

            //Assign tap gesture to image view
            imageView.addGestureRecognizer(tapGestureRecognizer)

            //Add Image to view
            self.view.addSubview(imageView)

            //Increase x axis
            x = x + 115

            //Increase image counter
            counterImages = counterImages + 1

            actualImageCounter = actualImageCounter + 1

            //If image counter is equal to 3, create new line.
            if(counterImages == 3)
            {
                //Increase y axis
                y = y + 115

                //Reset x axis
                x = 25

                //Reset image counter
                counterImages = 0
            }

            if(actualImageCounter == result.count)
            {
                //self.stopIndicator()
            }
        }
    }
}

以下是我在此过程中调用的两种方法:

func getLandGradingImages(jobNo:String, completionHandler:@escaping (_ result:Array<Dictionary<String, Any>>) -> Void) {
    //Define array for returning data
    var returnedResults = Array<Dictionary<String, Any>>()

    //Call API
    WebService().GetLandGradingImages(jobNo: jobNo)
    {
        (result: Array<Dictionary<String, Any>>) in

        DispatchQueue.main.async {
            //Return our results
            returnedResults = result
            completionHandler(returnedResults)
        }
    }
}

func getLandGradingImage(image:String, completionHandler:@escaping (_ result:Data) -> Void) {
    //Define array for returning data
    var returnedResults = Data()

    //Call API
    WebService().GetLandGradingImage(image: image)
    {
        (result: Data) in

        DispatchQueue.main.async {
            //Return our results
            returnedResults = result
            completionHandler(returnedResults)
        }
    }
}

对于这些电话,我使用的是Alamofire:

func GetLandGradingImage(image: String, completion: @escaping (_ result: Data) -> Void)
{
    let imagePath : String = image
    let url = URL(string: webserviceImages + imagePath)!

    Alamofire.request(url).authenticate(user: self.appDelegate.username!, password: self.appDelegate.password!).responseData { response in
        let noData = Data()

        if(response.error == nil)
        {
            if let data = response.data {
                completion(data)
            }
        }
        else
        {
            completion(noData)
        }
    }
}

我试过这个:

var counter:Int = 0 {
        didSet {
            let fractionalProgress = Float(counter) / 100.0
            let animated = counter != 0

            progress.setProgress(fractionalProgress, animated: animated)
        }
    }

并在for循环中:

DispatchQueue.global(qos: .background).async {
                    sleep(1)

                    DispatchQueue.main.async {
                        self.counter = self.counter + 1
                    }
                }

仍然没有,我的进度视图出现但没有更新。

0 个答案:

没有答案