В прошлой статье мы разобрали работу с 3DTouch на iPhone. Теперь осталось завершить пару штрихов и закончить наше приложение.
Как я уже сказал, основной функционал готов, остались только доработки:
import UIKit
import AudioToolbox
class ViewController: UIViewController {
@IBOutlet weak var scaleView: ScaleView!
@IBOutlet weak var forceLabel: UILabel!
@IBOutlet weak var grammLabel: UILabel!
var isPlaySound = true
...
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
if let touch = touches.first {
cicrcleView.center = touch.location(in: view)
if #available(iOS 9.0, *) {
if traitCollection.forceTouchCapability == UIForceTouchCapability.available {
if touch.force >= touch.maximumPossibleForce {
forceLabel.text = "100%+ force"
grammLabel.text = "385 грамм"
if isPlaySound { // Добавления // 1
AudioServicesPlaySystemSound(1519)
isPlaySound = false // 2
}
} else {
let force = (touch.force / touch.maximumPossibleForce) * 100
let grams = force * 385 / 100
let roundGrams = Int(grams)
isPlaySound = true // Добавления // 3
forceLabel.text = "\(Int(force))% force"
grammLabel.text = "\(roundGrams) грамм"
}
}
}
}
}
var isUpdate = true {
didSet {
if isUpdate == false {
DispatchQueue.main.asyncAfter(deadline: .now() + 0.01) {
self.isUpdate = oldValue
}
}
}
}
if isUpdate {
forceLabel.text = "\(Int(force))% force"
grammLabel.text = "\(roundGrams) грамм"
isUpdate = false
}
let cicrcleView = UIView(frame: CGRect(x: 0, y: 0, width: 80, height: 80)) // View 80 на 80
override func viewDidLoad() {
super.viewDidLoad()
forceLabel.text = "0% force"
grammLabel.text = "0 грамм"
cicrcleView.layer.cornerRadius = 40 // Закруглили углы по половине ширины View - получлся круг
cicrcleView.alpha = 0.6 // Прозрачность 60%
cicrcleView.backgroundColor = UIColor.red
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
if let touch = touches.first {
cicrcleView.center = touch.location(in: view) // 1
view.addSubview(cicrcleView) // 2
}
}
cicrcleView.transform = CGAffineTransform.init(scaleX: CGFloat(1 + (grams / 5) / 20), y: CGFloat(1 + (grams / 5) / 20))
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
forceLabel.text = "0% force"
grammLabel.text = "0 грамм"
// Добавить
cicrcleView.removeFromSuperview() // Убрали круг
cicrcleView.transform = .identity // Убрали трансформацию для круга
}
import UIKit
import AudioToolbox
class ViewController: UIViewController {
@IBOutlet weak var scaleView: ScaleView!
@IBOutlet weak var forceLabel: UILabel!
@IBOutlet weak var grammLabel: UILabel!
var isPlaySound = true
var isUpdate = true {
didSet {
if isUpdate == false {
DispatchQueue.main.asyncAfter(deadline: .now() + 0.01) {
self.isUpdate = oldValue
}
}
}
}
let cicrcleView = UIView(frame: CGRect(x: 0, y: 0, width: 80, height: 80))
override func viewDidLoad() {
super.viewDidLoad()
forceLabel.text = "0% force"
grammLabel.text = "0 грамм"
cicrcleView.layer.cornerRadius = 40
cicrcleView.alpha = 0.6
cicrcleView.backgroundColor = UIColor.red
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
if let touch = touches.first {
cicrcleView.center = touch.location(in: view) // 1
view.addSubview(cicrcleView) // 2
}
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
if let touch = touches.first {
cicrcleView.center = touch.location(in: view)
if #available(iOS 9.0, *) {
if traitCollection.forceTouchCapability == UIForceTouchCapability.available {
if touch.force >= touch.maximumPossibleForce {
forceLabel.text = "100%+ force"
grammLabel.text = "385 грамм"
if isPlaySound {
AudioServicesPlaySystemSound(1519)
isPlaySound = false
}
} else {
let force = (touch.force / touch.maximumPossibleForce) * 100
let grams = force * 385 / 100
let roundGrams = Int(grams)
isPlaySound = true
if isUpdate {
forceLabel.text = "\(Int(force))% force"
grammLabel.text = "\(roundGrams) грамм"
isUpdate = false
}
cicrcleView.transform = CGAffineTransform.init(scaleX: CGFloat(1 + (grams / 5) / 20), y: CGFloat(1 + (grams / 5) / 20))
}
}
}
}
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
forceLabel.text = "0% force"
grammLabel.text = "0 грамм"
cicrcleView.removeFromSuperview()
cicrcleView.transform = .identity
}
}
import UIKit
@IBDesignable
class ScaleView: UIView {
override func draw(_ rect: CGRect) {
let context = UIGraphicsGetCurrentContext()
context?.setStrokeColor(UIColor.red.cgColor)
context?.setLineWidth(14.0)
context?.addArc(center: CGPoint(x: 375 / 2, y: 375 / 2), radius: 375 / 2 - 14, startAngle: 0, endAngle: 2 * CGFloat(M_PI), clockwise: true)
context?.strokePath()
context?.setLineWidth(1.0)
context?.setStrokeColor(UIColor.lightGray.cgColor)
context?.addArc(center: CGPoint(x: 375 / 2, y: 375 / 2), radius: 375 / 4 - 14, startAngle: 0, endAngle: 2 * CGFloat(M_PI), clockwise: true)
context?.strokePath()
}
}
К сожалению, не доступен сервер mySQL