2016-04-23 19 views
3

スクロールビューでビューを回転すると、スクロールビューから移動し、回転/ズームジェスチャの後に完全に消えます。ズームスケールが1であれば正常に動作します。 これを避けるにはコードと何が関係がありますか?UIScrollview SwiftでUIViewを回転する

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とrotatonView holderViewのサブビューのサブビューです。

+0

ありがとうございました。 – cleverbit

関連する問題