在 UIScrollview Swift 中旋转 UIView
Posted
技术标签:
【中文标题】在 UIScrollview Swift 中旋转 UIView【英文标题】:Rotating UIView in UIScrollview Swift 【发布时间】:2016-04-23 12:15:02 【问题描述】:当我在滚动视图中旋转视图时,它会移出滚动视图并在一些旋转/缩放手势后完全消失。只要缩放比例为 1,它就可以正常工作。 为了避免这种情况,我应该如何处理我的代码?
import UIKit
class ViewController: UIViewController, UIScrollViewDelegate
let scrollView = UIScrollView(frame: UIScreen.mainScreen().bounds)
let rotationView = UIView()
override func viewDidLoad()
super.viewDidLoad()
let imageView = UIImageView()
view.addSubview(scrollView)
scrollView.delegate = self
scrollView.minimumZoomScale = 0.5
scrollView.maximumZoomScale = 2
let mapImage = UIImage(named: "BMS2_300.jpg")
imageView.frame = CGRect(origin: CGPoint(x: 0, y: 0), size:mapImage!.size)
imageView.image = mapImage
let rotationViewframe = CGRectMake(0, 0, imageView.frame.size.width, imageView.frame.size.height)
rotationView.frame = rotationViewframe
rotationView.addSubview(imageView)
//rotationView.sizeToFit()
scrollView.addSubview(rotationView)
scrollView.contentSize = CGSize(width: rotationView.bounds.width, height: rotationView.bounds.height)
scrollView.bringSubviewToFront(rotationView)
scrollView.contentOffset = CGPoint(x: rotationView.frame.width/2, y: rotationView.frame.height/2)
let mapRotGestureRecognizer = UIRotationGestureRecognizer(target: self, action: #selector(ViewController.rotateMap(_:)))
rotationView.addGestureRecognizer(mapRotGestureRecognizer)
func rotateMap(sender: UIRotationGestureRecognizer)
let radians = sender.rotation
if let senderView = sender.view
senderView.transform = CGAffineTransformRotate(senderView.transform, radians)
sender.rotation = 0
func viewForZoomingInScrollView(scrollView: UIScrollView) -> UIView?
return self.rotationView
func scrollViewDidEndZooming(scrollView: UIScrollView, withView view: UIView?, atScale scale: CGFloat)
scrollView.contentSize = rotationView.frame.size
【问题讨论】:
【参考方案1】:解决办法是在scrollview下多加一个uiview,所以新的holderView是scrollview的子view,rotatonView是holderView的子view。
【讨论】:
谢谢,这为我节省了一些时间!!以上是关于在 UIScrollview Swift 中旋转 UIView的主要内容,如果未能解决你的问题,请参考以下文章
在 UIScrollView 中旋转 UIViewControllers
如何在 UIScrollview 中触发 UIViewcontroller 视图的旋转事件