2017-04-25 146 views
1

我有一個滾動視圖,裏面有一些圖像,我水平滾動。我想使用捏手勢進行縮放。這是我正在使用的代碼,但它不起作用。我似乎無法弄清楚我做錯了什麼。有什麼建議嗎?在ScrollView中縮放圖像

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 
} 

回答

1

您需要將PinchGestureRecognizer添加到您的故事板,然後將其鏈接到像下面這樣的功能。

@IBAction func scaleImage(_ sender: UIPinchGestureRecognizer) { 
    self.subView.transform = self.subView.transform.scaledBy(x: sender.scale, y: sender.scale) 
    sender.scale = 1 
} 

注:subView應該要嘗試縮放視圖來代替。

+0

所以,我想這回你的形象,它會識別縮放手勢,但它不不放大圖像,它只是閃爍一點。 – Stephanie

+0

@Stephanie請參閱編輯。 – MwcsMac

+0

@Stephanie爲你工作過嗎?還是你仍然有問題? – MwcsMac

0

1)集變焦倍數

scrollView.maximumZoomScale = 10 
scrollView.minimumZoomScale = 1 

2)設置滾動視圖的委託,在viewForZooming

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