2016-04-23 88 views
3

當我在滾動視圖中旋轉視圖時,它將移出滾動視圖並在一些旋轉/縮放手勢後完全消失。只要縮放比例爲1,它就可以正常工作。 與我的代碼有什麼關係可以避免這種情況?在UIScrollview中旋轉UIView Swift

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

的解決方案是增加了滾動下一個額外的UIView,因此新holderView是滾動視圖和rotatonView的holderView的子視圖的子視圖。

+0

謝謝,這節省了我一些時間! – cleverbit