第二天:Swift手势操控弹性按钮
参考链接:https://www.jianshu.com/p/f080ede0f3a8
1 import UIKit 2 3 fileprivate let buttonH: CGFloat = 200 4 5 class ViewController: UIViewController, UIGestureRecognizerDelegate { 6 7 @IBOutlet weak var segmentControl: UISegmentedControl! 8 var randomBtn: UIButton! 9 override func viewDidLoad() { 10 super.viewDidLoad() 11 12 // Do any additional setup after loading the view. 13 14 setupUI() 15 } 16 17 18 @IBAction func segmentedControlAction(_ sender: UISegmentedControl) { 19 20 switch sender.selectedSegmentIndex { 21 case 0: 22 randomBtn.backgroundColor = UIColor.red 23 case 1: 24 randomBtn.backgroundColor = UIColor.green 25 case 2: 26 randomBtn.backgroundColor = UIColor.blue 27 case 3: 28 randomBtn.backgroundColor = UIColor.randomColor() 29 default: 30 randomBtn.backgroundColor = UIColor.yellow 31 } 32 } 33 34 override func didReceiveMemoryWarning() { 35 super.didReceiveMemoryWarning() 36 // Dispose of any resources that can be recreated. 37 } 38 39 40 /* 41 // MARK: - Navigation 42 43 // In a storyboard-based application, you will often want to do a little preparation before navigation 44 override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 45 // Get the new view controller using segue.destinationViewController. 46 // Pass the selected object to the new view controller. 47 } 48 */ 49 50 } 51 52 extension ViewController { 53 func setupUI() { 54 // 添加背景色 55 let backgroundLayer = CAGradientLayer() 56 backgroundLayer.colors = [UIColor.yellow.cgColor, UIColor.white.cgColor] 57 backgroundLayer.startPoint = CGPoint(x: 0.5, y: 0) 58 backgroundLayer.endPoint = CGPoint(x: 0.5, y: 1) 59 backgroundLayer.frame = self.view.bounds 60 self.view.layer.addSublayer(backgroundLayer) 61 62 self.view.bringSubview(toFront: segmentControl) 63 64 // 添加 button 65 self.randomBtn = UIButton() 66 randomBtn.frame.size = CGSize(width: buttonH, height: buttonH) 67 randomBtn.backgroundColor = UIColor.red 68 randomBtn.center = self.view.center 69 randomBtn.setTitle("MineCode", for: .normal) 70 randomBtn.layer.cornerRadius = buttonH / 2 71 randomBtn.titleLabel?.font = UIFont.systemFont(ofSize: 24) 72 randomBtn.titleLabel?.textColor = UIColor.white 73 self.view.addSubview(randomBtn) 74 75 addGesture() 76 } 77 78 func addGesture() { 79 80 let singleTapGes = UITapGestureRecognizer(target: self, action:#selector(signleTapAction(_ :))) 81 singleTapGes.numberOfTapsRequired = 1 82 self.randomBtn.addGestureRecognizer(singleTapGes) 83 84 let longPressGes = UILongPressGestureRecognizer(target: self, action:#selector(longPressAction(_ :))) 85 longPressGes.allowableMovement = 10 86 longPressGes.minimumPressDuration = 1 87 self.randomBtn.addGestureRecognizer(longPressGes) 88 89 let panGes = UIPanGestureRecognizer(target: self, action:#selector(panGesAction(_ :))) 90 panGes.minimumNumberOfTouches = 1 91 panGes.maximumNumberOfTouches = 1 92 self.randomBtn.addGestureRecognizer(panGes) 93 94 let pinchGes = UIPinchGestureRecognizer(target: self, action:#selector(pinchAction(_ :))) 95 pinchGes.delegate = self 96 self.randomBtn.addGestureRecognizer(pinchGes) 97 98 let rotationGes = UIRotationGestureRecognizer(target: self, action:#selector(rotationAction(_ :))) 99 rotationGes.delegate = self 100 self.randomBtn.addGestureRecognizer(rotationGes) 101 } 102 } 103 104 extension ViewController { 105 @objc func signleTapAction(_ tap: UITapGestureRecognizer) { 106 107 print("Tap click...") 108 109 let animation = CABasicAnimation(keyPath: "transform.rotation.z") 110 111 animation.duration = 0.08 112 animation.repeatCount = 4 113 animation.fromValue = (-M_1_PI) 114 animation.toValue = (M_1_PI) 115 animation.autoreverses = true 116 self.randomBtn.layer.anchorPoint = CGPoint(x: 0.5, y: 0.5) 117 self.randomBtn.layer.add(animation, forKey: "rotation.z") 118 } 119 120 @objc func longPressAction(_ longGes: UILongPressGestureRecognizer) { 121 122 print("Long Start...") 123 124 let animation = CAKeyframeAnimation(keyPath: "transform.translation.x") 125 126 animation.duration = 0.08 127 animation.repeatCount = 2 128 129 animation.values = [0, -self.randomBtn.frame.width / 4, self.randomBtn.frame.width / 4, 0] 130 animation.autoreverses = true 131 self.randomBtn.layer.anchorPoint = CGPoint(x: 0.5, y: 0.5) 132 self.randomBtn.layer.add(animation, forKey: "rotation.x") 133 } 134 135 @objc func panGesAction(_ panGes: UIPanGestureRecognizer) { 136 137 print("Pan Start") 138 139 let movePoint = panGes.translation(in: self.view) 140 var curPoint = self.randomBtn.center 141 curPoint.x += movePoint.x 142 curPoint.y += movePoint.y 143 self.randomBtn.center = curPoint 144 145 panGes.setTranslation(CGPoint.zero, in: self.view) 146 } 147 148 @objc func pinchAction(_ pinch: UIPinchGestureRecognizer) { 149 150 print("PinchAction start") 151 152 let pinchScale = pinch.scale 153 self.randomBtn.transform = self.randomBtn.transform.scaledBy(x: pinchScale, y: pinchScale) 154 155 pinch.scale = 1.0 156 } 157 158 @objc func rotationAction(_ rotation: UIRotationGestureRecognizer) { 159 160 print(("rotation Start")) 161 162 let rotationR = rotation.rotation 163 self.randomBtn.transform = self.randomBtn.transform.rotated(by: rotationR) 164 165 rotation.rotation = 0.0 166 } 167 168 func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool { 169 return true 170 } 171 } 172 173 extension UIColor { 174 convenience init(r: CGFloat, g: CGFloat, b: CGFloat) { 175 self.init(red: r / 255.0, green: g / 255.0, blue: b / 255.0, alpha: 1.0) 176 } 177 178 class func randomColor() -> UIColor { 179 return UIColor(r: CGFloat(arc4random_uniform(256)), g: CGFloat(arc4random_uniform(256)), b: CGFloat(arc4random_uniform(256))) 180 } 181 }
怎么样成为程序员,学习和实践,日积月累...