微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

[Swift通天遁地]三、手势与图表-(1)监听屏幕上触摸事件的各种状态

本文将演示监听屏幕上触摸事件的各种状态。

在项目导航区,打开视图控制器的代码文件【ViewController.swift】

现在开始编写代码,监听屏幕上的触摸事件的各种状态。

 1 import UIKit
 2 
 3 class ViewController: UIViewController {
 4 
 5     override func viewDidLoad() {
 6         super.viewDidLoad()
 7         // Do any additional setup after loading the view,typically from a nib.
 8         
 9     }
10 
11     //添加一个方法,用来监听手指按下时的事件
12     override func touchesBegan(_ touches: Set<UITouch>,with event: UIEvent?) {
13         //当手指在屏幕上刚刚按下时,在控制台输出日志信息。
14         print("touchesBegan");
15     }
16     
17     //添加一个方法,用来监听手指移动时的事件
18     override func touchesMoved(_ touches: Set<UITouch>,with event: UIEvent?) {
19         //当手指在屏幕上刚刚按下并移动时,在控制台输出日志信息。
20         print("touchesMoved");
21     }
22     
23     //添加一个方法,用来监听手指移动结束,离开屏幕时的事件
24     override func touchesEnded(_ touches: Set<UITouch>,with event: UIEvent?) {
25         //当手指移动结束时,离开屏幕时,在控制台输出日志信息。
26         print("touchesEnded");
27     }
28     
29     //添加一个方法,用来监听手势被取消的事件。例如手指子啊移动时,突然有电话接入时的情况
30     override func touchesCancelled(_ touches: Set<UITouch>,with event: UIEvent?) {
31         //当手势被取消时,在控制台输出日志信息。
32         print("touchesCancelled");
33     }
34     
35     override func didReceiveMemoryWarning() {
36         super.didReceiveMemoryWarning()
37         // dispose of any resources that can be recreated.
38     }
39 }

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐