我正试着用pangesturerecognizer旋转图像视图。基本上,我根据x坐标旋转图像。我就快到了,但我遇到了几个错误。
我通过这样做来设置手势识别器(我必须以编程方式执行所有操作):
let personDrag = UIPanGestureRecognizer(target: self, action: #selector(foo.personDragRecognizer))
rPersonImageView.isUserInteractionEnabled = true
rPersonImageView.addGestureRecognizer(personDrag)
self.fooVarTap6 = personDrag //the class is called foo
下面是手势识别器的功能:
@objc func personDragRecognizer(_ gestureRecognizer: UIPanGestureRecognizer) {
guard personDrag.state == .began || personDrag.state == .changed else {
return
}
guard let imgView = personDrag.view else {
return
}
let translation = personDrag.translation(in: self.view)
imgView.center = CGPoint(x: imgView.center.x + translation.x, y: imgView.center.y + translation.y)
personDrag.setTranslation(CGPoint.zero, in: self.view)
//originally had self before .degreesToRadians
let angle: CGFloat = rPersonImageView.degreesToRadians(180.0)
/* After reaching x point case - rotating and setting rotation occured to prohibit further rotation */
if imgView.layer.frame.origin.x <= 190, currentRotation != .xPoint190 {
imgView.transform = imgView.transform.rotated(by: angle)
currentRotation = .xPoint190
} else if imgView.layer.frame.origin.x >= 300, currentRotation != .xPoint300 {
imgView.transform = imgView.transform.rotated(by: angle)
currentRotation = .xPoint300
}
}
在我提到personDrag的每一处地方,它都说“使用未解析的标识符personDrag”,但正如您所看到的,我定义了personDrag。它还说“UIImageView没有成员degreestorand”,但堆栈上的其他人说它应该这样做。有人能帮我吗?任何帮助都将不胜感激!!先谢谢你。
干杯,
西奥
最佳答案
personDrag
被定义为手势识别器,分配正确,编码正确。但在定义的personDragRecognizer()
中,每个引用都应该与gestureRecognizer
相反。
最简单的解决方法是将函数重命名为:
func personDragRecognizer(_ personDrag UIPanGestureRecognizer) {
// your code here
}
另一个解决方案是将内部的每个引用重命名为:
guard gestureRecognizer.state == .began || gestureRecognizer.state == .changed else {
return
}
等等等等。
关于ios - 使用UIPanGestureRecognizer旋转ImageView,错误—快速,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42987855/