Code Monkey home page Code Monkey logo

Comments (7)

yonat avatar yonat commented on September 25, 2024

Hi @michaeldebo , please link to a minimal project that reproduces the problem. Itโ€™s very hard to fix a problem I canโ€™t reproduce...

from multislider.

michaeldebo avatar michaeldebo commented on September 25, 2024

I don't know how to produce a link to it, but here is all the relevant code for the slider:

private func configureAgeSlider() {
    let age = ProfileDataManager.shared.age
    var lowValue: CGFloat = 18
    if age - 8 > 18 {
        lowValue = CGFloat(age - 8)
    }
    ProfileDataManager.shared.minAge = Int(lowValue)
    var highValue: CGFloat = 90
    if age + 8 < 90 {
        highValue = CGFloat(age + 8)
    }
    ageSlider.minimumValue = 18
    ageSlider.maximumValue = 90
    ageSlider.value = [lowValue, highValue]
    ageSlider.orientation = .horizontal
    ageSlider.tintColor = UIColor.convertHexColor(hex: "efefef")
    ageSlider.thumbImage = UIImage(named: "slider")!
    ageSlider.trackWidth = 3
    ageSlider.showsThumbImageShadow = false
    ProfileDataManager.shared.maxAge = Int(highValue)
    ageSlider.translatesAutoresizingMaskIntoConstraints = false
    view.addSubview(ageSlider)
    let verticalOffset = getSize(large: 80, medium: 60, small: 45)
    ageSlider.topAnchor.constraint(equalTo: distanceSlider.bottomAnchor, constant: verticalOffset).isActive = true
    ageSlider.leftAnchor.constraint(equalTo: letsNarrowItDownLabel.leftAnchor).isActive = true
    ageSlider.rightAnchor.constraint(equalTo: letsNarrowItDownLabel.rightAnchor).isActive = true
    ageSlider.heightAnchor.constraint(equalToConstant: 30).isActive = true
    ageSlider.addTarget(self, action: #selector(ageSliderChanged(_:)), for: .valueChanged)
    addAgeLabel()
    addAgeTitleLabel()
    addMinimumAgeLabel()
    addMaximumAgeLabel()
}

@objc func ageSliderChanged(_ ageSlider: MultiSlider) {
    print("thumb \(ageSlider.draggedThumbIndex) moved")
    print("now thumbs are at \(ageSlider.value)")
    ageSlider.keepsDistanceBetweenThumbs = true
    ageSlider.distanceBetweenThumbs = 4
    ageLabel.text = "\(Int(ageSlider.value[0])) - \(Int(ageSlider.value[1]))"
    ProfileDataManager.shared.minAge = Int(ageSlider.value[0])
    ProfileDataManager.shared.maxAge = Int(ageSlider.value[1])
    print("Range slider value changed: \(ageSlider.value)")
}

from multislider.

yonat avatar yonat commented on September 25, 2024

Please provide code I can execute to reproduce the problem, without calling functions that I don't have the code to. See https://stackoverflow.com/help/minimal-reproducible-example for instructions on how to create such code.

I really want to help, but I can't fix what I can't reproduce.

from multislider.

yonat avatar yonat commented on September 25, 2024

Okay, had some time so I did that for you. I don't get a crash... Here is the code I added to the Example project found in this repo, based on your code:

    private func configureAgeSlider() {
        let ageSlider = MultiSlider()
        ageSlider.minimumValue = 18
        ageSlider.maximumValue = 30

        let age: CGFloat = 50
        let lowValue = max(ageSlider.minimumValue, age - 8)
        let highValue = min(ageSlider.maximumValue, age + 8)
        ageSlider.value = [lowValue, highValue]

        ageSlider.orientation = .horizontal
        ageSlider.tintColor = .red
        ageSlider.trackWidth = 3
        ageSlider.showsThumbImageShadow = false
        ageSlider.addTarget(self, action: #selector(ageSliderChanged(_:)), for: .valueChanged)
        view.addConstrainedSubview(ageSlider, constrain: .leftMargin, .rightMargin)
        view.constrain(ageSlider, at: .top, to: multiSlider, at: .bottom, diff: 16)

        // *** test problem here:
        ageSlider.valueLabelPosition = .top
        ageSlider.distanceBetweenThumbs = 4
    }

    @objc func ageSliderChanged(_ ageSlider: MultiSlider) {
        // *** no need to change slider properties here
        print("thumb \(ageSlider.draggedThumbIndex) moved")
        print("now thumbs are at \(ageSlider.value)")
        print("Range slider value changed: \(ageSlider.value)")
    }

(Plus calling configureAgeSlider() from the end of viewDidLoad())

This works for me, please try it and see if you get an error.

from multislider.

michaeldebo avatar michaeldebo commented on September 25, 2024

Ahh, so I can't use autolayout, no "translatesAutoresizingIntoConstraints = false" and it should work. Got it. Thanks for your help!

from multislider.

yonat avatar yonat commented on September 25, 2024

Auto layout works fine: view.addConstrainedSubview() sets translatesAutoresizingIntoConstraints to false internally.

from multislider.

michaeldebo avatar michaeldebo commented on September 25, 2024

I needed to specify ageSlider.valueLabelPosition and it works. That's what my code was missing. Thx

from multislider.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.