将UISlider值从一个ViewController传递到另一个

时间:2018-09-02 13:36:04

标签: ios swift xcode uislider

我正在使用AVSpeechSynthesizer,正在尝试将滑块上的音量,音高和速率设置值从设置视图控制器传递到主视图控制器。 在设置View Controller中,从默认值开始,

public static (List<MaskedMat>, List<VectorOfPoint>) Segment(Mat mat, Mat diff, int threshold, int areaThreshold, double buffer)
{
    //Greyscale
    VectorOfVectorOfPoint contours = FindBinaryContours(diff, threshold);

    //Get the bigger contours
    List<VectorOfPoint> largeContours = new List<VectorOfPoint>();
    for (int i = 0; i < contours.Size; i++)
    {
        double area = CvInvoke.ContourArea(contours[i]);
        if (area > areaThreshold) largeContours.Add(contours[i]);
    }

    //Crop the images
    List<MaskedMat> crops = new List<MaskedMat>();
    foreach (VectorOfPoint contour in largeContours)
    {
        //Centroid
        MCvMoments moments = CvInvoke.Moments(contour);
        //Centroid(moments);
        var centreX = (int) Math.Round(moments.M10 / moments.M00);
        var centreY = (int) Math.Round(moments.M01 / moments.M00);
        Point centroid = new Point(centreX, centreY);

        //Get max radius
        double maxRadius = 0;
        for (int i = 0; i < contour.Size; i++)
        {
            double radius = Math.Sqrt(Math.Pow(contour[i].X - centreX, 2) + Math.Pow(contour[i].Y - centreY, 2));
            if (radius > maxRadius)
            {
                maxRadius = radius;
            }
        }

        //Crop area
        int halfWidth = (int) Math.Round(maxRadius * buffer);
        SquareWithinBounds sq = new SquareWithinBounds(centroid, halfWidth, mat.Size);

        //Crop
        var cropArea = sq.Rectangle;
        Mat cropTemp = new Mat(mat, cropArea);
        Mat crop = new Mat();
        cropTemp.CopyTo(crop);
        cropTemp.Dispose();

        //Raw mask
        Mat mask = new Mat(mat.Rows, mat.Cols, DepthType.Cv8U, 1);
        mask.SetTo(new MCvScalar(0));
        VectorOfVectorOfPoint contourArray = new VectorOfVectorOfPoint(contour.Clone());
        CvInvoke.FillPoly(mask, contourArray, new MCvScalar(255));
        Mat maskTemp = new Mat(mask, cropArea);
        mask = new Mat();
        maskTemp.CopyTo(mask);
        maskTemp.Dispose();

        //Shift contour
        var shiftedContour = contour.Translate((Size)Point.Subtract(sq.Centroid, (Size)centroid));
        crops.Add(new MaskedMat(crop, mask, shiftedContour, sq.Centroid));
    }                  
    return (crops, largeContours);
}

和滑块操作

var volume : Float = 1.0
var pitch: Float = 1.0
var rate: Float = AVSpeechUtteranceDefaultSpeechRate

在主视图控制器中,

@IBAction func volumeSliderDidSlide(_ sender: UISlider) {
    volume = sender.value
    testLable.text = String(volume)
}

@IBAction func pitchSliderDidSlide(_ sender: UISlider) {
    pitch = sender.value
}

@IBAction func rateSliderDidSlide(_ sender: UISlider) {
    rate = sender.value
}

但是仅使用音量,音高和速率的默认值,而不使用通过更改在滑块上设置的默认值。

1 个答案:

答案 0 :(得分:0)

我编写了一些与您的代码具有相同功能的代码。

在SettingViewController中

class SettingViewController: UIViewController {

    var volume: Float = 0.0

    @IBAction func slider(_ sender: UISlider) {
        volume = sender.value
    }

    @IBAction func closeButton(_ sender: Any) {
        dismiss(animated: true)
    }
}

和ViewController.swift

class ViewController: UIViewController {

    var settingVC: SettingViewController!

    override func viewDidLoad() {
        super.viewDidLoad()
        settingVC = UIStoryboard(name: "Main", bundle: nil)
            .instantiateViewController(withIdentifier: "SettingViewController") as! SettingViewController
    }

    @IBAction func printSettingValue(_ sender: Any) {
        // Use setting view controller's value
        print(settingVC.volume)
    }

    @IBAction func showSettingView(_ sender: Any) {
        show(settingVC, sender: nil)
    }
}

所以,这是我的代码,效果很好。

有我的假设。您使用的TTSSettingsViewController的值可能与您更改的值不同。