第二天: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 }

 

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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/354236.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

asp获取mysql数据报错_ASP.NET在删除掉数据库文件后报错处理

在开发asp.net mvc程序时,默认时我们会使用LocalDB, 我们有时会以为删除掉App_Data目录就可以自动新建数据库,但是我们在网站重新启动后(进入Account)就会发现报如下错误:The ASP.NET Simple Membership database could not be initialized. …

按插入顺序排序的map

LinkedHashMap HashMap是无序的,HashMap在put的时候是根据key的hashcode进行hash然后放入对应的地方。所以在按照一定顺序put进HashMap中,然后遍历出HashMap的顺序跟put的顺序不同。单纯的HashMap是无法实现排序的。 区别: 1.HashMap里面存入…

lua excel to mysql_在Lua程序中使用MySQL的教程

http://www.jb51.net/article/66972.htmhttps://www.2cto.com/database/201501/372767.html常用sql语句:mysql -h localhost -u 用戶名 -p密碼 //連接數據庫use desk_show; //使用數據庫show tables; …

java实践_Java怪异实践

java实践总览 Java中有许多实践使我感到困惑。 这里只是一些。 使用-Xmx和-Xms 选项-Xmx广泛用于设置最大内存大小。 如Java HotSpot VM Options中所述,以-X开头的选项是非标准的(不保证所有VM实施都支持该选项),并且在以后的JDK…

函数参数:

函数参数:一、位置参数:按顺序一一对应赋值 def stu(name,age,course,country) stu("刘老根",25,"linux","CN")二、关健参数:在实参中使用 号赋值(可以不按顺序) def stu(name,a…

mysql group by 重复_mysql – 使用GROUP BY删除重复项的查询

id_specific_price id_product-------------------------------1 22 23 24 35 36 37 3需要删除重复项,预期结果:id_specific_price id_product-------------------------------3 27 3SELECT *FROM ps_specific_priceWHERE id_specific_price NOT IN(SELECT MAX(id_s…

您的JAX-RS API并非天生就等于:使用动态功能

这次,我们将讨论一些有关JAX-RS 2.0 API的内容,并涉及该规范的一个非常有趣的方面: 动态功能以及它们的有用性。 传统上,当配置和部署JAX-RS 2.0 API(使用Application类,从servlet引导或通过RuntimeDelega…

ionic2 安装与cordova打包

1.安装: cnpm install -g cordova ionic ionic start name cd name cnpm install 2、环境配置: http://www.cnblogs.com/changyaoself/p/6544082.html 这里是具体配置。 测试环境: cordova platform list 如下才可以: 3、添加…

mysql vacuum_PostgreSQL DBA快速入门(四) - 体系架构

PostgreSQL在开源关系型数据库市场是最先进的数据库。他的第一个版本在1989年发布,从那时开始,他得到了很多扩展。根据db-enginers上的排名情况,PostgreSQL目前在数据库领域排名第四。 本篇博客,我们来讨论一下PostgreSQL的内部架…

(ab)使用Java 8 FunctionalInterfaces作为本地方法

如果您使用Scala或Ceylon甚至JavaScript等更高级的语言进行编程,则“嵌套函数”或“本地函数”是您非常常见的习惯用法。 例如,您将编写诸如fibonacci函数之类的东西: def f() {def g() "a string!"g() "– says g" …

mysql的配置实现远程访问_MySQL 远程连接配置的正确实现 | 学步园

此文章主要向大家描述的是MySQL远程连接配置的实际操作步骤,以及在其实际操作中值得我们大家注意的相关事项的描述, 以下就是具体方案的描述,希望在你今后的学习中会有所帮助。MySQL远程配置GRANT ALL PRIVILEGES ON *.* TO root% IDENTIFIED…

oracle 用户账户被锁处理

一、以管理员身份登录 SQL> conn sys/sys as sysdba; (分号是必须的但是我是以system登录的所在这不应该写conn sys/sys as sysdba应该写conn system/orcl as sysdba;)Connected. 二、解锁被锁用户SQL> alter user scott account unlock;User alte…

总结mysql的基础语法_mysql 基础sql语法总结 (二)DML

二、DML(增、删、改)1)插入数据第一种写法:INSERT INTO 表名 (列名1,列名2,,......)VALUES(列值1,列值2,......)第二种写法:INSERT INTO 表名 VALUES(列值1,列值2,......…

提高团队协作效率

提高团队协作效率 分工合理,责任明确 团队是由个人组成的,团队中的个人往往经历不同、背景不同、性格有差异、水平有高低。在团队形成后、正式开工前,首先应该进行合理分工,要结合每个 人的特点和爱好,充分发挥出每个人…

mysql远程访问时间长无反应_远程MySQL访问需要很长时间

Running a MySQL query on a local database takes only about 20-30ms but running the same query through a remote connection(internet) takes 500ms. Is this normal? If not what could be the possible reason for such delay?This is a wireshark time extract for …

c# 利用AForge和百度AI开发实时人脸识别

baiduAIFaceIdentify项目是C#语言,集成百度AI的SDK利用AForge开发的实时人脸识别的小demo,里边包含了人脸检测识别,人脸注册,人脸登录等功能 人脸实时检测识别功能 思路是利用AForge打开摄像头,通过摄像头获取到的图像…

Java中Array和ArrayList之间的9个区别

array和ArrayList都是Java中两个重要的数据结构,在Java程序中经常使用。 即使ArrayList在内部由数组支持,了解Java中的数组和ArrayList之间的差异对于成为一名优秀的Java开发人员也至关重要。 如果您知道相似点和不同点,则可以明智地决定何时…

python读二进制文件遍历_使用python反向读取二进制文件

从这个问题中我可以看出代码中有几点需要改进。首先,while循环在Python中很少使用,因为使用for循环或使用一些内置函数几乎总是有更好的方法来表达相同的内容。在我想代码纯粹是为了培训目的。否则,我会首先问真正的目标是什么(因为知道了问题…

vue 在已有的购买列表中(数据库返回的数据)修改商品数量

连续加班一个月 连续通宵三天 到最后还是少了一个功能 心碎 简介:一个生成好的商品列表(数据库返回的数据) 首先拿到我们需要渲染的数组 在data中定义 我是在测试的时候 直接写了两条数据 下面开始点击删除 点击添加是一样的代码 只不过加号…

python饼状图教程_Python数据可视化:饼状图的实例讲解

使用python实现论文里面的饼状图:原图:python代码实现:# # 饼状图# plot.figure(figsize(8,8))labels [uCanteen, uSupermarket, uDorm, uOthers]sizes [73, 21, 4, 2]colors [red, yellow, blue, green]explode (0.05, 0, 0, 0)patches,…