我在使用手势时遇到问题。实际上,我想在捏手势上增加标签的字体,以便可以保持文本的质量,我很容易做到这一点。问题是,如果我将视图拖动到其他位置,然后应用捏手势,它将自动将视图放置在其实际位置上。
import UIKit
class ViewController: UIViewController, UIGestureRecognizerDelegate {
@IBOutlet weak var myLabel: UILabel!
@IBOutlet weak var myTextView: UITextView!
var initialCenter = CGPoint()
var pointSize: CGFloat = 0
override func viewDidLoad() {
super.viewDidLoad()
let pinchGesture = UIPinchGestureRecognizer(target: self, action: #selector(pinchRecoginze(_:)))
pinchGesture.delegate = self
myLabel.addGestureRecognizer(pinchGesture)
let rotationGesture = UIRotationGestureRecognizer(target: self, action: #selector(self.rotationGestureRecognizer(_:)))
rotationGesture.delegate = self
myLabel.addGestureRecognizer(rotationGesture)
let panGesture = UIPanGestureRecognizer(target: self, action: #selector(self.panGestureRecognizer(_:)))
panGesture.maximumNumberOfTouches = 1
panGesture.delegate = self
myLabel.addGestureRecognizer(panGesture)
}
@objc func panGestureRecognizer(_ recognizer: UIPanGestureRecognizer) {
guard recognizer.view != nil else {return}
let piece = recognizer.view!
let translation = recognizer.translation(in: piece.superview)
if recognizer.state == .began {
self.initialCenter = piece.center
}
// Update the position for the .began, .changed, and .ended states
if recognizer.state != .cancelled {
// Add the X and Y translation to the view's original position.
let newCenter = CGPoint(x: initialCenter.x + translation.x, y: initialCenter.y + translation.y)
piece.center = newCenter
} else {
// On cancellation, return the piece to its original location.
piece.center = initialCenter
}
print("2--", myLabel.frame)
}
@objc func rotationGestureRecognizer(_ recognizer: UIRotationGestureRecognizer) {
if let view = recognizer.view {
view.transform = view.transform.rotated(by: recognizer.rotation)
recognizer.rotation = 0.0
}
}
@IBAction func changeTextAction(_ sender: Any) {
myLabel.text = myTextView.text
self.myTextView.resignFirstResponder()
}
@objc func pinchRecoginze(_ pinchGesture: UIPinchGestureRecognizer) {
guard pinchGesture.view != nil else {return}
let view = pinchGesture.view!
if (pinchGesture.view is UILabel) {
let textLabel = view as! UILabel
if textLabel.text?.count == 0 {
return
}
if pinchGesture.state == .began {
let font = textLabel.font
pointSize = font!.pointSize
pinchGesture.scale = textLabel.font!.pointSize * 0.1
print("pinch began")
}
if 2 <= pinchGesture.scale, pinchGesture.scale <= 30 {// upto 30
textLabel.font = UIFont(name: textLabel.font!.fontName, size: pinchGesture.scale * 10)
}
}
}
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
return true
}
}
答案 0 :(得分:0)
您可以找到发生捏合手势的位置并在那里放大:
@objc func pinchRecoginze(_ pinchGesture: UIPinchGestureRecognizer) {
guard pinchGesture.view != nil else {return}
let view = pinchGesture.view!
let location = sender.location(in: view.superview)
view.center = location
}