将视图更改为刷卡手势上的一个圆圈,iOS,Swift
问题描述:
我试图将刷卡手势(任何方向,快或慢)上的视图缩小为一个圆,类似于WhatsApp videoCall视图上的体验。查看下面的图片,了解我想要实现的目标。将视图更改为刷卡手势上的一个圆圈,iOS,Swift
我相信我需要使用滑动手势来实现这一点,我已经向我的videoView添加了滑动手势,我不确定接下来要做什么。
在viewDidLoad中我有以下
videoView.addGestureRecognizer(UISwipeGestureRecognizer(target: self, action: #selector(self.minimiseView)))
我想我需要用手势位置?而且我还需要设置随着滑动而增加的拐角半径。请问一些人,请告诉我如何才能做到这一点?
func minimiseView(gesture: UISwipeGestureRecognizer){
let location = gesture.location(in: self.view)
}
答
你基本上要执行以下步骤:
- 捕获起始姿势位置
- 在刷卡,从原来的刷卡
- 使用这个距离测量距离增加相机视角的圆角半径
- 例如设置
cornerRadius = distanceSwiped
- 例如设置
- 一旦拐角半径已经达到了一定的量(以及视图为圆形),再次捕获当前手势位置
- 使用此值来开始跟踪运动,并用它来减少宽度视图的
- 当视图是足够小驳回
这里是你如何完成这样的基本设置:
enum VideoDismissState {
case cornerRadiusChanging, sizeChanging, complete
}
var initialGesturePosition: CGPoint = .zero
var maxCornerRadiusGesturePosition: CGPoint = .zero
var dismissState: VideoDismissState = .complete
func minimiseView(_ gesture: UISwipeGestureRecognizer) {
let location = gesture.location(in: videoView)
switch gesture.state {
case .began:
initialGesturePosition = gesture.location(in: videoView)
dismissState = .cornerRadiusChanging
case .changed:
let currentPosition = gesture.location(in: videoView)
switch dismissState {
case cornerRadiusChanging:
let swipeDistance = distance(between: initialGesturePosition, and: currentPosition)
// play around with this formula to see what feels right
videoView.layer.cornerRadius = swipeDistance/2
// at a certain point, switch to changing the size
if swipeDistance >= videoView.width/2 {
maxCornerRadiusGesturePosition = currentPosition
dismissState = .sizeChanging
}
case sizeChanging:
let swipeDistance = distance(between: maxCornerGesturePosition, and: currentPosition)
// again try different things to see what feels right here
let scaleFactor = 50/swipeDistance
videoView.layer.transform = CGAffineTransform(scaledX: scaleFactor, y: scaleFactor
if scaleFactor <= 0.2 {
dismissState = .complete
}
case complete:
// reset values
initialGesturePosition = .zero
maxCornerRadiusGesturePosition = .zero
// dismiss videoView
// for example: videoView.isHidden = true OR videoView.removeFromSuperview()
}
case .ended:
// if the gesture ends too soon you may want to animate the view back to full screen
}
}
/// Measure distance between two points
func distance(between first: CGPoint, and second: CGPoint) -> CGFloat {
return sqrt((first.x - second.x)^2 + (first.y - second.y)^2)
}
这可能并不完全,因为我没有测试过,但基本的想法应该足以让你开始。
感谢您的指导,我不知道如何计算距离? 'distance(between:initialGesturePosition,and:currentPosition)' – user44776
@ user44776我在代码的末尾写了这个函数。这只是两个x,y坐标的基本距离公式。 – Paolo
我想我需要玩这个来达到我想要的地方,但它是一个很好的首发。 – user44776