2016-08-22 10 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()

여기 제가 가지고있는 시차 코드입니다. 마지막 기능은 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. 
     } 
    } 
} 

에 표시되는 설명에있는 tableview의 스크롤이 다른 기능이 후 다시 호출 될 때의 간단한 효과를 backButtonPressed()이 호출됩니다. 그러므로 새로운 VC에서 원하는 빨강을주지는 않지만 반투명으로 남겨 둡니다. 어떻게하면 backButtonPressed가 호출 될 때이 다른 함수가 호출되는 것을 멈출 수 있습니까?

+0

다른 이미지를 호출하는 경우 'imageOffset <150'이 좋습니다. 아마도 수학을 확인하고 무엇이 깨지는 지 찾아야 할 것입니다. –

답변

2

viewDidLayoutSubviews은 사용자 이벤트에 응답하지 않아야합니다. Storyboard에 이미 추가 된 프로그램에 프로그래밍 방식의 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() 
    } 
} 
,369이

override func viewDidLayoutSubviews() { 

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

교체

그리고을 viewDidLoad()에 넣으십시오.

+0

너희들이 말하는 것을 이해하는 것처럼 보이지만 나는 이것을 바꿨다. 아직도 다른 사람에게 전화를하고있는 것처럼 보입니다. hhhmm ... – MarkPitch

+0

나는 그것을 테스트했으며 뒤로 버튼을 사용하면'scrollViewDidScroll '도 트리거되는 것 같습니다. 이 문제를 해결하기 위해 내 대답을 수정하겠습니다. –

+0

@ MarkPitch 편집 된 답변보기 이제는 효과가있다. –