[Swift UIPanGestureRecognizer]Pan手势识别 变成 slider(仿Prisma)

Pan手势识别的声明:

let panRecognizer =  UIPanGestureRecognizer(target: self, action: #selector(URVIEWController.respondsToPenGesture))

self.view.addGestureRecognizer(panRecognizer)

设置后面调用的方法:respondsToPenGesture

func respondsToPenGesture(sender: UIPanGestureRecognizer) {}

最重要的就是在方法中根据 手势的state来作出不同的响应:

if (sender.state == UIGestureRecognizerState.Began) {

startPanLocation = sender.locationInView(URVIEW)

} else if (sender.state == UIGestureRecognizerState.Changed) {

let stopLocation = sender.locationInView(URVIEW)

let abscissaChange = stopLocation.x - startPanLocation!.x

} else if (sender.state == UIGestureRecognizerState.Ended) {

let stopLocation = sender.locationInView(URVIEW)

let abscissaChange = stopLocation.x - startPanLocation!.x

}

需要注意的是 startPanLocation 你需要存在一个全局变量里,因为在你state.changed时候,如果不是全剧变量你取不到starPanLocation的值。


剩下的事情就比较简单了 你获得了abscissaChange 同理也能简单的到竖直方向的变化值,用这个值来对于你想要改变的值做一个加减计算就可以轻松实现 Slider的功能。


Prisma的slider的功能也应该是一个道理来实现的,在view上在设置一个UITextlabel 就能达到一摸一样的功效,(当然还要加上一个延迟消失的效果,可以用dispatch_after来实现)


Good Luck ,tlm . 

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容