Swift UIPageViewController & UIActivityIndicatorView
Posted
技术标签:
【中文标题】Swift UIPageViewController & UIActivityIndicatorView【英文标题】:Swift UIPageViewController & UIActivityIndicatorView 【发布时间】:2015-05-12 06:44:53 【问题描述】:是否有人知道停止 UIActivityIndicatorView (AI)/删除已添加到 UIPageViewController 的 UIView 的问题?
我正在使用以下方法展示 AI:
override func viewWillLayoutSubviews()
actInd.frame = CGRectMake(0,0, 80, 80)
actInd.center.x = self.view.center.x
actInd.center.y = self.view.center.y - 40
actInd.hidesWhenStopped = true
actInd.layer.cornerRadius = 5
actInd.alpha = 0.5
actInd.backgroundColor = UIColor.blackColor()
actInd.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.WhiteLarge
self.view.addSubview(actInd)
actInd.startAnimating()
// This next line prints out the details of each subview on the page for testing purposes
listSubviews(self.view)
出于测试目的,我还使用以下方法将空白 UIView 添加到视图中:
aView.frame = CGRectMake(0, 0, 200, 200)
aView.backgroundColor = UIColor.blueColor()
self.view.addSubview(aView)
然后我使用委托发送我的 Parse.com 查询,以便在数据准备好时通知 UIPageViewController(例如此视图)。这工作正常,我已经测试了数据是否正确返回,并且正在调用预期的方法。
在这种方法中,我尝试停止 UIActivityViewController 并尝试隐藏 aView:
self.actInd.stopAnimating()
aView.removeFromSuperView()
这不起作用,所以我尝试了:
self.actInd.removeFromSuperView()
这也不起作用。所以我然后尝试使用以下方法搜索当前视图的子视图:
func populateBoards(boards: [Board])
println(self.actInd) // PRINT LINE 1
var subviews = self.view.subviews
for v in subviews
if v.isKindOfClass(UIActivityIndicatorView)
println("YES, it is an activity view indicator \(v)") // PRINT LINE 2
v.stopAnimating()
v.removeFromSuperview()
self.boards = boards
println("Populated Boards!") // PRINT LINE 3
打印第 1 行输出:>
PRINT LINE 2 输出:YES,它是一个活动视图指示器 >
PRINT LINE 3 输出:“Populated board!”
编辑:
使用以下代码设置 UIPageViewController(以防万一):
func setupUIPageView()
self.pageViewController = UIPageViewController(transitionStyle: UIPageViewControllerTransitionStyle.Scroll, navigationOrientation: UIPageViewControllerNavigationOrientation.Horizontal, options: nil)
self.pageViewController.delegate = self
self.pageViewController.dataSource = self
var startVC = self.viewControllerAtIndex(0) as ViewController
var viewControllers:[ViewController] = [startVC]
self.pageViewController.setViewControllers(viewControllers, direction: .Forward, animated: true, completion: nil)
self.pageViewController.view.frame = CGRectMake(0, 0, self.view.frame.width, self.view.frame.height)
self.pageViewController.view.alpha = 0.0
self.addChildViewController(self.pageViewController)
self.view.addSubview(self.pageViewController.view)
self.pageViewController.didMoveToParentViewController(self)
帮助!提前谢谢大家:D
【问题讨论】:
【参考方案1】:您应该在主队列中调用所有与 UI 相关的代码。您的 Parse 查询是异步的,可能会在不同的线程上调用完成块。
将此调用包装在此块中:
NSOperationQueue.mainQueue().addOperationWithBlock
self.actInd.stopAnimating()
【讨论】:
完美,谢谢。我什至没有意识到这是可能的:D 您可能对此课程感兴趣:github.com/goktugyil/CozyLoadingActivity以上是关于Swift UIPageViewController & UIActivityIndicatorView的主要内容,如果未能解决你的问题,请参考以下文章
swift [Swift Notes]在学习Swift #Swift的同时收集笔记