我真的很难在网上找到教程以及已经回答的问题(我已经尝试了它们,但它们似乎不起作用).我有一个UI
ImageView,我在我的视图中心.我现在能够在屏幕上点击并拖动它.我希望能够缩放并旋转此视图.我该如何实现这一目标?我已经尝试过下面的旋转代码,但它似乎不起作用?任何帮助都将是一个巨大的帮助,并标记为答案.感谢你们.
import UIKit
class DraggableImage: UIImageView {
override func touchesBegan(_ touches: Set<UITouch>,with event: UIEvent?) {
self.backgroundColor = .blue
}
override func touchesEnded(_ touches: Set<UITouch>,with event: UIEvent?) {
self.backgroundColor = .green
}
override func touchesMoved(_ touches: Set<UITouch>,with event: UIEvent?) {
if let touch = touches.first {
let position = touch.location(in: superview)
center = CGPoint(x: position.x,y: position.y)
}
}
}
class CVController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .white
let rotateGesture = UIRotationGestureRecognizer(target: self,action: #selector(rotateAction(sender:)))
firstimageView.addGestureRecognizer(rotateGesture)
setupViews()
}
func rotateAction(sender: UIRotationGestureRecognizer) {
let rotatePoint = sender.location(in: view)
let firstimageView = view.hitTest(rotatePoint,with: nil)
firstimageView?.transform = (firstimageView?.transform.rotated(by: sender.rotation))!
sender.rotation = 0
}
let firstimageView: DraggableImage = {
let iv = DraggableImage()
iv.backgroundColor = .red
iv.isUserInteractionEnabled = true
return iv
}()
func setupViews() {
view.addSubview(firstimageView)
let firstimageWidth: CGFloat = 50
let firstimageHeight: CGFloat = 50
firstimageView.frame = CGRect(x: (view.frame.width / 2) - firstimageWidth / 2,y: (view.frame.height / 2) - firstimageWidth / 2,width: firstimageWidth,height: firstimageHeight)
}
}
您的代码中存在一些问题.首先,您需要将UIGestureRecognizerDelegate添加到视图控制器,并将其设置为手势识别器委托.并添加shouldRecognizeSimultaneously方法并返回true.第二,当应用比例时,您需要在捏开始时保存变换并在其顶部应用比例:
class DraggableImageView: UIImageView {
override func touchesBegan(_ touches: Set<UITouch>,with event: UIEvent?) {
backgroundColor = .blue
}
override func touchesEnded(_ touches: Set<UITouch>,with event: UIEvent?) {
backgroundColor = .green
}
override func touchesMoved(_ touches: Set<UITouch>,with event: UIEvent?) {
if let position = touches.first?.location(in: superview){
center = position
}
}
}
class ViewController: UIViewController,UIGestureRecognizerDelegate {
var identity = CGAffineTransform.identity
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .white
setupViews()
let pinchGesture = UIPinchGestureRecognizer(target: self,action: #selector(scale))
let rotationGesture = UIRotationGestureRecognizer(target: self,action: #selector(rotate))
pinchGesture.delegate = self
rotationGesture.delegate = self
view.addGestureRecognizer(pinchGesture)
view.addGestureRecognizer(rotationGesture)
}
let firstimageView: DraggableImageView = {
let iv = DraggableImageView()
iv.backgroundColor = .red
iv.isUserInteractionEnabled = true
return iv
}()
func setupViews() {
view.addSubview(firstimageView)
let firstimageWidth: CGFloat = 50
let firstimageHeight: CGFloat = 50
firstimageView.frame = CGRect(x: view.frame.midX - firstimageWidth / 2,y: view.frame.midY - firstimageWidth / 2,height: firstimageHeight)
}
func scale(_ gesture: UIPinchGestureRecognizer) {
switch gesture.state {
case .began:
identity = firstimageView.transform
case .changed,.ended:
firstimageView.transform = identity.scaledBy(x: gesture.scale,y: gesture.scale)
case .cancelled:
break
default:
break
}
}
func rotate(_ gesture: UIRotationGestureRecognizer) {
firstimageView.transform = firstimageView.transform.rotated(by: gesture.rotation)
}
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer,shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
return true
}
}