Dan*_*ark 198
答案在这里:
滚动视图还处理内容的缩放和平移.当用户进行捏合或捏合手势时,滚动视图调整内容的偏移和缩放.当手势结束时,管理内容视图的对象应该根据需要更新内容的子视图.(请注意,手势可能会结束,手指仍然可能已关闭.)当手势正在进行时,滚动视图不会向子视图发送任何跟踪调用.
UIScrollView类可以有一个必须采用UIScrollViewDelegate协议的委托.要使缩放和平移工作,委托必须实现viewForZoomingInScrollView:和scrollViewDidEndZooming:withView:atScale:; 此外,最大(maximumZoomScale)和最小(minimumZoomScale)缩放比例必须不同.
所以:
UIScrollViewDelegate并设置的委托delegateUIScrollViewviewForZoomingInScrollView:(必须返回内容查看你感兴趣的缩放).您也可以选择实施scrollViewDidEndZooming:withView:atScale:.UIScrollView实例上,您必须设置minimumZoomScale和maximumZoomScale不同(默认情况下它们是1.0).注意:有趣的是,如果你想打破缩放的话.nil在viewForZooming...方法中返回是否足够?它确实打破了缩放,但是一些手势将被搞砸(两个手指).因此,要打破缩放,您应将最小和最大缩放比例设置为1.0.
阅读这篇Ray Wenderlich教程:
http://www.raywenderlich.com/76436/use-uiscrollview-scroll-zoom-content-swift
如果您按照"滚动和缩放较大图像"部分进行操作,它将获得图像并使您能够进行捏合和缩放.
如果链接被更改,这里是主要信息:将此代码放在视图控制器中(这将设置主要功能):
override func viewDidLoad() {
super.viewDidLoad()
// 1
let image = UIImage(named: "photo1.png")!
imageView = UIImageView(image: image)
imageView.frame = CGRect(origin: CGPoint(x: 0, y: 0), size:image.size)
scrollView.addSubview(imageView)
// 2
scrollView.contentSize = image.size
// 3
var doubleTapRecognizer = UITapGestureRecognizer(target: self, action: "scrollViewDoubleTapped:")
doubleTapRecognizer.numberOfTapsRequired = 2
doubleTapRecognizer.numberOfTouchesRequired = 1
scrollView.addGestureRecognizer(doubleTapRecognizer)
// 4
let scrollViewFrame = scrollView.frame
let scaleWidth = scrollViewFrame.size.width / scrollView.contentSize.width
let scaleHeight = scrollViewFrame.size.height / scrollView.contentSize.height
let minScale = min(scaleWidth, scaleHeight);
scrollView.minimumZoomScale = minScale;
// 5
scrollView.maximumZoomScale = 1.0
scrollView.zoomScale = minScale;
// 6
centerScrollViewContents()
}
Run Code Online (Sandbox Code Playgroud)
将其添加到课程中:
func centerScrollViewContents() {
let boundsSize = scrollView.bounds.size
var contentsFrame = imageView.frame
if contentsFrame.size.width < boundsSize.width {
contentsFrame.origin.x = (boundsSize.width - contentsFrame.size.width) / 2.0
} else {
contentsFrame.origin.x = 0.0
}
if contentsFrame.size.height < boundsSize.height {
contentsFrame.origin.y = (boundsSize.height - contentsFrame.size.height) / 2.0
} else {
contentsFrame.origin.y = 0.0
}
imageView.frame = contentsFrame
}
Run Code Online (Sandbox Code Playgroud)
如果你想要识别双击手势,那么这个:
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)
}
Run Code Online (Sandbox Code Playgroud)
如果你想要更多的细节阅读教程,但这几乎涵盖了它.
确保将 viewController 设置为滚动视图委托并实现:
func viewForZooming(in scrollView: UIScrollView) -> UIView? {
return imageView
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
50747 次 |
| 最近记录: |