2017-04-25 59 views
1

編輯:我已更新問題代碼,以便它可以幫助任何人。Swift3放大如何更改UICollectionView框架大小捏?

我有一個scrollViewUICollectionViewCell,但不能讓縮放工作在模擬器與捏。我可以通過手動縮放視圖來實現這一點,但當用戶捏合放大時,我需要將圖像視圖框更改爲全尺寸。現在它的大小可以填充幀寬設置爲屏幕寬度和幀高設置爲屏幕高度。我需要動態地改變幀的寬度,使其等於幀的高度,因爲圖像是方形的。

這裏是我的代碼:

import UIKit 

class MyCollectionViewCell: UICollectionViewCell, UIScrollViewDelegate { 

    let screenWidth = UIScreen.main.bounds.width 

    let activityIndicator: UIActivityIndicatorView = { 
     let spinner = UIActivityIndicatorView() 
     spinner.activityIndicatorViewStyle = .white 
     spinner.color = Constants.APP_SPINNER_COLOR 
     spinner.hidesWhenStopped = true 
     return spinner 
    }() 

    var zoomScrollView: UIScrollView = { 
     let scrollView = UIScrollView() 
     scrollView.backgroundColor = .white 
     scrollView.minimumZoomScale = 1.0 
     scrollView.maximumZoomScale = 6.0 
     scrollView.clipsToBounds = true 
     scrollView.isUserInteractionEnabled = true 
     scrollView.translatesAutoresizingMaskIntoConstraints = false 
     return scrollView 
    }() 

    var itemImageView: UIImageView = { 
     let imageView = UIImageView() 
     imageView.contentMode = .scaleAspectFit 
     imageView.translatesAutoresizingMaskIntoConstraints = false 
     imageView.backgroundColor = .white 
     imageView.isUserInteractionEnabled = true 
     return imageView 
    }() 

    func viewForZooming(in zoomScrollView: UIScrollView) -> UIView? { 
     return itemImageView 
    } 

    override init(frame: CGRect) { 
     super.init(frame: frame) 

     zoomScrollView.delegate = self 

     contentView.backgroundColor = .white 

     contentView.addSubview(zoomScrollView) 

     zoomScrollView.addSubview(itemImageView) 

     itemImageView.addSubview(activityIndicator) 


    zoomImageView.frame = CGRect(x:0, y:0, width: screenWidth, height: screenHeight) 

    zoomScrollView.frame = itemImageView.frame 

     activityIndicator.anchorCenterXToSuperview() 
     activityIndicator.anchorCenterYToSuperview()  
    } 

    required init?(coder aDecoder: NSCoder) { 
      fatalError("init(coder:) has not been implemented") 
     } 
} 
+0

什麼是ZoomimageView和Zoomscrollview –

回答

1

此處製作滾動視圖的簡單演示。

注:不給任何約束圖像視圖

下面的代碼了滾動:

import UIKit 

class ViewController: UIViewController,UIScrollViewDelegate { 

    var imgDemo: UIImageView = { 
     let img = UIImageView() 
     img.contentMode = .scaleAspectFill 
     img.isUserInteractionEnabled = true 
     return img 
    }() 


    var scrollView:UIScrollView = { 
     let scroll = UIScrollView() 
     scroll.maximumZoomScale = 4.0 
     scroll.minimumZoomScale = 0.25 
     scroll.clipsToBounds = true 
     return scroll 
    }() 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     imgDemo.frame = CGRect(x: 0, y: 0, width: view.frame.size.width, height: view.frame.size.height) 
     imgDemo.image = UIImage(named: "5.jpg") 
     scrollView.delegate = self 
     scrollView.frame = imgDemo.frame 
     scrollView.addSubview(imgDemo) 
     view.addSubview(scrollView) 
    } 

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

} 
+0

我需要這個視圖來適合.scaleAspectfit,直到我捏它,然後我想它scaleAspectFill,我該怎麼做? – markhorrocks

+0

Ohk然後由你自己做出來,因爲我不知道該怎麼做。 –

+0

我已經更新了問題,所以我的代碼現在可以工作。之前缺少的是我將scrollView.userInteraction設置爲false。感謝您輸入框架。 – markhorrocks