代码如下
class ZoomView: UIImageView {
var Drag: UIPanGestureRecognizer!
var Rotate: UIRotationGestureRecognizer!
var Scale: UIPinchGestureRecognizer!
required init?(coder: NSCoder) {
super.init(coder: coder)
backgroundColor = .red
Drag = UIPanGestureRecognizer(target: self, action: #selector(onDrag(gesture:)))
Rotate = UIRotationGestureRecognizer(target: self, action: #selector(onRotate(gesture:)))
Scale = UIPinchGestureRecognizer(target: self, action: #selector(onScale(gesture:)))
addGestureRecognizer(Drag)
addGestureRecognizer(Rotate)
addGestureRecognizer(Scale)
}
//拖拽手势方法具体实现
@objc func onDrag(gesture: UIPanGestureRecognizer) {
let translation = gesture.translation(in: self.superview)
var center = gesture.view!.center
center.x += translation.x
center.y += translation.y
gesture.view!.center = center
gesture.setTranslation(CGPoint.zero, in: self.superview)
}
//旋转手势方法具体实现
@objc func onRotate(gesture: UIRotationGestureRecognizer?) {
if let transform = gesture?.view?.transform.rotated(by: gesture?.rotation ?? 0.0) {
gesture?.view?.transform = transform
}
gesture?.rotation = 0
}
//缩放手势方法具体实现
@objc func onScale(gesture: UIPinchGestureRecognizer?) {
if let transform = gesture?.view?.transform.scaledBy(x: gesture?.scale ?? 0.0, y: gesture?.scale ?? 0.0) {
gesture?.view?.transform = transform
}
gesture?.scale = 1
}
}
StoryBoard使用
使用截图.png
网友评论