2016-08-21 8 views
0

このトピックはかなりの箇所で表示されていますが、なぜ私のコードが正しく機能しないのかわかりません。UIScrollView内のUIImageViewの整列

私は、-90度から90度の人工的な地平線のイメージを持っています。私はちょうど約-20から20度を示す小さな窓を通してそれを見たいと思う。それから私はロボットが傾いている角度に基づいて画像を上下に動かしたいと思う。

私はUIImageViewにUIImageを追加することから始めました。すべてのアライメントが正しい。次に、画像を上下に移動する最も簡単な方法は、UIImageViewをUIScrollViewに追加することだと思いました。今私はどのように整列権を得るかを理解することができません。スクロールビューでドラッグするとそこに画像が表示されますが、取り消すとすぐに元の場所に戻ります。

ここに私のコードです。これを行うには良い方法があるかどうか、私は任意の嘲笑(ちょうど穏やかな、冗談を)歓迎ので、これはあなたが実際の画像サイズにscrollViewのcontentSizeのないように設定する必要が

override func viewDidLoad() { 
    super.viewDidLoad() 

    let imageRect = CGRectMake(self.view.frame.width/2 - 100, self.view.frame.height/2, 200, 200) 

    self.myImageView = UIImageView.init() 
    self.myImageView = UIImageView(frame: imageRect) 
    self.myImageView.contentMode = UIViewContentMode.Center 
    self.myImageView.clipsToBounds = true 
    self.myImageView.image = UIImage.init(named:"horizon") 
    //self.view.addSubview(self.image) 


    self.myScrollView = UIScrollView(frame: imageRect) 
    self.myScrollView.contentSize = CGSize(width: imageRect.width, height: imageRect.height) 

    self.myImageView.center = self.myScrollView.center 
    self.myScrollView.frame = imageRect 
    self.myScrollView.backgroundColor = UIColor.blackColor() 
    self.myScrollView.contentOffset = CGPoint(x: 0, y: 0) 
    self.myScrollView.addSubview(self.myImageView) 
    self.view.addSubview(self.myScrollView) 

    ///////// 


    self.connectionStatusLabel.text = "Disconnected" 
    self.connectionStatusLabel.textColor = UIColor.redColor() 

    self.textBox.font = UIFont(name: self.textBox.font!.fontName, size: 8) 

    // Watch Bluetooth connection 
    NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(ViewController.connectionChanged(_:)), name: BLEServiceChangedStatusNotification, object: nil) 


    NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(ViewController.dataReceived(_:)), name: BLEDataChangedStatusNotification, object: nil) 


    // Start the Bluetooth discovery process 
    btDiscoverySharedInstance 
} 

答えて

1

私が書いた最初のスウィフトコードですimageViewのサイズまた、imageViewのサイズを実際の画像サイズにも設定します。

let image = UIImage.init(named:"horizon") 
// Set the imageView's size to the actual image size 
self.myImageView.frame = CGRect(x: 0, y: 0, width: size.width, height: size.height) 
... 
... 
// Set the scrollView's contentSize to the actual image size 
self.myScrollView.contentSize = image.size 
関連する問題