我有以下问题: 我有下一个tableviewcell架构,
问题在于,当我放大UIImageView时,它总是会弹回到图片的左上角,任何缩放。当我缩小时,可以看到缩小到左上角。
没有约束,一切都是以编程方式创建的
尝试了我在堆栈溢出时可以找到的所有技术,但没有解决它。我认为原因是在uitableviewcell中执行此操作有问题。那就是我的TableViewCell.swift里面的内容
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
self.frame = CGRectMake(0, 0, screenWidth, screenWidth)
self.ImageView.frame = CGRectMake(0,0,screenWidth,screenWidth)
self.CameraView.frame = CGRectMake(0,0,screenWidth,screenWidth)
self.ScrollView.frame = CGRectMake(0,0,screenWidth,screenWidth)
self.addSubview(CameraView)
//!----Scroll view----//
ScrollView.delegate = self
ScrollView.alwaysBounceHorizontal = false
ScrollView.alwaysBounceVertical = false
ScrollView.bounces = false
ScrollView.bouncesZoom = false
ScrollView.minimumZoomScale = 1.0
ScrollView.maximumZoomScale = 10.0
ScrollView.contentSize = CGSizeMake(screenWidth, screenWidth)
ScrollView.pagingEnabled = false
var doubleTapRecognizer = UITapGestureRecognizer(target: self, action: "scrollViewDoubleTapped:")
doubleTapRecognizer.numberOfTapsRequired = 2
doubleTapRecognizer.numberOfTouchesRequired = 1
ScrollView.addGestureRecognizer(doubleTapRecognizer)
self.contentView.addSubview(ScrollView)
self.ScrollView.addSubview(ImageView)
var centerPoint = CGPointMake(CGRectGetMidX(self.ScrollView.bounds), CGRectGetMidY(self.ScrollView.bounds))
ImageView.center = centerPoint
ScrollView.center = centerPoint
ImageView.userInteractionEnabled = true
ImageView.clipsToBounds = false
centerScrollViewContents()
}
func centerScrollViewContents(){
var boundsSize:CGSize = self.ScrollView.bounds.size
var contentsFrame = self.ImageView.frame
if (contentsFrame.size.width < boundsSize.width){
contentsFrame.origin.x = (boundsSize.width - contentsFrame.size.width)/2
}else{
contentsFrame.origin.x = 0
}
if (contentsFrame.size.height < boundsSize.height){
contentsFrame.origin.y = (boundsSize.height - contentsFrame.size.height)/2
}else{
contentsFrame.origin.y = 0
}
self.ImageView.frame = contentsFrame
}
func scrollViewDoubleTapped(recognizer: UITapGestureRecognizer) {
// 1
let pointInView = recognizer.locationInView(ImageView)
// 2
var newZoomScale = ScrollView.zoomScale * 1.5
newZoomScale = min(newZoomScale, ScrollView.maximumZoomScale)
// 3
let scrollViewSize = ScrollView.bounds.size
let w = scrollViewSize.width / newZoomScale
let h = scrollViewSize.height / newZoomScale
let x = pointInView.x - (w / 2.0)
let y = pointInView.y - (h / 2.0)
let rectToZoomTo = CGRectMake(x, y, w, h);
// 4
ScrollView.zoomToRect(rectToZoomTo, animated: true)
}
func scrollViewDidZoom(scrollView: UIScrollView) {
centerScrollViewContents()
}
func viewForZoomingInScrollView(scrollView: UIScrollView) -> UIView? {
return self.ImageView
}