博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
iOS手势操作(Swift)
阅读量:7204 次
发布时间:2019-06-29

本文共 2586 字,大约阅读时间需要 8 分钟。

hot3.png

1 继承 UITabBarController (这个例子是这样)

2 Setup gestures (L and R)

override func viewDidLoad() {	super.viewDidLoad()	/* setup switch gestures */	self.swipeLeftGesture = UISwipeGestureRecognizer(target: self,		action: "onSwipeGesture:")	self.swipeLeftGesture!.direction		= UISwipeGestureRecognizerDirection.Left	self.view.addGestureRecognizer(self.swipeLeftGesture!)	self.swipeRightGesture = UISwipeGestureRecognizer(target: self,		action: "onSwipeGesture:")	self.swipeRightGesture!.direction		= UISwipeGestureRecognizerDirection.Right	self.view.addGestureRecognizer(self.swipeRightGesture!)}

3 实现gestures callback

/* gestures callback */@IBAction private func onSwipeGesture (sender: UISwipeGestureRecognizer) {	print(__LINE__, __FUNCTION__)	if (nil == self.viewControllers) {		print(__LINE__, "nil viewControllers")		return	}	let dire = sender.direction	var l = false	var r = false	switch (dire) {	case UISwipeGestureRecognizerDirection.Left:		/* right-to-left => left: need to show the right one */		print(__LINE__, "UISwipeGestureRecognizerDirection.Left")		l = true	case UISwipeGestureRecognizerDirection.Right:		print(__LINE__, "UISwipeGestureRecognizerDirection.Right")		r = true	default:		print(__LINE__, "UISwipeGestureRecognizerDirection.xx unhandled")	}	let si = self.selectedIndex	let c = self.viewControllers!.count	print(__LINE__, "si: \(si) c: \(c)")	var fromView: UIView?	var toShow: UIView?	if (l && (si < (c - 1))) {		/* right-to-left => left: need to show the right one */		fromView = self.selectedViewController!.view		toShow = self.viewControllers![si + 1].view	} else if (r && (si > 0)) {		fromView = self.selectedViewController!.view		toShow = self.viewControllers![si - 1].view	}	if (nil != toShow) {		if (l) {			/* right-to-left => left: need to show the right one */			UIView.transitionFromView(fromView!, toView: toShow!, duration: 0.5,				options: UIViewAnimationOptions.TransitionFlipFromRight,				completion: {					if ($0 && (nil != self.viewControllers)) {						let si = self.selectedIndex						let to = si + 1						self.selectedViewController = self.viewControllers![to]					}				})		} else {			UIView.transitionFromView(fromView!, toView: toShow!, duration: 0.5,				options: UIViewAnimationOptions.TransitionFlipFromLeft,				completion: {					if ($0 && (nil != self.viewControllers)) {						let si = self.selectedIndex						let to = si - 1						self.selectedViewController = self.viewControllers![to]					}				})		}	}}var swipeLeftGesture: UISwipeGestureRecognizer?var swipeRightGesture: UISwipeGestureRecognizer?

转载于:https://my.oschina.net/yuiwong/blog/513064

你可能感兴趣的文章
leetcode:Count Primes
查看>>
[转] babel的使用
查看>>
CentOS7.0安装与配置Tomcat-7
查看>>
C# SQL数据访问帮助类
查看>>
.net面试(汇总)
查看>>
.NET Entity Framework基本使用方法
查看>>
BZOJ3528: [Zjoi2014]星系调查
查看>>
Lua 随机数生成问题
查看>>
CLR的执行模型(4):执行程序集的代码
查看>>
同一脚本sh 脚本名 报Syntax error: "(" unexpected而./脚本名不报错,求解!!
查看>>
ZJOI2008皇帝的烦恼
查看>>
新手windows安装nginx
查看>>
浏览器兼容问题踩坑收集
查看>>
Python 实用技巧
查看>>
object c中@property 的使用
查看>>
Sping 核心IOC容器
查看>>
poj 2524
查看>>
MapReduce
查看>>
论文阅读笔记五十六:(ExtremeNet)Bottom-up Object Detection by Grouping Extreme and Center Points(CVPR2019)...
查看>>
回收期计算
查看>>