2016-08-22 5 views
1

私はGithubで見つけたParallaxテーブルビューのサブクラスであるtableViewを持っています。すべてのテーブルビューを正常に設定しました(無関係なので削除しました)。viewDidLayoutSubviewsでUITableViewスクロールを検出すると、過剰なコールバックが発生します

class EventDetailTableViewController: ParallaxTableViewController { 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     // Set the image: 
     self.imageView.image = eventImage 

     self.navigationController?.navigationBar.setBackgroundImage(UIImage(), forBarMetrics: .Default) 
     self.navigationController?.navigationBar.shadowImage = UIImage() 
     self.navigationController?.navigationBar.translucent = true 
    } 


    // MARK: - UITableViewDelegate 
    override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { 
     return UITableViewAutomaticDimension 
    } 

    override  
    func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { 
     return UITableViewAutomaticDimension 
    } 

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
     return 4 
    } 

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return 1 
    } 

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
     // SET UP CELL 
    } 

    @IBAction func backButtonPressed(sender: AnyObject) { 
     print("backButtonPressed") 
     self.navigationController?.navigationBar.translucent = false 

     self.navigationController?.navigationBar.barTintColor = UIColor(red: 227/255, green: 38/255, blue: 54/255, alpha: 1.0) 

     self.navigationController?.popViewControllerAnimated(true) 
    } 
} // END OF VIEWCONTROLLER CLASS 

ここで重要なことは、私がnavigationBarを透明にしたことです。 backButtonPressedが呼び出されたら、navigationBar.Colorを前のView Controller(赤色)の色に戻したいと思います。だから私はそれをコード化したbackButtonPressed()

ここに私が持っている視差のコードです。最後のfunc moveImage()をご覧ください。あなたは私のノートに見ることができるように私navigationBarタイトル「イベントの説明」と表示される説明に十分ダウン時にテーブルビューのスクロールの簡単な効果

class ParallaxTableViewController: UITableViewController { 

    // Create the UIView 
    //var bottomFloatingView = UIView() 

    // Create the UIImageView 
    let imageView = UIImageView() 

    // Set the factor for the parallaxEffect. This is overwritable. 
    var parallaxFactor:CGFloat = 2 

    // Set the default height for the image on the top. 
    var imageHeight:CGFloat = 320 { 
     didSet { 
      moveImage() 
     } 
    } 

    // Initialize the scrollOffset varaible. 
    var scrollOffset:CGFloat = 0 { 
     didSet { 
      moveImage() 
     } 
    } 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     // Set the contentInset to make room for the image. 
     self.tableView.contentInset = UIEdgeInsets(top: imageHeight, left: 0, bottom: 0, right: 0) 

     // Change the contentMode for the ImageView. 
     imageView.contentMode = UIViewContentMode.ScaleAspectFill 

     // Add the imageView to the TableView and send it to the back. 
     view.addSubview(imageView) 
     view.sendSubviewToBack(imageView) 
    } 

    override func viewDidLayoutSubviews() { 
     // Update the image position after layout changes. 
     moveImage() 
    } 

    // Define method for image location changes. 
    func moveImage() { 
     let imageOffset = (scrollOffset >= 0) ? scrollOffset/parallaxFactor : 0 
     let imageHeight = (scrollOffset >= 0) ? self.imageHeight : self.imageHeight - scrollOffset 
     imageView.frame = CGRect(x: 0, y: -imageHeight + imageOffset, width: view.bounds.width, height: imageHeight) 

     if imageOffset > 150 { 
      print("imageOffSet") 
      self.navigationItem.title = "Event Description" 
      self.navigationController?.navigationBar.translucent = false 
      self.navigationController?.navigationBar.barTintColor = UIColor.whiteColor() 
      self.navigationController?.navigationBar.shadowImage = nil 

     } else { 
      print("else") 
      self.navigationItem.title = "" 
      self.navigationController?.navigationBar.shadowImage = UIImage() 
      self.navigationController?.navigationBar.translucent = true 

      // This is being called after the back button is being pressed. Which means this else function overrides my backButtonPressed() therefore making my navigation bar in the new VC not the correct color. 
     } 
    } 
} 

を持って、この他の機能は、後に再び呼び出されていますbackButtonPressed()が呼び出されます。したがって、それは新しいVCで私に望ましい赤を与えませんが、代わりに半透明にします。 backButtonPressedが呼び出されているときにこの他の関数が呼び出されるのを止める方法はありますか?

+0

それ以外の場合は 'imageOffset <150'が呼び出されます。おそらくあなたの数学をチェックし、そこに何が壊れているのかを調べる必要があります。 –

答えて

2

viewDidLayoutSubviewsは、ユーザーイベントへの応答に決して関与してはなりません。これは、ストーリーボードにすでに追加されているプログラムにプログラムの追加または修正を行わない限り、まれに使用される内部のライフサイクルメソッドです。NSLayoutConstraintsこれは、かなり頻繁に見える冗長性と呼ばれます。

代わりにテーブルデリゲートメソッドを使用します。特にUIScrollViewDelegateそう基準からUITableViewDelegate継承:

UITableView Scroll event

そして例えば可視情報のtableView自体を問い合わせますindexPathsForVisibleRows:BaseZenは彼の答えで説明したように

https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITableView_Class/#//apple_ref/occ/instp/UITableView/indexPathsForVisibleRows

1

あなたはこのようviewDidLayoutSubviews()を使用しないでください。 scrollViewDidScrollを使用して、あなたが望むものを達成することができます。

この

var backButtonWasPressed = false 

@IBAction func backPressed(sender: AnyObject) { 

    print("backButtonPressed") 

    backButtonWasPressed = true 

    self.navigationController?.navigationBar.translucent = false 

    self.navigationController?.navigationBar.barTintColor = UIColor(red: 227/255, green: 38/255, blue: 54/255, alpha: 1.0) 

    self.navigationController?.popViewControllerAnimated(true) 

} 

でこの

@IBAction func backButtonPressed(sender: AnyObject) { 
    print("backButtonPressed") 
    self.navigationController?.navigationBar.translucent = false 

    self.navigationController?.navigationBar.barTintColor = UIColor(red: 227/255, green: 38/255, blue: 54/255, alpha: 1.0) 

    self.navigationController?.popViewControllerAnimated(true) 
} 

を交換し、この

override func scrollViewDidScroll(scrollView: UIScrollView) { 
    if !backButtonWasPressed { 
     moveImage() 
    } 
} 
でこの

override func viewDidLayoutSubviews() { 

// Update the image position after layout changes. 
moveImage() 
} 

を置き換えます

そして、あなたのviewDidLoad()にbackButtonWasPressed = falseを入れてください。

+0

あなたの言うことが意味をなさないように見えますが、私はこれを変更しました。まだ他の人に電話しているようです... – MarkPitch

+0

私はそれをテストしました。また、戻るボタンを使うと 'scrollViewDidScroll'も起動するようです。この問題を解決するために私の答えを編集します。 –

+0

@MarkPitch私の編集した答えを見てください。今はうまくいくはずです。 –

関連する問題