这是用于在捏合时更改背景颜色的代码。
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var mylabel: UILabel!
let background = UIColor(red: 1.0, green: 0.98, blue: 0.96, alpha: 1.0)
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = background
let pinchGesture = UIPinchGestureRecognizer(target: self, action:#selector(pinchAction(sender1:)))
self.view.addGestureRecognizer(pinchGesture)
}
func pinchAction(sender1 sender:UIPinchGestureRecognizer){
if sender.state == .began{
mylabel.text = "Pinch Began"
view.backgroundColor = UIColor.yellow
}
if sender.state == .changed{
mylabel.text = String(format:"Pinch scale: %1.3f",sender.scale)
}
if sender.state == .ended{
mylabel.text = "Pinch Ended"
view.backgroundColor = background
}
}
}
我还在故事板中标记了“启用了用户交互”,但仍然没有发生任何事情。