[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 }

猜你喜欢

转载自www.cnblogs.com/strengthen/p/10207918.html
今日推荐