参考链接: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 }

转载于:https://www.cnblogs.com/chmhml/p/8294874.html

第二天:Swift手势操控弹性按钮相关推荐

  1. Apple Watch新玩法:手势操控无人机

    当Apple Watch遇上无人机,手势操控还能这么玩. 相信很多人都幻想过能够通过手势来自由操控一个物体.而现在这项技术竟然可以通过Apple Watch来实现.据路透社报道,台湾科技团队PVD+的 ...

  2. svg果冻弹性按钮动画js特效

    下载地址 svg果冻弹性按钮动画特效,漂亮的按钮切换的时候出现的创意选中样式带动画特效. dd:

  3. 推特用户飙升;酷狗基于华为鸿蒙系统开发新应用;苹果新专利 可用手势操控车辆 | 每日大事件...

    数据智能产业创新服务媒体 --聚焦数智 · 改变商业 01.推特下载量飙升.日活创新高 11月8日消息,虽然最近推特陷入混乱,但根据美国科技媒体The Verge获得的推特公司的一份文件显示,在马斯克 ...

  4. mix2 android 8.0,小米 MIX2 升级安卓 8.0,新增全局手势操控

    上周一,小米旗下的当家花旦 MIX2 开启了 Android 8.0 的内测活动,想必已经有不少满足条件的小米 MIX 2 用户获得了内测资格.最近,就有成功获得内测资格的 MIX 2 用户向我们展示 ...

  5. 播放器常用手势操控封装GestureView

    一.播放器常用手势操控包括: 1.单击显示和隐藏播放器控件: 2.双击播放暂停: 3.左侧上下滑动调节亮度: 4.右侧上下滑动调节音量: 5.左右滑动调节进度. 二.手势检测帮助类PlayerGest ...

  6. DIY穷人版谷歌眼镜,自定义手势操控,树莓派再一次被开发新玩法

    兴坤 发自 凹非寺 量子位 报道 | 公众号 QbitAI 通过帅气的手势,操控投影在眼前的电子成像,这不就是科幻片里的基础配置嘛. 现在,有人把它从科幻电影中带入了现实.动动手指,实现对眼前世界的掌 ...

  7. iOS Swift UISearchController的取消按钮

    UISearchController的取消按钮 关于UISearchController的设置就不多说了,可以参考<UISearchController仿微信搜索框>或者自行上网查找. 情 ...

  8. 第二章——Swift语言

    Swift是苹果于2014年推出的一种新语言.Swfit 将替换 Objective-C,成为 iOS 和 Mac 的推荐开发语言.在本章中,您将重点学习Swift的基础知识. 你不会学到所有的知识, ...

  9. 根据黑马pink老师讲的bootstrap而做的笔记(不过多赘述,简洁高效)第二章 第二节 bootstrap前端开发框架以按钮为例(引用网站上已经设置的布局和样式)

    咱们大部分围绕着两个问题来叙述 是什么? 怎么做? 一.目录(总体,这门课要学的目录)(现在是第二章,bootstrap前端开发框架) 1.响应式开发 2.bootstrap前端开发框架 3.boot ...

最新文章

  1. C#学习-EF在三层中使用
  2. Python fabric实现远程操作和部署
  3. ASP.NET MVC – 视图简介
  4. hadoop 2.x安装:不能加载本地库 - 解决libc.so.6 version GLIBC_2.14 not found问题
  5. dram和nand哪个难生产_草缸能不能用陶粒,看完和水草泥的对比,你就知道哪个更好了...
  6. nodejs安装到d盘怎么使用npm_NodeJS、NPM安装配置步骤
  7. LDA主题模型原文解读
  8. 实现ftoa与itoa
  9. GPU版的tensorflow在windows上的安装时的错误解决方案
  10. 针对SQL INJECTION的SQL SERVER安全设置初级篇
  11. 案例:规则引擎Drools解决汽水问题
  12. linux的一个find命令rm删除某目录下所有子目录 中的某类文件
  13. Bash递归函数计算斐波纳吉(fibonacci)数列
  14. Nebula3渲染层: Graphics
  15. android 编译 libjpeg-turbo,编译Android环境的libjpeg-turbo
  16. 哈夫曼编码问题(贪心算法)
  17. 解决问题:Something's wrong--perhaps a missing \item. \end{thebibliography}
  18. LineRenderer画虚线
  19. 计算机房精密空调术语,机房精密空调参数及含义
  20. goto的理解与使用

热门文章

  1. 深度学习pytorch--softmax回归(三)
  2. nginx配合python_人生苦短我用python[0x02] nginx与python结合
  3. matlab的7.3版本是什么_王者荣耀:玩不好元歌的3大原因,无论什么版本,元歌起码T1.5_电竞...
  4. 文章id 文章标题点击量php,WordPress如何通过文章ID获取文章标题等信息
  5. 天刀服务器维护时间,6月3日服务器例行维护公告(已完成)
  6. matlab 自定义对象,自定义类的对象显示
  7. r java_如何在R中使用JAVA写的程序包?
  8. 云服务器 生物信息学,云服务器 生物信息学
  9. 多节锂电串联保护板ic_如何有效保护锂电池板,一款优质的MOS管就能解决
  10. 带网管工业交换机跟不带网管交换机的差别