这是我的自定义页面控件类。我在主故事板上创建了一个UIView,并给它一个类myPageControl。但它出于某种原因崩溃了。我不知道为什么。
import UIKit
class myPageControl: UIControl {
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
let kSmallCircleSize: CGSize = CGSize(width: 15.0, height: 15.0)
let kBigCircleSize: CGSize = CGSize(width: 50.0, height: 50.0)
let kSmallCirclesPadding: CGFloat = 35.0
var numberOfPages: Int = 0
var currentPage: Int = 0
var layers: NSArray = []
func incrementCurrentPage() {
if self.currentPage < (self.numberOfPages - 1) {
++self.currentPage
}
}
func decrementCurrentPage() {
if self.currentPage > 0 {
--self.currentPage
}
}
func setNumberOfPagess(value: Int) {
if numberOfPages != value {
self.layers.forEach { $0.removeFromSuperlayer() }
//self.layers.makeObjectsPerformSelector("removeFromSuperlayer")
self.numberOfPages = value
let numberOfPages: Int = self.numberOfPages
var layers: [AnyObject] = [AnyObject](arrayLiteral: numberOfPages)
var layer: CALayer = self.layer
for var index = 0; index < numberOfPages; ++index {
var shapeLayer: PageControlShapeLayer = PageControlShapeLayer()
shapeLayer.path = UIBezierPath(ovalInRect: CGRectMake(0.0, 0.0, 10.0, 10.0)).CGPath
shapeLayer.fillColor = UIColor.clearColor().CGColor
shapeLayer.strokeColor = UIColor.blueColor().CGColor
layers.append(shapeLayer)
layer.addSublayer(shapeLayer)
}
self.layers = layers
}
}
func setCurrentPages(value: Int) {
if currentPage != value {
self.currentPage = value
self.setNeedsLayout()
}
}
override func layoutSubviews() {
let bounds: CGRect = self.bounds
let midY: CGFloat = CGRectGetMidY(bounds)
let midX: CGFloat = CGRectGetMidX(bounds)
var layers: NSArray = self.layers
let currentPage: Int = self.currentPage
for var index = (currentPage - 1); index >= 0; --index {
var shapeLayer: CAShapeLayer = layers[index] as! CAShapeLayer
var rect: CGRect = CGRectMake((midX - (kBigCircleSize.width / 2.0) - (kSmallCirclesPadding * CGFloat(currentPage - index))), (midY - (kSmallCircleSize.height / 2.0)), kSmallCircleSize.width, kSmallCircleSize.height)
shapeLayer.path = UIBezierPath(ovalInRect: rect).CGPath
}
var shapeLayer: CAShapeLayer = layers[currentPage] as! CAShapeLayer
shapeLayer.path = UIBezierPath(ovalInRect: CGRectMake((midX - (kBigCircleSize.width / 2.0)), (midY - (kBigCircleSize.height / 2.0)), kBigCircleSize.width, kBigCircleSize.height)).CGPath
for var index = (currentPage + 1); index < self.numberOfPages; ++index {
var shapeLayer: CAShapeLayer = layers[index] as! CAShapeLayer
var rectWidth = midX + kBigCircleSize.width / 2.0
var rectWidth2 = kSmallCirclesPadding * CGFloat(index - currentPage) - kSmallCircleSize.width
var rect: CGRect = CGRectMake((rectWidth + rectWidth2), (midY - (kSmallCircleSize.height / 2.0)), kSmallCircleSize.width, kSmallCircleSize.height)
shapeLayer.path = UIBezierPath(ovalInRect: rect).CGPath
}
}
override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
var touch: UITouch = touches.first!
if touch.tapCount == 1 {
let midX: CGFloat = CGRectGetMidX(self.bounds)
if touch.locationInView(self).x < (midX - (kBigCircleSize.width / 2.0)) {
self.decrementCurrentPage()
}
else if touch.locationInView(self).x > (midX + (kBigCircleSize.width / 2.0)) {
self.incrementCurrentPage()
}
}
}
}
当我运行此代码时,它会崩溃并给我以下内容:
2016-01-10 12:38:15.831 PageControl[22444:830608] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArray0 objectAtIndex:]: index 0 beyond bounds for empty NSArray'
*** First throw call stack:
(
0 CoreFoundation 0x0027ea84 __exceptionPreprocess + 180
1 libobjc.A.dylib 0x0235fe02 objc_exception_throw + 50
2 CoreFoundation 0x00225390 __CFArrayGetTypeID_block_invoke + 0
3 CoreFoundation 0x001c37f8 -[NSArray objectAtIndexedSubscript:] + 40
4 PageControl 0x0008f1ae _TFC11PageControl13myPageControl14layoutSubviewsfS0_FT_T_ + 590
5 PageControl 0x0008fa32 _TToFC11PageControl13myPageControl14layoutSubviewsfS0_FT_T_ + 34
6 UIKit 0x00f70eb7 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 813
7 libobjc.A.dylib 0x02374059 -[NSObject performSelector:withObject:] + 70
8 QuartzCore 0x00d7180a -[CALayer layoutSublayers] + 144
9 QuartzCore 0x00d654ee _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 388
10 QuartzCore 0x00d65352 _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 26
11 QuartzCore 0x00d57e8b _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 317
12 QuartzCore 0x00d8be03 _ZN2CA11Transaction6commitEv + 561
13 QuartzCore 0x00d8c6c4 _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 92
14 CoreFoundation 0x0019861e __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 30
15 CoreFoundation 0x0019857e __CFRunLoopDoObservers + 398
16 CoreFoundation 0x0018d728 CFRunLoopRunSpecific + 504
17 CoreFoundation 0x0018d51b CFRunLoopRunInMode + 123
18 UIKit 0x00ea1854 -[UIApplication _run] + 540
19 UIKit 0x00ea71eb UIApplicationMain + 160
20 PageControl 0x00091c6c main + 140
21 libdyld.dylib 0x02dcaa21 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
有谁知道如何解决这个问题,我哪里出错?