iOS Swift,无法使用捏合手势
iOS Swift, cannot get pinch gesture to work
我有一个测试项目,它从文件中获取文本,将其添加到文本视图并显示它。
我想添加一些手势,但似乎无法使其工作......
这是相关代码:
class ViewController2: UIViewController, UIGestureRecognizerDelegate {
@IBOutlet var textview1: UITextView!
var pinchGesture = UIPinchGestureRecognizer()
override func viewDidLoad() {
super.viewDidLoad()
self.textview1.userInteractionEnabled = true
self.textview1.multipleTouchEnabled = true
self.pinchGesture.delegate = self
self.pinchGesture = UIPinchGestureRecognizer(target: self, action: #selector(ViewController2.pinchRecognized(_:)))
self.view.addGestureRecognizer(self.pinchGesture)
}
@IBAction func pinchRecognized(pinch: UIPinchGestureRecognizer) {
self.textview1.addGestureRecognizer(pinchGesture)
self.textview1.transform = CGAffineTransformScale(self.textview1.transform, pinch.scale, pinch.scale)
pinch.scale = 1.0
}
有什么想法吗?遵循了几个教程,但 none 似乎有所帮助。代码在实际 iPhone...
上进行了测试
非常感谢
编辑解决方案:
@IBAction func pinchRecognized(pinch: UIPinchGestureRecognizer) {
var pinchScale = pinchGesture.scale
pinchScale = round(pinchScale * 1000) / 1000.0
if (pinchScale < 1) {
self.textview1.font = UIFont(name: self.textview1.font!.fontName, size: self.textview1.font!.pointSize - pinchScale)
pinchScale = pinchGesture.scale
} else {
self.textview1.font = UIFont(name: self.textview1.font!.fontName, size: self.textview1.font!.pointSize + pinchScale)
pinchScale = pinchGesture.scale
}
}
感谢 nishith Singh
你在初始化前设置了self.pinchGesture
的委托。
- 首先初始化
self.pinchGesture
。
- 设置
delegate
.
将self.pinchGesture
添加到self.view
self.pinchGesture.delegate = self
self.pinchGesture = UIPinchGestureRecognizer(target: self, action:#selector(ViewController2.pinchRecognized(_:)))
self.view.addGestureRecognizer(self.pinchGesture)
尝试将手势识别器添加到 viewDidLoad 中的文本视图,而不是将其添加到 pinchRecognized 中。目前您正在将 pinchGesture 添加到文本视图后面的视图中,因此不会收到触摸
var pinchGesture = UIPinchGestureRecognizer()
使用此代码:
override func viewDidLoad() {
super.viewDidLoad()
self.textview1.userInteractionEnabled = true
self.textview1.multipleTouchEnabled = true
self.pinchGesture = UIPinchGestureRecognizer(target: self, action:#selector(pinchRecognized(_:)))
self.textview1.addGestureRecognizer(self.pinchGesture)
// Do any additional setup after loading the view.
}
@IBAction func pinchRecognized(_ pinch: UIPinchGestureRecognizer) {
let fontSize = self.textview1.font!.pointSize*(pinch.scale)/2
if fontSize > 12 && fontSize < 32{
textview1.font = UIFont(name: self.textview1.font!.fontName, size:fontSize)
}
}
您可能需要根据需要尝试使用最小和最大字体大小,目前最小字体大小为 12,最大字体大小为 32。
class ViewController2: UIViewController, UIGestureRecognizerDelegate {
@IBOutlet var textview1: UITextView!
var pinchGesture = UIPinchGestureRecognizer()
override func viewDidLoad() {
super.viewDidLoad()
self.textview1.userInteractionEnabled = true
self.textview1.multipleTouchEnabled = true
self.pinchGesture.delegate = self
self.pinchGesture = UIPinchGestureRecognizer(target: self, action: "pinchRecognized:")
self.view.addGestureRecognizer(self.pinchGesture)
}
func pinchRecognized(pinch: UIPinchGestureRecognizer) {
self.textview1.addGestureRecognizer(pinchGesture)
self.textview1.transform = CGAffineTransformScale(self.textview1.transform, pinch.scale, pinch.scale)
pinch.scale = 1.0
}
您的代码确实有效。但可能不是你想要的方式。
最初您将手势识别器分配给视图控制器的视图。
但随后在该方法中,您向 UITextView 添加了相同的手势识别器。
所以它应该在 UITextView 上工作。手势识别器从视图控制器的视图中移除。手势识别器只能有一个目标。选择视图控制器的视图或文本视图。
let pinchGesture = UIPinchGestureRecognizer(target: self, action:#selector(self.pinchGesture))
func pinchGesture(sender: UIPinchGestureRecognizer){
sender.view?.transform = (sender.view?.transform)!.scaledBy(x: sender.scale, y: sender.scale)
sender.scale = 1
print("pinch gesture")
}
我有一个测试项目,它从文件中获取文本,将其添加到文本视图并显示它。 我想添加一些手势,但似乎无法使其工作...... 这是相关代码:
class ViewController2: UIViewController, UIGestureRecognizerDelegate {
@IBOutlet var textview1: UITextView!
var pinchGesture = UIPinchGestureRecognizer()
override func viewDidLoad() {
super.viewDidLoad()
self.textview1.userInteractionEnabled = true
self.textview1.multipleTouchEnabled = true
self.pinchGesture.delegate = self
self.pinchGesture = UIPinchGestureRecognizer(target: self, action: #selector(ViewController2.pinchRecognized(_:)))
self.view.addGestureRecognizer(self.pinchGesture)
}
@IBAction func pinchRecognized(pinch: UIPinchGestureRecognizer) {
self.textview1.addGestureRecognizer(pinchGesture)
self.textview1.transform = CGAffineTransformScale(self.textview1.transform, pinch.scale, pinch.scale)
pinch.scale = 1.0
}
有什么想法吗?遵循了几个教程,但 none 似乎有所帮助。代码在实际 iPhone...
上进行了测试非常感谢
编辑解决方案:
@IBAction func pinchRecognized(pinch: UIPinchGestureRecognizer) {
var pinchScale = pinchGesture.scale
pinchScale = round(pinchScale * 1000) / 1000.0
if (pinchScale < 1) {
self.textview1.font = UIFont(name: self.textview1.font!.fontName, size: self.textview1.font!.pointSize - pinchScale)
pinchScale = pinchGesture.scale
} else {
self.textview1.font = UIFont(name: self.textview1.font!.fontName, size: self.textview1.font!.pointSize + pinchScale)
pinchScale = pinchGesture.scale
}
}
感谢 nishith Singh
你在初始化前设置了self.pinchGesture
的委托。
- 首先初始化
self.pinchGesture
。 - 设置
delegate
. 将
self.pinchGesture
添加到self.view
self.pinchGesture.delegate = self self.pinchGesture = UIPinchGestureRecognizer(target: self, action:#selector(ViewController2.pinchRecognized(_:))) self.view.addGestureRecognizer(self.pinchGesture)
尝试将手势识别器添加到 viewDidLoad 中的文本视图,而不是将其添加到 pinchRecognized 中。目前您正在将 pinchGesture 添加到文本视图后面的视图中,因此不会收到触摸
var pinchGesture = UIPinchGestureRecognizer()
使用此代码:
override func viewDidLoad() {
super.viewDidLoad()
self.textview1.userInteractionEnabled = true
self.textview1.multipleTouchEnabled = true
self.pinchGesture = UIPinchGestureRecognizer(target: self, action:#selector(pinchRecognized(_:)))
self.textview1.addGestureRecognizer(self.pinchGesture)
// Do any additional setup after loading the view.
}
@IBAction func pinchRecognized(_ pinch: UIPinchGestureRecognizer) {
let fontSize = self.textview1.font!.pointSize*(pinch.scale)/2
if fontSize > 12 && fontSize < 32{
textview1.font = UIFont(name: self.textview1.font!.fontName, size:fontSize)
}
}
您可能需要根据需要尝试使用最小和最大字体大小,目前最小字体大小为 12,最大字体大小为 32。
class ViewController2: UIViewController, UIGestureRecognizerDelegate {
@IBOutlet var textview1: UITextView!
var pinchGesture = UIPinchGestureRecognizer()
override func viewDidLoad() {
super.viewDidLoad()
self.textview1.userInteractionEnabled = true
self.textview1.multipleTouchEnabled = true
self.pinchGesture.delegate = self
self.pinchGesture = UIPinchGestureRecognizer(target: self, action: "pinchRecognized:")
self.view.addGestureRecognizer(self.pinchGesture)
}
func pinchRecognized(pinch: UIPinchGestureRecognizer) {
self.textview1.addGestureRecognizer(pinchGesture)
self.textview1.transform = CGAffineTransformScale(self.textview1.transform, pinch.scale, pinch.scale)
pinch.scale = 1.0
}
您的代码确实有效。但可能不是你想要的方式。
最初您将手势识别器分配给视图控制器的视图。
但随后在该方法中,您向 UITextView 添加了相同的手势识别器。
所以它应该在 UITextView 上工作。手势识别器从视图控制器的视图中移除。手势识别器只能有一个目标。选择视图控制器的视图或文本视图。
let pinchGesture = UIPinchGestureRecognizer(target: self, action:#selector(self.pinchGesture))
func pinchGesture(sender: UIPinchGestureRecognizer){
sender.view?.transform = (sender.view?.transform)!.scaledBy(x: sender.scale, y: sender.scale)
sender.scale = 1
print("pinch gesture")
}