有没有办法优化布局事件?

时间:2018-01-28 04:22:54

标签: ios swift autolayout instruments

我使用时间配置文件来检查是什么原因导致我的应用在快速更改pageiwController's page时变得非常口吃和口吃,并且它检测到布局事件需要很多CPU和线程使用(请参阅看下面'pic'),它的成本最高为182%CPU和最高96%的线程资源,它必须是不合理的。它们是系统事件,我如何优化它们?

非常感谢你!

enter image description here

代码段1:存档

private func aulayoutViews() {
    view.addSubview(pageControl)
    view.addSubview(navigationBar)

    //MARK: navigationBar
    PageViewController.navigationBarHeight = 44

    navigationBar.translatesAutoresizingMaskIntoConstraints = false
    navigationBar.widthAnchor.constraint(equalToConstant: self.view.frame.width).isActive = true
    navigationBar.heightAnchor.constraint(equalToConstant: PageViewController.navigationBarHeight).isActive = true
    navigationBar.leadingAnchor.constraint(equalTo: self.view.leadingAnchor, constant: 0).isActive = true

    if #available(iOS 11.0, *) {
        navigationBar.topAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.topAnchor, constant: 0).isActive = true
    } else {
        navigationBar.topAnchor.constraint(equalTo: self.view.topAnchor, constant: 20).isActive = true
    }

    //MARK: pageControl
    PageViewController.pageControlHeight = 10

    pageControl.translatesAutoresizingMaskIntoConstraints = false
    pageControl.widthAnchor.constraint(equalToConstant: self.view.frame.width).isActive = true
    pageControl.heightAnchor.constraint(equalToConstant: PageViewController.pageControlHeight).isActive = true
    pageControl.leadingAnchor.constraint(equalTo: self.view.leadingAnchor, constant: 0).isActive = true
    pageControl.topAnchor.constraint(equalTo: self.navigationBar.bottomAnchor, constant: 0).isActive = true
}

代码段2:主要是堆栈视图impelmentaion

 private func layoutViews() {
    var updateTimeX: CGFloat = CGFloat()
    var updateTimeY: CGFloat = CGFloat()

    switch iPhoneType {
    case .iPhoneSE:
        updateTimeX = 0
        updateTimeY = 0
    case .iPhone:
        updateTimeX = 0
        updateTimeY = 8
    case .iPhonePlus:
        updateTimeX = 4
        updateTimeY = 8
    default: //case .iPhoneX:
        updateTimeX = 0
        updateTimeY = 8
    }

    //MARK: updateTime的autolayout
    self.view.addSubview(updateTime)
    updateTime.translatesAutoresizingMaskIntoConstraints = false
    updateTime.topAnchor.constraint(equalTo:  self.view.layoutMarginsGuide.topAnchor, constant: updateTimeY).isActive = true

    NSLayoutConstraint.activate(NSLayoutConstraint.constraints(
        withVisualFormat: "H:|-updateTimeX-[updateTime]",
        options: [],
        metrics: ["updateTimeX": updateTimeX],
        views: ["updateTime": updateTime]))

    self.view.addSubview(stackView)
    stackView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor).isActive = true

    NSLayoutConstraint.activate(NSLayoutConstraint.constraints(
        withVisualFormat: "H:|[stackView]-16-|",
        options: [],
        metrics: nil,
        views: ["stackView": stackView]))

    stackView.translatesAutoresizingMaskIntoConstraints = false
    stackView.addArrangedSubview(contentStackView)
    stackView.addArrangedSubview(buttonAQIStackView)
    stackView.axis = .vertical
    stackView.alignment = .leading
    stackView.distribution = .fill
    stackView.spacing = 8

    contentStackView.translatesAutoresizingMaskIntoConstraints = false
    contentStackView.addArrangedSubview(tempWeatherStackView)
    contentStackView.addArrangedSubview(sunWindHumidityStackView)
    contentStackView.axis = .vertical
    contentStackView.alignment = .leading
    contentStackView.distribution = .fill
    contentStackView.spacing = -16

    tempWeatherStackView.translatesAutoresizingMaskIntoConstraints = false
    tempWeatherStackView.addArrangedSubview(currentTemp)
    tempWeatherStackView.addArrangedSubview(currentWeather)
    tempWeatherStackView.axis = .horizontal
    tempWeatherStackView.alignment = .firstBaseline
    tempWeatherStackView.distribution = .fill
    tempWeatherStackView.spacing = -32

    sunWindHumidityStackView.translatesAutoresizingMaskIntoConstraints = false
    sunWindHumidityStackView.addArrangedSubview(sunTime)
    sunWindHumidityStackView.addArrangedSubview(wind)
    sunWindHumidityStackView.addArrangedSubview(humidity)
    sunWindHumidityStackView.axis = .horizontal
    sunWindHumidityStackView.alignment = .top
    sunWindHumidityStackView.distribution = .fill
    sunWindHumidityStackView.spacing = 8

    weatherButton.translatesAutoresizingMaskIntoConstraints = false
    weatherButton.heightAnchor.constraint(equalToConstant: 24).isActive = true

    AQIButton.translatesAutoresizingMaskIntoConstraints = false
    AQIButton.heightAnchor.constraint(equalToConstant: 24).isActive = true

    buttonAQIStackView.translatesAutoresizingMaskIntoConstraints = false
    buttonAQIStackView.addArrangedSubview(weatherButton)
    buttonAQIStackView.addArrangedSubview(AQIButton)
    buttonAQIStackView.axis = .horizontal
    buttonAQIStackView.alignment = .fill
    buttonAQIStackView.distribution = .fill
    buttonAQIStackView.spacing = 8
}

代码段3:堆栈视图&约束

private func layoutViews() {
    var iconImageSize: CGFloat = CGFloat()
    var stackWidth: CGFloat = CGFloat()
    var scrollHeight: CGFloat = CGFloat()
    var stackViewWidth: CGFloat = CGFloat()

    switch iPhoneType {
    case .iPhoneSE:
        iconImageSize = 29
        stackWidth = 79.5
        scrollHeight = 980
        stackViewWidth = stackWidthCalculateBy(stackWidth, totalStackQty: 16, sceneStackQty: 6, sceneSpacing: 16)
    case .iPhone:
        iconImageSize = 32
        stackWidth = 92
        scrollHeight = 109.76
        stackViewWidth = stackWidthCalculateBy(stackWidth, totalStackQty: 16, sceneStackQty: 6, sceneSpacing: 16)
    case .iPhonePlus:
        iconImageSize = 32
        stackWidth = 96
        scrollHeight = 122.24
        stackViewWidth = stackWidthCalculateBy(stackWidth, totalStackQty: 16, sceneStackQty: 6, sceneSpacing: 16)
    default: //case .iPhoneX:
        iconImageSize = 32
        stackWidth = 92
        scrollHeight = 109.76
        stackViewWidth = stackWidthCalculateBy(stackWidth, totalStackQty: 16, sceneStackQty: 6, sceneSpacing: 16)
    }

    //MARK: stackView
    scrollView.addSubview(stackView)
    stackView.translatesAutoresizingMaskIntoConstraints = false

    stackView.axis = .horizontal
    stackView.alignment = .fill
    stackView.distribution = .equalSpacing

    NSLayoutConstraint.activate(NSLayoutConstraint.constraints(
        withVisualFormat: "V:|[stackView(scrollHeight)]|",
        options: [],
        metrics: ["scrollHeight": scrollHeight],
        views: ["stackView": stackView]))

    NSLayoutConstraint.activate(NSLayoutConstraint.constraints(
        withVisualFormat: "H:|[stackView(stackViewWidth)]|",
        options: [],
        metrics: ["stackViewWidth": stackViewWidth],
        views: ["stackView": stackView]))

    for index in 0...ix {
        tempStackView[index].addArrangedSubview(highTempLabel[index])
        tempStackView[index].addArrangedSubview(lowTempLabel[index])

        tempStackView[index].translatesAutoresizingMaskIntoConstraints = false
        tempStackView[index].axis = .vertical
        tempStackView[index].alignment = .fill
        tempStackView[index].distribution = .fillEqually

        iconImage[index].translatesAutoresizingMaskIntoConstraints = false
        iconImage[index].widthAnchor.constraint(equalToConstant: iconImageSize).isActive = true
        iconImage[index].heightAnchor.constraint(equalToConstant: 62).isActive = true
        iconImage[index].contentMode = .scaleAspectFit

        iconTempStackView[index].addArrangedSubview(iconImage[index])
        iconTempStackView[index].addArrangedSubview(tempStackView[index])

        iconTempStackView[index].translatesAutoresizingMaskIntoConstraints = false
        iconTempStackView[index].axis = .horizontal
        iconTempStackView[index].alignment = .fill
        iconTempStackView[index].distribution = .fill

        weekLabel[index].setContentHuggingPriority(UILayoutPriority(rawValue: 249), for: .horizontal)
        weekDateStackView[index].addArrangedSubview(weekLabel[index])
        weekDateStackView[index].addArrangedSubview(dateLabel[index])

        weekDateStackView[index].translatesAutoresizingMaskIntoConstraints = false
        weekDateStackView[index].axis = .horizontal
        weekDateStackView[index].alignment = .fill
        weekDateStackView[index].distribution = .fill
        weekDateStackView[index].spacing = 8

        contentStackView[index].widthAnchor.constraint(equalToConstant: stackWidth).isActive = true
        contentStackView[index].addArrangedSubview(weekDateStackView[index])
        contentStackView[index].addArrangedSubview(iconTempStackView[index])
        contentStackView[index].addArrangedSubview(weatherLabel[index])

        contentStackView[index].translatesAutoresizingMaskIntoConstraints = false
        contentStackView[index].axis = .vertical
        contentStackView[index].alignment = .fill
        contentStackView[index].distribution = .equalSpacing

        stackView.addArrangedSubview(contentStackView[index])
    }

    //MARK: scrollView
    view.addSubview(scrollView)
    scrollView.translatesAutoresizingMaskIntoConstraints = false

    NSLayoutConstraint.activate(NSLayoutConstraint.constraints(
        withVisualFormat: "H:|[scrollView]|",
        options: [],
        metrics: nil,
        views: ["scrollView": scrollView]))

    NSLayoutConstraint.activate(NSLayoutConstraint.constraints(
        withVisualFormat: "V:[scrollView(scrollHeight)]|",
        options: [],
        metrics: ["scrollHeight": scrollHeight],
        views: ["scrollView": scrollView]))
}

0 个答案:

没有答案