Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/neo4j/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 在ScrollView中缩放图像_Swift_Xcode_Uiscrollview_Pinchzoom_Zooming - Fatal编程技术网

Swift 在ScrollView中缩放图像

Swift 在ScrollView中缩放图像,swift,xcode,uiscrollview,pinchzoom,zooming,Swift,Xcode,Uiscrollview,Pinchzoom,Zooming,我有一个滚动视图,其中有一些图像,我水平滚动。我想用捏手势缩放。这是我正在使用的代码,但它不起作用。我似乎不知道我做错了什么。有什么建议吗 let scrollView = UIScrollView(frame: CGRect(x: 0, y: 100, width: UIScreen.main.bounds.size.width , height: (UIScreen.main.bounds.size.height - 140))) var frame: CGRect = CGRect(x:

我有一个滚动视图,其中有一些图像,我水平滚动。我想用捏手势缩放。这是我正在使用的代码,但它不起作用。我似乎不知道我做错了什么。有什么建议吗

let scrollView = UIScrollView(frame: CGRect(x: 0, y: 100, width: UIScreen.main.bounds.size.width , height: (UIScreen.main.bounds.size.height - 140))) var frame: CGRect = CGRect(x: 0, y: 0, width: 0, height: 0)
var subView = UIImageView()
override func viewDidLoad() {
   super.viewDidLoad()
   getOfferImages()
   swipeGesture()
   setupView()
   scrollView.delegate = self
   scrollView.minimumZoomScale = 1.0
   scrollView.maximumZoomScale = 4.0
 }
 func configureScrollView(){

   scrollView.backgroundColor = UIColor.black
   scrollView.showsHorizontalScrollIndicator = false
   scrollView.isUserInteractionEnabled = true
   scrollView.clipsToBounds = true
   scrollView.bouncesZoom = true

   pageNumberlabel.text = "1 of \(offersImagesArray.count)"
   self.view.addSubview(scrollView)

   for index in 0..<images.count {

       frame.origin.x = self.scrollView.frame.size.width * CGFloat(index)
       frame.size = self.scrollView.frame.size
       subView = UIImageView(frame: frame)
       subView.tag = 1
       subView.contentMode = .scaleToFill
       subView.isUserInteractionEnabled = true
       subView.sd_setImage(with: images[index], placeholderImage: UIImage(named:""), options: [.continueInBackground])
       self.scrollView.addSubview(subView)
   }
   self.scrollView.isPagingEnabled = true
   self.scrollView.contentSize = CGSize(width: (self.scrollView.frame.size.width *  CGFloat(images.count)), height: self.scrollView.frame.size.height)

 }
 func viewForZoomingInScrollView(scrollView: UIScrollView) -> UIView? {
  // let image = self.view.findSubview(withTag: 1)
   return self.subView
 }
let scrollView=UIScrollView(帧:CGRect(x:0,y:100,宽度:UIScreen.main.bounds.size.width,高度:(UIScreen.main.bounds.size.height-140)))var-frame:CGRect=CGRect(x:0,y:0,宽度:0,高度:0)
var subView=UIImageView()
重写func viewDidLoad(){
super.viewDidLoad()
getOfferImages()
swipeGesture()
setupView()
scrollView.delegate=self
scrollView.minimumZoomScale=1.0
scrollView.maximumZoomScale=4.0
}
func configureScrollView(){
scrollView.backgroundColor=UIColor.black
scrollView.showsHorizontalScrollIndicator=false
scrollView.isUserInteractionEnabled=true
scrollView.clipsToBounds=true
scrollView.bouncesZoom=true
pageNumberlabel.text=“第1页,共\(OfferImage.count)”
self.view.addSubview(滚动视图)
对于0..UIView中的索引{
//让image=self.view.findSubview(带标记:1)
返回self.subView
}

您需要将
PinchGestureRecognizer
添加到情节提要中,然后将其链接到如下函数

@IBAction func scaleImage(_ sender: UIPinchGestureRecognizer) {
    self.subView.transform = self.subView.transform.scaledBy(x: sender.scale, y: sender.scale)
    sender.scale = 1
}
注意:
子视图
应替换为您尝试缩放的视图。

1)设置缩放比例

scrollView.maximumZoomScale = 10
scrollView.minimumZoomScale = 1
2) 设置滚动视图代理,在视图中返回图像进行缩放

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

所以我尝试了这个方法,它确实能识别挤压手势,但它不会缩放图像,只是会闪烁一点。@Stephanie对你有用吗?或者你仍然有问题吗?我想缩放子视图,但问题是它只应用于scrollView中的最后一个图像,刚才我尝试用scrollView本身替换它虽然它不是我想要的样子,但它还是起作用了。谢谢你的帮助!看看另一个答案,这个答案实际上没有使用滚动视图来缩放。