UIImageView按键快速缩放

u59ebvdq  于 2022-09-19  发布在  Swift
关注(0)|答案(14)|浏览(208)

我希望有人能帮帮我。我正在尝试允许用户在UIImageView上收缩缩放(允许最大和最小级别)。但由于某些原因,它不能正常工作。图像会稍微放大,然后就会反弹回来。谢谢。

这是变焦功能

func zoom(sender:UIPinchGestureRecognizer) {

    if sender.state == .Ended || sender.state == .Changed {

        let currentScale = self.view.frame.size.width / self.view.bounds.size.width
        var newScale = currentScale*sender.scale

        if newScale < 1 {
            newScale = 1
        }
        if newScale > 9 {
            newScale = 9
        }

        let transform = CGAffineTransformMakeScale(newScale, newScale)

        self.imageView?.transform = transform
        sender.scale = 1

    }

}
aor9mmx1

aor9mmx11#

UIImageView使用UIScrollView缩小缩放||SWIFT 3和Xcode 8字母中的iOS图像缩放Youtube video URL

在情节提要中设置uiscrollview代理

class PhotoDetailViewController: UIViewController, UIScrollViewDelegate {

    @IBOutlet weak var scrollView: UIScrollView!
    @IBOutlet weak var imgPhoto: UIImageView!

    override func viewDidLoad() {

        super.viewDidLoad()

        scrollView.minimumZoomScale = 1.0
        scrollView.maximumZoomScale = 6.0        
        // scrollView.delegate = self - it is set on the storyboard.
    }  

    func viewForZooming(in scrollView: UIScrollView) -> UIView? {

        return imgPhoto
    }
57hvy0tb

57hvy0tb2#

我决定将ImageView添加到UIScrollView中。它允许用户进行缩放和平移。以下是我使用的代码。

为了设置我使用的最大/最小缩放:

scrollImg.minimumZoomScale = 1.0
    scrollImg.maximumZoomScale = 10.0

以下是代码的其余部分。

var vWidth = self.view.frame.width
    var vHeight = self.view.frame.height

    var scrollImg: UIScrollView = UIScrollView()
    scrollImg.delegate = self
    scrollImg.frame = CGRectMake(0, 0, vWidth!, vHeight!)
    scrollImg.backgroundColor = UIColor(red: 90, green: 90, blue: 90, alpha: 0.90)
    scrollImg.alwaysBounceVertical = false
    scrollImg.alwaysBounceHorizontal = false
    scrollImg.showsVerticalScrollIndicator = true
    scrollImg.flashScrollIndicators()

    scrollImg.minimumZoomScale = 1.0
    scrollImg.maximumZoomScale = 10.0

    defaultView!.addSubview(scrollImg)

    imageView!.layer.cornerRadius = 11.0
    imageView!.clipsToBounds = false
    scrollImg.addSubview(imageView!)

我还必须加上这一点

func viewForZoomingInScrollView(scrollView: UIScrollView) -> UIView? {
    return self.imageView
}

SWIFT 3及以上功能原型

func viewForZooming(in scrollView: UIScrollView) -> UIView? {
    return self.mainImage
}
pxy2qtax

pxy2qtax3#

支持SWIFT 5.1,可以创建UIImageView的扩展,如下所示:

extension UIImageView {
  func enableZoom() {
    let pinchGesture = UIPinchGestureRecognizer(target: self, action: #selector(startZooming(_:)))
    isUserInteractionEnabled = true
    addGestureRecognizer(pinchGesture)
  }

  @objc
  private func startZooming(_ sender: UIPinchGestureRecognizer) {
    let scaleResult = sender.view?.transform.scaledBy(x: sender.scale, y: sender.scale)
    guard let scale = scaleResult, scale.a > 1, scale.d > 1 else { return }
    sender.view?.transform = scale
    sender.scale = 1
  }
}
5uzkadbs

5uzkadbs4#

SWIFT 4的选项

class ViewController: UIViewController, UIScrollViewDelegate {

@IBOutlet weak var scrolView: UIScrollView!
@IBOutlet weak var imgPhoto: UIImageView!

  override func viewDidLoad() {
    super.viewDidLoad()
    scrolView.delegate = self
    scrolView.minimumZoomScale = 1.0
    scrolView.maximumZoomScale = 10.0
  }

  func viewForZooming(in scrollView: UIScrollView) -> UIView? {
    return imgPhoto
  }
}
e37o9pze

e37o9pze5#

您可以使用开源的ImageScrollView,这是一种可缩放和可滚动的图像视图。http://github.com/huynguyencong/ImageScrollView

像这个开源软件一样,将ImageView添加到ScrollView中

open class ImageScrollView: UIScrollView {
   var zoomView: UIImageView? = nil
}

extension ImageScrollView: UIScrollViewDelegate{

    public func viewForZooming(in scrollView: UIScrollView) -> UIView? {
        return zoomView
    }

    public func scrollViewDidZoom(_ scrollView: UIScrollView) {
        adjustFrameToCenter()
    }
}
oalqel3c

oalqel3c6#

在使用SWIFT 5.0时,以下是它对我的作用:

let myImageView = UIImageView(image: myImage)
myImageView.isUserInteractionEnabled = true
let pinchMethod = UIPinchGestureRecognizer(target: self, action: #selector(pinchImage(sender:)))
myImageView.addGestureRecognizer(pinchMethod)

@objc func pinchImage(sender: UIPinchGestureRecognizer) {
  guard let sender.view != nil else { return }

if let scale = (sender.view?.transform.scaledBy(x: sender.scale, y: sender.scale)) {
  guard scale.a > 1.0 else { return }
  guard scale.d > 1.0 else { return }
  sender.view?.transform = scale
  sender.scale = 1.0
 }
}

您可以使用scale.a、scale.b、scale.c、scale.d、scale.tx和scale.ty来设置缩放限制。

34gzjxbg

34gzjxbg7#

在我看来,问题在于您对CurrentScale的决心。它始终等于1,因为您更改了ImageView的比例。您应该按如下方式分配您的CurentScale:

let currentScale = self.imageView?.frame.size.width / self.imageView?.bounds.size.width
xsuvu9jc

xsuvu9jc8#

SWIFT 3解决方案

默认情况下,禁用UIImageView's``userInteration。在UIImageView中添加任何手势之前启用它。

imgView.isUserInteractionEnabled = true
相对于屏幕坐标中两个接触点的比例系数

var lastScale:CGFloat!
func zoom(gesture:UIPinchGestureRecognizer) {
    if(gesture.state == .began) {
        // Reset the last scale, necessary if there are multiple objects with different scales
        lastScale = gesture.scale
    }
    if (gesture.state == .began || gesture.state == .changed) {
    let currentScale = gesture.view!.layer.value(forKeyPath:"transform.scale")! as! CGFloat
    // Constants to adjust the max/min values of zoom
    let kMaxScale:CGFloat = 2.0
    let kMinScale:CGFloat = 1.0
    var newScale = 1 -  (lastScale - gesture.scale)
    newScale = min(newScale, kMaxScale / currentScale)
    newScale = max(newScale, kMinScale / currentScale)
    let transform = (gesture.view?.transform)!.scaledBy(x: newScale, y: newScale);
    gesture.view?.transform = transform
    lastScale = gesture.scale  // Store the previous scale factor for the next pinch gesture call
  }
}
0s7z1bwu

0s7z1bwu9#

SWIFT 3解决方案

这是我用过的代码。我将ImageView作为子视图添加到ScrollView中。

class ZoomViewController: UIViewController,UIScrollViewDelegate {

@IBOutlet weak var scrollView:UIScrollView!
@IBOutlet weak var imageView:UIImageView!

override func viewDidLoad() {

        super.viewDidLoad()
        scrollView.delegate = self

        scrollView.minimumZoomScale = 1.0
        scrollView.maximumZoomScale = 10.0//maximum zoom scale you want
        scrollView.zoomScale = 1.0

}

func viewForZooming(in scrollView: UIScrollView) -> UIView? {
        return imageView
}
yzuktlbb

yzuktlbb10#

我发布了一个答案here,用于在Swift 3中使用按压和双击的缩放功能。效果非常好。

noj0wjuj

noj0wjuj11#

我认为最大的问题是在func的末尾,你有sender.scale = 1。如果您删除了这行代码,您的图像应该不会每次都反弹回来。

z5btuh9x

z5btuh9x12#

这是一个老问题,但我没有看到任何解释原始代码错误的答案。

这一行:

let currentScale = self.view.frame.size.width / self.view.bounds.size.width

正在处理主视图而不是ImageView,因此比例计算始终为~1

这一简单的更改使它的行为符合预期

let currentScale = sender.view!.frame.size.width / sender.view!.bounds.size.width

通过将自身更改为发送者(并强制视图展开),比例计算按预期工作。

67up9zun

67up9zun13#

我最终来到了这里,可能找错了方向。

我在内容模式=.center中使用了我的ImageView。但我认为它太放大了,我正在寻找一种方法来缩小它。以下是方法:

self.imageView.contentScaleFactor = 3

1就好像你在做任何事情。超过1将缩小...3对我来说很好用,但你需要测试一下。

xmd2e60i

xmd2e60i14#

[2022年9月,SWIFT 5]

其中一种方法似乎对我来说很好用,那就是将图像视图嵌入到滚动视图中,您可以快速查看我共享的库,以便更好地理解,以简短地回答这个问题。我已经建立了这个小库,并将其用于生产应用程序。您可以将文件安装或直接复制粘贴到您的项目中。它很容易作为UIView使用,并在需要时听取其委托以获得更多选项。

该库名为Interactive ImageView,支持iOS 11.0及更高版本,链接至GitHub:https://github.com/egzonpllana/InteractiveImageView

相关问题