使UIScrollView可以访问其他类

时间:2016-03-30 20:35:20

标签: ios swift uiscrollview

我一直在处理UIScrollView我希望它可以被其他类访问,所以我可以通过它进行更改。

viewController的代码scrollView已定义

import UIKit

public class ViewController: UIViewController , UIScrollViewDelegate {
  @IBOutlet public weak var scrollView: UIScrollView!

  override public func viewDidLoad() {
    super.viewDidLoad()

    self.scrollView.pagingEnabled = true
    self.scrollView.delegate = self
    // Do any additional setup after loading the view, typically from a nib.
    let V1 = self.storyboard?.instantiateViewControllerWithIdentifier("HomeScreen") as UIViewController!
    //Add initialized view to main view and its scroll view and also set bounds
    self.addChildViewController(V1)
    self.scrollView.addSubview(V1.view)
    V1.didMoveToParentViewController(self)
    V1.view.frame = scrollView.bounds

    //Initialize using Unique ID for the View
    let V2 = self.storyboard?.instantiateViewControllerWithIdentifier("MainScreen") as UIViewController!
    //Add initialized view to main view and its scroll view also set bounds
    self.addChildViewController(V2)
    self.scrollView.addSubview(V2.view)
    V2.didMoveToParentViewController(self)
    V2.view.frame = scrollView.bounds

    //Create frame for the view and define its urigin point with respect to View 1
    var V2Frame: CGRect = V2.view.frame
    V2Frame.origin.x = self.view.frame.width
    V2.view.frame = V2Frame

    //The width is set here as we are dealing with Horizontal Scroll
    //The Width is x3 as there are 3 sub views in all
    self.scrollView.contentSize = CGSizeMake((self.view.frame.width) * 2, (self.view.frame.height))    
  }
}

现在我想在课程scrollView

中访问此课程中的Home_Screen

Home_Screen的代码

import UIKit

class Home_Screen: UIViewController , UIScrollViewDelegate {

  @IBOutlet weak var nameLabel: UILabel!
  @IBOutlet weak var slideToUnlockLabel: UILabel!
  @IBOutlet weak var imageView: UIImageView!
  @IBOutlet weak var shimmeringView : FBShimmeringView!

  override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.

    let viewController = ViewController()
    let scrollView = viewController.scrollView

    // Add The ShimmeringView
    shimmeringView.contentView = slideToUnlockLabel
    shimmeringView.shimmering = true
    shimmeringView.shimmeringBeginFadeDuration = 0.5
    shimmeringView.shimmeringOpacity = 0.45
    shimmeringView.shimmeringAnimationOpacity = 1.5  
  }
}

1 个答案:

答案 0 :(得分:0)

问题是如何使用scrollview初始化ViewController。您是从故事板或Xib创建的,但是在没有它的情况下进行初始化。试试这样的事情

let storyboard = UIStoryboard(name: "ViewController", bundle: nil)
if let vc = storyboard.instantiateViewControllerWithIdentifier("ViewControllerId") as? ViewController{
        let scrollview = vc.scrollview
    }