Ios 使UIScrollView可访问其他类
我一直在处理Ios 使UIScrollView可访问其他类,ios,swift,uiscrollview,Ios,Swift,Uiscrollview,我一直在处理UIScrollView我希望它可以被另一个类访问,这样我就可以通过它对它进行更改 定义了viewController的代码,其中scrollView import UIKit public class ViewController: UIViewController , UIScrollViewDelegate { @IBOutlet public weak var scrollView: UIScrollView! override public func viewD
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))
}
}
现在我希望在类主屏幕中可以访问该类中的滚动视图
主屏幕的代码
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
}
}
问题在于如何使用scrollview初始化ViewController。您从故事板或Xib创建了它,但您正在初始化它,而没有脚本。试试这样的
let storyboard = UIStoryboard(name: "ViewController", bundle: nil)
if let vc = storyboard.instantiateViewControllerWithIdentifier("ViewControllerId") as? ViewController{
let scrollview = vc.scrollview
}