我有一个 UIScrollView 连接了 2 个 View Controller 。像这样
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.scrollVieww.addSubview(V1.view)
V1.didMoveToParentViewController(self)
V1.view.frame = scrollVieww.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.scrollVieww.addSubview(V2.view)
V2.didMoveToParentViewController(self)
V2.view.frame = scrollVieww.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.scrollVieww.contentSize = CGSizeMake((self.view.frame.width) * 2, (self.view.frame.height))
现在我可以使用此类中编写的 ScrollView 委托(delegate)方法。
所以现在我进入了我的类 Main_Screen 并使用 UIScrollViewDelegate 对其进行子类化。
Main_Screen 类的代码
import UIKit
public class Main_Screen: UIViewController , UIScrollViewDelegate {
@IBOutlet weak var aboutMebtn: UIButton!
@IBOutlet weak var studybtn: UIButton!
@IBOutlet weak var appsbtn: UIButton!
@IBOutlet weak var skillsbtn: UIButton!
var scrollView : UIScrollView!
@IBOutlet var avatarImageView: UIImageView!
weak var pageControl: UIPageControl!
var mainRead : Bool = false
override public func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
// Making the view round
for v : UIView in self.view.subviews {
if v.tag != 1 {
v.layer.cornerRadius = v.frame.size.width/2
v.layer.masksToBounds = true
}
}
}
public func animate(){
// I get a fatal error here
var buttons : Array<UIButton> = [aboutMebtn , studybtn , appsbtn , skillsbtn]
avatarImageView.alpha = 0.0
avatarImageView.transform = CGAffineTransformMakeScale(1.25, 1.25)
UIView.animateWithDuration(1.0, delay: 1.0, usingSpringWithDamping: 1.0, initialSpringVelocity: 0.0, options: .CurveEaseIn, animations: { () -> Void in
self.avatarImageView.alpha = 1.0
self.avatarImageView.transform = CGAffineTransformMakeScale(0.75, 0.75)
}, completion: nil)
}
public func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
if(scrollView.contentOffset.x == self.view.frame.size.width * 1 ){
print("Main is read")
}
}
但是 UIScrollView Delegate 的方法不会执行。我可以在 ViewController 中执行此操作,但我不能,因为我想在 scrollViewDidEndDecleating
方法上运行上面的动画函数
最佳答案
您必须将 View Controller 指定为 UIScrollView 委托(delegate)
self.scrollVieww.delegate = self;
你可能会错过它。 你可以把它放在viewDidLoad中
关于ios - 访问另一个类中的 ScrollView 委托(delegate)方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36340814/