0

Yellow part is the button:

enter image description here

Inside PanGesture:

       case .changed:
            tempAngle = atan2(touchPoint.y - nailPoint.y, touchPoint.x - nailPoint.x)
            changedAngle = tempAngle
            let distance = distanceFromTwoPoints(nailPoint, touchPoint)
            drawTempUnitButton(buttonPoint: nailPoint, unitButtonWidth: distance, anchorPoint: currentAnglePoint, angle: tempAngle, button: tempUnitButton, unitValue: distance)

How I create button, both width and height are fixed

   func drawTempUnitButton(buttonPoint:CGPoint, unitButtonWidth:CGFloat, anchorPoint:CGPoint, angle:CGFloat, button:UIButton, unitValue:CGFloat){
        self.addSubview(button)
        button.frame =  CGRect(x: buttonPoint.x, y: buttonPoint.y, width: unitButtonWidth, height: 30)
        let floatUnitValue = Float(unitValue)
        let buttonTxt = String(floatUnitValue)
        button.setTitle(buttonTxt, for: .normal)
        button.titleLabel?.textAlignment = .center
        button.setTitleColor(.red, for: .normal)
        button.titleLabel?.font = .boldSystemFont(ofSize: 16)
        button.backgroundColor = UIColor.yellow
        button.setAnchorPoint(anchorPoint)
        let transform = CGAffineTransform(rotationAngle: angle)
        button.transform = transform
    }

What is the problem with it?

bao le
  • 927
  • 2
  • 7
  • 12
  • @LeoDabus sr but how am I supposed to use it? is it ` button.bouns = CGRect(x: buttonPoint.x, y: buttonPoint.y, width: unitButtonWidth, height: 30) `?, which I tried but it doesn't even display the button – bao le Oct 27 '20 at 03:25
  • https://stackoverflow.com/questions/1210047/cocoa-whats-the-difference-between-the-frame-and-the-bounds/28917673#28917673 – Leo Dabus Oct 27 '20 at 03:35

1 Answers1

0

Base on Leo Dabus comment, I fixed with these

        button.frame = CGRect(x: buttonPoint.x, y: buttonPoint.y, width: 0, height: 0)
        button.bounds.size.width = unitButtonWidth
        button.bounds.size.height = 30
bao le
  • 927
  • 2
  • 7
  • 12