Swift - 带有手势的 UIImageView 子类 - 翻译不工作
Swift - UIImageView subclass with Gestures -- Translate Not Working
我在 Swift 中子类化 UIImageView 并在其中添加捏合、平移、缩放、旋转和点击手势。
我有一个基本的 ViewController 添加了一个新的 "PinchZoomImageView" 作为子视图。
一切正常,除了当我更改子类视图的中心时 Pan Gesture Translate 稍微关闭。
func handlePan(recognizer:UIPanGestureRecognizer) {
let translation = recognizer.translationInView(self)
if let view = recognizer.view {
view.center = CGPoint(x:view.center.x + translation.x, y:view.center.y + translation.y)
}
recognizer.setTranslation(CGPointZero, inView: self)
}
平移图像并在屏幕周围移动图像后,它的边界关闭。
想法?
解决了!
我没有将视图移动到它的中心 属性 (view.center),而是使用 view.transform(翻译 translation.x、translation.y)移动它通过 .center 直接作用于视图而不是框架...
func handlePan(识别器:UIPanGestureRecognizer){
let translation = recognizer.translationInView(recognizer.view)
if let view = recognizer.view {
view.transform = CGAffineTransformTranslate(view.transform, translation.x, translation.y)
}
recognizer.setTranslation(CGPointZero, inView: self)
}
我在 Swift 中子类化 UIImageView 并在其中添加捏合、平移、缩放、旋转和点击手势。
我有一个基本的 ViewController 添加了一个新的 "PinchZoomImageView" 作为子视图。
一切正常,除了当我更改子类视图的中心时 Pan Gesture Translate 稍微关闭。
func handlePan(recognizer:UIPanGestureRecognizer) {
let translation = recognizer.translationInView(self)
if let view = recognizer.view {
view.center = CGPoint(x:view.center.x + translation.x, y:view.center.y + translation.y)
}
recognizer.setTranslation(CGPointZero, inView: self)
}
平移图像并在屏幕周围移动图像后,它的边界关闭。
想法?
解决了!
我没有将视图移动到它的中心 属性 (view.center),而是使用 view.transform(翻译 translation.x、translation.y)移动它通过 .center 直接作用于视图而不是框架...
func handlePan(识别器:UIPanGestureRecognizer){
let translation = recognizer.translationInView(recognizer.view)
if let view = recognizer.view {
view.transform = CGAffineTransformTranslate(view.transform, translation.x, translation.y)
}
recognizer.setTranslation(CGPointZero, inView: self)
}