Developers Write Swift Code to Weigh Plums Using 3D Touch on iPhone 6s


The developers at FlexMonkey have written code in Swift to weigh plums using 3D Touch on an iPhone 6s.

Their latest app, called Plum-O-Meter, allows you to place two plums on the iPhone’s screen and will highlight the heavier of the two in yellow. The code behind the app is actually quite simple.

When the plum is placed on the screen, the app adds a new CircleWithLabel (which draws a circle with a label) to its view’s layer for each touch. This layer is then added to a dictionary with the touch as the key. The amount of force produced by the plum is used to control the new layer’s radius, which is displayed in the label.

    var circles = [UITouch: CircleWithLabel]()


    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?)
        label.hidden = true
        for touch in touches
            let circle = CircleWithLabel()
                force: touch.force / touch.maximumPossibleForce)
            circles[touch] = circle


If the plums move at any point, the touchesMoved function is used to readjust the position of the CircleWithLabel layer. The other key function in the code is the one which highlights the heaviest plum by figuring out which object is producing the greatest force on the display.

    func highlightHeaviest()
        func getMaxTouch() -> UITouch?
            return circles.sort({
                (a: (UITouch, CircleWithLabel), b: (UITouch, CircleWithLabel)) -> Bool in
                return a.0.force > b.0.force
            $0.1.isMax = $0.0 == getMaxTouch()




The iPhone 6s is not going to replace a high precision electronic scale anytime soon, however it is interesting to see what can be done with 3D Touch. The developers at FlexMonkey originally intended the app to work for grapes, however, they are too light to activate the 3D Touch sensors.

The source code for the Plum-O-Meter is available on GitHub if you are interested in giving it a try.