編輯:我已更新問題代碼,以便它可以幫助任何人。Swift3放大如何更改UICollectionView框架大小捏?
我有一個scrollView
在UICollectionViewCell
,但不能讓縮放工作在模擬器與捏。我可以通過手動縮放視圖來實現這一點,但當用戶捏合放大時,我需要將圖像視圖框更改爲全尺寸。現在它的大小可以填充幀寬設置爲屏幕寬度和幀高設置爲屏幕高度。我需要動態地改變幀的寬度,使其等於幀的高度,因爲圖像是方形的。
這裏是我的代碼:
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")
}
}
什麼是ZoomimageView和Zoomscrollview –