gpt4 book ai didi

ios - 双击放大/缩小

转载 作者:可可西里 更新时间:2023-11-01 00:40:35 24 4
gpt4 key购买 nike

我有一个应用程序,当用户在文本 ListView 屏幕中按下按钮时,它会显示一堆图片。我有一个特定的子类,允许用户捏和放大,但我很好奇我需要在我的特定 swift 文件中输入什么代码以允许用户双击以放大和缩小。使用此代码时出现三个错误。两个说“'ZoomingScrollView'类型的值没有成员'scrollview'或'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 导出的 View Controller 复制了一些代码。我猜你的 viewForZooming: UIView 是你正在放大的 ImageView 。你也不必再引用 self.scrollView 因为 self 是 ScrollView ,因为你是 ScrollView 的子类:)我认为下面的代码应该解决你的问题(注意:它是最新的快速语法。你发布的是不是,所以如果有必要,您可能必须将代码切换回旧样式。不过,您应该尝试使用最新的 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;
}
}

关于ios - 双击放大/缩小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44207783/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com