点击手势 捏合手势 旋转手势 轻扫手势 拖拽手势 边缘平移手势 长按手势
//点击手势
func handleTap(tap: UITapGestureRecognizer){
if tap.state == .ended {
}
}
//捏合手势
func handlePinch(pinch: UIPinchGestureRecognizer) {
//时刻改变
if pinch.state == .began || pinch.state == .changed {
view.transform = view.transform.scaledBy(x: pinch.scale, y: pinch.scale)
pinch.scale = 1
// pinch.velocity 缩放速度 放大为正 缩小为负 单位 缩放比/秒
}
}
//旋转手势
func handleRotation(rotation: UIRotationGestureRecognizer) {
if rotation.state == .began || rotation.state == .changed {
view.transform = view.transform.rotated(by: rotation.rotation)
rotation.rotation = 0.0
// rotation.velocity 旋转速度 顺时针为正 逆时针为负 单位是 弧度/秒
}
}
//轻扫手势
func handleSwipe(swipe: UISwipeGestureRecognizer) {
if swipe.state == .ended {
}
}
var startCenter = CGPoint.zero
//拖拽手势
func handlePan(pan: UIPanGestureRecognizer){
// pan.setTranslation(<#T##translation: CGPoint##CGPoint#>, in: <#T##UIView?#>) 设置手指在某个view里面的偏移量
// pan.velocity(in: <#T##UIView?#>) 平移速度 单位 point/秒
let translation = pan.translation(in: view)
if pan.state == .began {
startCenter = view.center
}
//.began .changed .ended 状态时更新label的位置
if pan.state != .cancelled {
view.center = CGPoint(x: startCenter.x + translation.x, y: startCenter.y + translation.y)
}else{
//.cancelled 时恢复到原位置 被系统事件打断
view.center = startCenter
}
}
//边缘平移手势
func handleScreenEdgePan(screenEdgePan: UIScreenEdgePanGestureRecognizer){
let x = screenEdgePan.translation(in: view).x
if screenEdgePan.state == .began || screenEdgePan.state == .changed {
view.transform = view.transform.scaledBy(x: x, y: 0)
}else{
UIView.animate(withDuration: 0.3) {
self.view.transform = .identity
}
}
}
//长按手势
func handleLongPress(longPress: UILongPressGestureRecognizer){
if longPress.state == .ended {
view.backgroundColor = #colorLiteral(red: 0.521568656, green: 0.1098039225, blue: 0.05098039284, alpha: 1)
}
}
网友评论