双击缩放 In/Out
Double Tap to Zoom In/Out
我有一个应用程序,当用户在文本列表视图屏幕中按下按钮时,它会显示一堆图片。我有一个特定的子类,允许用户捏和放大,但我很好奇我需要在我的特定 swift 文件中输入什么代码以允许用户双击以放大和缩小。使用此代码时出现三个错误。两个说 "Value of type 'ZoomingScrollView' has no member 'scrollview' or 'imageview' " 和“'CGRectZero' 在 Swift 中不可用” 下面是我允许用户放大的子类代码以及我用来双击缩放的代码.
import UIKit
class ZoomingScrollView: UIScrollView, UIScrollViewDelegate {
@IBOutlet weak var viewForZooming: UIView? = nil {
didSet {
self.delegate = self
} }
func viewForZoomingInScrollView(scrollView: UIScrollView) -> UIView? {
return viewForZooming }
@IBAction func userDoubleTappedScrollview(recognizer: UITapGestureRecognizer) {
if let scrollV = self.scrollview {
if (scrollV.zoomScale > scrollV.minimumZoomScale) {
scrollV.setZoomScale(scrollV.minimumZoomScale, animated: true)
}
else {
//(I divide by 3.0 since I don't wan't to zoom to the max upon the double tap)
let zoomRect = self.zoomRectForScale(scrollV.maximumZoomScale / 3.0, center: recognizer.locationInView(recognizer.view))
self.scrollview?.zoomToRect(zoomRect, animated: true)
}
} }
func zoomRectForScale(scale : CGFloat, center : CGPoint) -> CGRect {
var zoomRect = CGRectZero
if let imageV = self.imageView {
zoomRect.size.height = imageV.frame.size.height / scale;
zoomRect.size.width = imageV.frame.size.width / scale;
let newCenter = imageV.convertPoint(center, fromView: self.scrollview)
zoomRect.origin.x = newCenter.x - ((zoomRect.size.width / 2.0));
zoomRect.origin.y = newCenter.y - ((zoomRect.size.height / 2.0));
}
return zoomRect; }}
问题似乎是您从具有 scrollView 和 imageView 出口的视图控制器复制了一些代码。我猜你的 viewForZooming: UIView 是你正在放大的图像视图。您也不必再引用 self.scrollView 因为 self 是滚动视图,因为您是滚动视图的子类 :) 我认为下面的代码应该可以解决您的问题(注意:它是最新的 swift语法。您发布的内容不是,因此如果有必要,您可能必须将代码切换回旧样式。不过您应该尝试使用最新的 Swift)。祝你好运,如果您有任何问题,请告诉我。
import UIKit
class ZoomingScrollView: UIScrollView, UIScrollViewDelegate {
@IBOutlet weak var viewForZooming: UIView? = nil {
didSet {
self.delegate = self
} }
func viewForZoomingInScrollView(scrollView: UIScrollView) -> UIView? {
return viewForZooming }
@IBAction func userDoubleTappedScrollview(recognizer: UITapGestureRecognizer) {
if (zoomScale > minimumZoomScale) {
setZoomScale(minimumZoomScale, animated: true)
}
else {
//(I divide by 3.0 since I don't wan't to zoom to the max upon the double tap)
let zoomRect = zoomRectForScale(scale: maximumZoomScale / 3.0, center: recognizer.location(in: recognizer.view))
zoom(to: zoomRect, animated: true)
}
}
func zoomRectForScale(scale : CGFloat, center : CGPoint) -> CGRect {
var zoomRect = CGRect.zero
if let imageV = self.viewForZooming {
zoomRect.size.height = imageV.frame.size.height / scale;
zoomRect.size.width = imageV.frame.size.width / scale;
let newCenter = imageV.convert(center, from: self)
zoomRect.origin.x = newCenter.x - ((zoomRect.size.width / 2.0));
zoomRect.origin.y = newCenter.y - ((zoomRect.size.height / 2.0));
}
return zoomRect;
}
}
我有一个应用程序,当用户在文本列表视图屏幕中按下按钮时,它会显示一堆图片。我有一个特定的子类,允许用户捏和放大,但我很好奇我需要在我的特定 swift 文件中输入什么代码以允许用户双击以放大和缩小。使用此代码时出现三个错误。两个说 "Value of type 'ZoomingScrollView' has no member 'scrollview' or 'imageview' " 和“'CGRectZero' 在 Swift 中不可用” 下面是我允许用户放大的子类代码以及我用来双击缩放的代码.
import UIKit
class ZoomingScrollView: UIScrollView, UIScrollViewDelegate {
@IBOutlet weak var viewForZooming: UIView? = nil {
didSet {
self.delegate = self
} }
func viewForZoomingInScrollView(scrollView: UIScrollView) -> UIView? {
return viewForZooming }
@IBAction func userDoubleTappedScrollview(recognizer: UITapGestureRecognizer) {
if let scrollV = self.scrollview {
if (scrollV.zoomScale > scrollV.minimumZoomScale) {
scrollV.setZoomScale(scrollV.minimumZoomScale, animated: true)
}
else {
//(I divide by 3.0 since I don't wan't to zoom to the max upon the double tap)
let zoomRect = self.zoomRectForScale(scrollV.maximumZoomScale / 3.0, center: recognizer.locationInView(recognizer.view))
self.scrollview?.zoomToRect(zoomRect, animated: true)
}
} }
func zoomRectForScale(scale : CGFloat, center : CGPoint) -> CGRect {
var zoomRect = CGRectZero
if let imageV = self.imageView {
zoomRect.size.height = imageV.frame.size.height / scale;
zoomRect.size.width = imageV.frame.size.width / scale;
let newCenter = imageV.convertPoint(center, fromView: self.scrollview)
zoomRect.origin.x = newCenter.x - ((zoomRect.size.width / 2.0));
zoomRect.origin.y = newCenter.y - ((zoomRect.size.height / 2.0));
}
return zoomRect; }}
问题似乎是您从具有 scrollView 和 imageView 出口的视图控制器复制了一些代码。我猜你的 viewForZooming: UIView 是你正在放大的图像视图。您也不必再引用 self.scrollView 因为 self 是滚动视图,因为您是滚动视图的子类 :) 我认为下面的代码应该可以解决您的问题(注意:它是最新的 swift语法。您发布的内容不是,因此如果有必要,您可能必须将代码切换回旧样式。不过您应该尝试使用最新的 Swift)。祝你好运,如果您有任何问题,请告诉我。
import UIKit
class ZoomingScrollView: UIScrollView, UIScrollViewDelegate {
@IBOutlet weak var viewForZooming: UIView? = nil {
didSet {
self.delegate = self
} }
func viewForZoomingInScrollView(scrollView: UIScrollView) -> UIView? {
return viewForZooming }
@IBAction func userDoubleTappedScrollview(recognizer: UITapGestureRecognizer) {
if (zoomScale > minimumZoomScale) {
setZoomScale(minimumZoomScale, animated: true)
}
else {
//(I divide by 3.0 since I don't wan't to zoom to the max upon the double tap)
let zoomRect = zoomRectForScale(scale: maximumZoomScale / 3.0, center: recognizer.location(in: recognizer.view))
zoom(to: zoomRect, animated: true)
}
}
func zoomRectForScale(scale : CGFloat, center : CGPoint) -> CGRect {
var zoomRect = CGRect.zero
if let imageV = self.viewForZooming {
zoomRect.size.height = imageV.frame.size.height / scale;
zoomRect.size.width = imageV.frame.size.width / scale;
let newCenter = imageV.convert(center, from: self)
zoomRect.origin.x = newCenter.x - ((zoomRect.size.width / 2.0));
zoomRect.origin.y = newCenter.y - ((zoomRect.size.height / 2.0));
}
return zoomRect;
}
}