预期产出:
现在,我的输出产生:
电流输出:
如何删除圈子并添加按钮组?
import UIKitimport QuartzCorepublic protocol FrostedSIDebarDelegate{ func sIDebar(sIDebar: FrostedSIDebar,willShowOnScreenAnimated animated: Bool) func sIDebar(sIDebar: FrostedSIDebar,dIDShowOnScreenAnimated animated: Bool) func sIDebar(sIDebar: FrostedSIDebar,willdismissFromScreenAnimated animated: Bool) func sIDebar(sIDebar: FrostedSIDebar,dIDdismissFromScreenAnimated animated: Bool) func sIDebar(sIDebar: FrostedSIDebar,dIDTAPItemAtIndex index: Int) func sIDebar(sIDebar: FrostedSIDebar,dIDEnable itemEnabled: Bool,itemAtIndex index: Int)}var sharedSIDebar: FrostedSIDebar?public enum SIDebarItemSelectionStyle{ case None se Single case All}public class FrostedSIDebar: UIVIEwController { public var wIDth: CGfloat = 300.0 /** If the sIDebar should show from the right. */ public var showFromright: Bool = false /** The speed at which the sIDebar is presented/dismissed. */ public var animationDuration: CGfloat = 0.25 /** The size of the sIDebar items. */ public var itemSize: CGSize = CGSize(wIDth: 200.0,height: 200.0) /** The background color of the sIDebar items. */ public var itemBackgroundcolor: UIcolor = UIcolor(white: 1,Alpha: 0.25) /** The wIDth of the ring around selected sIDebar items. */ public var borderWIDth: CGfloat = 2 /** The sIDebar's delegate. */ public var delegate: FrostedSIDebarDelegate? = nil /** A dictionary that holds the actions for each item index. */ public var actionForIndex: [Int : ()->()] = [:] /** The indexes that are selected and have rings around them. */ public var selectedindices: NSMutableIndexSet = NSMutableIndexSet() /** If the sIDebar should be positioned beneath a navigation bar that is on screen. */ public var adjustForNavigationbar: Bool = false /** Returns whether or not the sIDebar is currently being displayed */ public var isCurrentlyOpen: Bool = false /** The selection style for the sIDebar. */ public var selectionStyle: SIDebarItemSelectionStyle = .None{ dIDSet{ if case .All = selectionStyle{ selectedindices = NSMutableIndexSet(indexesInRange: NSRange(location: 0,length: images.count)) } } } //MARK: Private PropertIEs private var contentVIEw: UIScrollVIEw = UIScrollVIEw() private var blurVIEw: UIVisualEffectVIEw = UIVisualEffectVIEw(effect: UIBlurEffect(style: .Dark)) private var dimVIEw: UIVIEw = UIVIEw() private var tapGesture: UITapGestureRecognizer? = nil private var images: [UIImage] = [] private var bordercolors: [UIcolor]? = nil private var itemVIEws: [CalloutItem] = [] //MARK: Public Methods /** Returns an object initialized from data in a given unarchiver. */ required public init?(coder aDecoder: NSCoder) { super.init(coder: aDecoder) } /** Returns a sIDebar initialized with the given data. - Parameter itemImages: The images that will be used for each item. - Parameter colors: The color of rings around each image. - Parameter selectionStyle: The selection style for the sIDebar. - Precondition: `colors` is either `nil` or contains the same number of elements as `itemImages`. */ public init(itemImages: [UIImage],colors: [UIcolor]?,selectionStyle: SIDebarItemSelectionStyle){ contentVIEw.alwaysBounceHorizontal = false contentVIEw.alwaysBounceVertical = true contentVIEw.bounces = true contentVIEw.clipsToBounds = false contentVIEw.showsHorizontalScrollindicator = false contentVIEw.showsverticalScrollindicator = false if let colors = colors{ assert(itemImages.count == colors.count,"If item color are supplIEd,the itemImages and colors arrays must be of the same size.") } self.selectionStyle = selectionStyle bordercolors = colors images = itemImages for (index,image) in images.enumerate(){ let vIEw = CalloutItem(index: index) vIEw.clipsToBounds = true vIEw.imageVIEw.image = image contentVIEw.addSubvIEw(vIEw) itemVIEws += [vIEw] if let bordercolors = bordercolors{ if selectedindices.containsIndex(index){ let color = bordercolors[index] vIEw.layer.bordercolor = color.CGcolor } } else{ vIEw.layer.bordercolor = UIcolor.clearcolor().CGcolor } } super.init(nibname: nil,bundle: nil) } public overrIDe func shouldautorotate() -> Bool { return true } public overrIDe func supportedInterfaceOrIEntations() -> UIInterfaceOrIEntationMask { return UIInterfaceOrIEntationMask.All } public overrIDe func vIEwWillTransitionToSize(size: CGSize,withTransitionCoordinator coordinator: UIVIEwControllerTransitionCoordinator) { super.vIEwWillTransitionToSize(size,withTransitionCoordinator: coordinator) if isVIEwLoaded(){ dismissAnimated(false,completion: nil) } } public overrIDe func loadVIEw() { super.loadVIEw() vIEw.backgroundcolor = UIcolor.clearcolor() vIEw.addSubvIEw(dimVIEw) vIEw.addSubvIEw(blurVIEw) vIEw.addSubvIEw(contentVIEw) tapGesture = UITapGestureRecognizer(target: self,action: #selector(FrostedSIDebar.handleTap(_:))) vIEw.addGestureRecognizer(tapGesture!) } /** Shows the sIDebar in a vIEw controller. - Parameter vIEwController: The vIEw controller in which to show the sIDebar. - Parameter animated: If the sIDebar should be animated. */ public func showInVIEwController(vIEwController: UIVIEwController,animated: Bool){ layoutItems() if let bar = sharedSIDebar{ bar.dismissAnimated(false,completion: nil) } delegate?.sIDebar(self,willShowOnScreenAnimated: animated) sharedSIDebar = self addtoparentVIEwController(vIEwController,callingAppearanceMethods: true) vIEw.frame = vIEwController.vIEw.bounds dimVIEw.backgroundcolor = UIcolor.blackcolor() dimVIEw.Alpha = 0 dimVIEw.frame = vIEw.bounds let parentWIDth = vIEw.bounds.size.wIDth var contentFrame = vIEw.bounds contentFrame.origin.x = showFromright ? parentWIDth : -wIDth contentFrame.size.wIDth = wIDth contentVIEw.frame = contentFrame contentVIEw.contentOffset = CGPoint(x: 0,y: 0) layoutItems() var blurFrame = CGRect(x: showFromright ? vIEw.bounds.size.wIDth : 0,y: 0,wIDth: 0,height: vIEw.bounds.size.height) blurVIEw.frame = blurFrame blurVIEw.contentMode = showFromright ? UIVIEwContentMode.topRight : UIVIEwContentMode.topleft blurVIEw.clipsToBounds = true vIEw.insertSubvIEw(blurVIEw,belowSubvIEw: contentVIEw) contentFrame.origin.x = showFromright ? parentWIDth - wIDth : 0 blurFrame.origin.x = contentFrame.origin.x blurFrame.size.wIDth = wIDth let animations: () -> () = { self.contentVIEw.frame = contentFrame self.blurVIEw.frame = blurFrame self.dimVIEw.Alpha = 0.25 } let completion: (Bool) -> VoID = { finished in if finished{ self.delegate?.sIDebar(self,dIDShowOnScreenAnimated: animated) } } if animated{ UIVIEw.animateWithDuration(NSTimeInterval(animationDuration),delay: 0,options: UIVIEwAnimationoptions(),animations: animations,completion: completion) } else{ animations() completion(true) } for (index,item) in itemVIEws.enumerate(){ item.layer.transform = CAtransform3DMakeScale(0.3,0.3,1) item.Alpha = 0 item.originalBackgroundcolor = itemBackgroundcolor item.layer.borderWIDth = borderWIDth animateSpringWithVIEw(item,IDx: index,initDelay: animationDuration) } self.isCurrentlyOpen = true } /** dismisses the sIDebar. - Parameter animated: If the sIDebar should be animated. - Parameter completion: Completion handler called when the sIDebar is dismissed. */ public func dismissAnimated(animated: Bool,completion: ((Bool) -> VoID)?){ let completionBlock: (Bool) -> VoID = {finished in self.removeFromParentVIEwControllerCallingAppearanceMethods(true) self.delegate?.sIDebar(self,dIDdismissFromScreenAnimated: true) self.layoutItems() if let completion = completion{ completion(finished) } } delegate?.sIDebar(self,willdismissFromScreenAnimated: animated) if animated{ let parentWIDth = vIEw.bounds.size.wIDth var contentFrame = contentVIEw.frame contentFrame.origin.x = showFromright ? parentWIDth : -wIDth var blurFrame = blurVIEw.frame blurFrame.origin.x = showFromright ? parentWIDth : 0 blurFrame.size.wIDth = 0 UIVIEw.animateWithDuration(NSTimeInterval(animationDuration),options: UIVIEwAnimationoptions.BeginFromCurrentState,animations: { self.contentVIEw.frame = contentFrame self.blurVIEw.frame = blurFrame self.dimVIEw.Alpha = 0 },completion: completionBlock) } else{ completionBlock(true) } self.isCurrentlyOpen = false } /** Selects the item at the given index. - Parameter index: The index of the item to select. */ public func selectItemAtIndex(index: Int){ let dIDEnable = !selectedindices.containsIndex(index) if let bordercolors = bordercolors{ let stroke = bordercolors[index] let item = itemVIEws[index] if dIDEnable{ if case .Single = selectionStyle{ selectedindices.removeAllindexes() for item in itemVIEws{ item.layer.bordercolor = UIcolor.clearcolor().CGcolor } } item.layer.bordercolor = stroke.CGcolor let borderAnimation = CABasicAnimation(keyPath: "bordercolor") borderAnimation.fromValue = UIcolor.clearcolor().CGcolor borderAnimation.tovalue = stroke.CGcolor borderAnimation.duration = 0.5 item.layer.addAnimation(borderAnimation,forKey: nil) selectedindices.addindex(index) } else{ if case .None = selectionStyle{ item.layer.bordercolor = UIcolor.clearcolor().CGcolor selectedindices.removeIndex(index) } } let pathFrame = CGRect(x: -CGRectGetMIDX(item.bounds),y: -CGRectGetMIDY(item.bounds),wIDth: item.bounds.size.wIDth,height: item.bounds.size.height) let path = UIBezIErPath(roundedRect: pathFrame,cornerRadius: item.layer.cornerRadius) let shapeposition = vIEw.convertPoint(item.center,fromVIEw: contentVIEw) let circleShape = CAShapeLayer() circleShape.path = path.CGPath circleShape.position = shapeposition circleShape.fillcolor = UIcolor.clearcolor().CGcolor circleShape.opacity = 0 circleShape.strokecolor = stroke.CGcolor circleShape.linewidth = borderWIDth vIEw.layer.addSublayer(circleShape) let scaleAnimation = CABasicAnimation(keyPath: "transform.scale") scaleAnimation.fromValue = NSValue(CAtransform3D: CAtransform3DIDentity) scaleAnimation.tovalue = NSValue(CAtransform3D: CAtransform3DMakeScale(2.5,2.5,1)) let AlphaAnimation = CABasicAnimation(keyPath: "opacity") AlphaAnimation.fromValue = 1 AlphaAnimation.tovalue = 0 let animation = CAAnimationGroup() animation.animations = [scaleAnimation,AlphaAnimation] animation.duration = 0.5 animation.timingFunction = camediatimingFunction(name: kcamediatimingFunctionEaSEOut) circleShape.addAnimation(animation,forKey: nil) } if let action = actionForIndex[index]{ action() } delegate?.sIDebar(self,dIDTAPItemAtIndex: index) delegate?.sIDebar(self,dIDEnable: dIDEnable,itemAtIndex: index) } //MARK: Private Classes private class CalloutItem: UIVIEw{ var imageVIEw: UIImageVIEw = UIImageVIEw() var itemIndex: Int var originalBackgroundcolor:UIcolor? { dIDSet{ backgroundcolor = originalBackgroundcolor } } required init?(coder aDecoder: NSCoder) { itemIndex = 0 super.init(coder: aDecoder) } init(index: Int){ imageVIEw.backgroundcolor = UIcolor.clearcolor() imageVIEw.contentMode = UIVIEwContentMode.ScaleAspectFit itemIndex = index super.init(frame: CGRect.zero) addSubvIEw(imageVIEw) } overrIDe func layoutSubvIEws() { super.layoutSubvIEws() let inset: CGfloat = bounds.size.height/2 imageVIEw.frame = CGRect(x: 0,wIDth: inset,height: inset) imageVIEw.center = CGPoint(x: inset,y: inset) } overrIDe func touchesBegan(touches: Set<UItouch>,withEvent event: UIEvent?) { super.touchesBegan(touches,withEvent: event) var r: CGfloat = 0,g: CGfloat = 0,b: CGfloat = 0,a: CGfloat = 0 let darkenFactor: CGfloat = 0.3 var darkercolor: UIcolor if originalBackgroundcolor != nil && originalBackgroundcolor!.getRed(&r,green: &g,blue: &b,Alpha: &a){ darkercolor = UIcolor(red: max(r - darkenFactor,0),green: max(g - darkenFactor,blue: max(b - darkenFactor,Alpha: a) } else if originalBackgroundcolor != nil && originalBackgroundcolor!.getWhite(&r,Alpha: &a){ darkercolor = UIcolor(white: max(r - darkenFactor,Alpha: a) } else{ darkercolor = UIcolor.clearcolor() assert(false,"Item color should be RBG of White/Alpha in order to darken the button") } backgroundcolor = darkercolor } overrIDe func touchesEnded(touches: Set<UItouch>,withEvent event: UIEvent?) { super.touchesEnded(touches,withEvent: event) backgroundcolor = originalBackgroundcolor } overrIDe func touchesCancelled(touches: Set<UItouch>?,withEvent event: UIEvent?) { super.touchesCancelled(touches,withEvent: event) backgroundcolor = originalBackgroundcolor } } //MARK: Private Methods private func animateSpringWithVIEw(vIEw: CalloutItem,IDx: Int,initDelay: CGfloat){ let delay: NSTimeInterval = NSTimeInterval(initDelay) + NSTimeInterval(IDx) * 0.1 UIVIEw.animateWithDuration(0.5,delay: delay,usingSpringWithdamPing: 10.0,initialSpringVeLocity: 50.0,animations: { vIEw.layer.transform = CAtransform3DIDentity vIEw.Alpha = 1 },completion: nil) } @objc private func handleTap(recognizer: UITapGestureRecognizer){ let location = recognizer.locationInVIEw(vIEw) if !CGRectContainsPoint(contentVIEw.frame,location){ dismissAnimated(true,completion: nil) } else{ let tAPIndex = indexOfTap(recognizer.locationInVIEw(contentVIEw)) if let tAPIndex = tAPIndex{ selectItemAtIndex(tAPIndex) } } } private func layoutSubvIEws(){ let x = showFromright ? parentVIEwController!.vIEw.bounds.size.wIDth - wIDth : 0 contentVIEw.frame = CGRect(x: x,wIDth: wIDth,height: parentVIEwController!.vIEw.bounds.size.height) blurVIEw.frame = contentVIEw.frame layoutItems() } private func layoutItems(){ let leftpadding: CGfloat = (wIDth - itemSize.wIDth) / 2 let toppadding: CGfloat = leftpadding for (index,item) in itemVIEws.enumerate(){ let IDx: CGfloat = adjustForNavigationbar ? CGfloat(index) + 0.5 : CGfloat(index) let frame = CGRect(x: leftpadding,y: toppadding*IDx + itemSize.height*IDx + toppadding,wIDth:itemSize.wIDth,height: itemSize.height) item.frame = frame item.layer.cornerRadius = frame.size.wIDth / 2 item.layer.bordercolor = UIcolor.clearcolor().CGcolor item.Alpha = 0 if selectedindices.containsIndex(index){ if let bordercolors = bordercolors{ item.layer.bordercolor = bordercolors[index].CGcolor } } } let itemCount = CGfloat(itemVIEws.count) if adjustForNavigationbar{ contentVIEw.contentSize = CGSizeMake(0,(itemCount + 0.5) * (itemSize.height + toppadding) + toppadding) } else { contentVIEw.contentSize = CGSizeMake(0,itemCount * (itemSize.height + toppadding) + toppadding) } } private func indexOfTap(location: CGPoint) -> Int? { var index: Int? for (IDx,item) in itemVIEws.enumerate(){ if CGRectContainsPoint(item.frame,location){ index = IDx break } } return index } private func addtoparentVIEwController(vIEwController: UIVIEwController,callingAppearanceMethods: Bool){ if let _ = parentVIEwController{ removeFromParentVIEwControllerCallingAppearanceMethods(callingAppearanceMethods) } if callingAppearanceMethods{ beginAppearanceTransition(true,animated: false) } vIEwController.addChildVIEwController(self) vIEwController.vIEw.addSubvIEw(vIEw) dIDMovetoParentVIEwController(self) if callingAppearanceMethods{ endAppearanceTransition() } } private func removeFromParentVIEwControllerCallingAppearanceMethods(callAppearanceMethods: Bool){ if callAppearanceMethods{ beginAppearanceTransition(false,animated: false) } willMovetoParentVIEwController(nil) vIEw.removeFromSupervIEw() removeFromParentVIEwController() if callAppearanceMethods{ endAppearanceTransition() } }}解决方法 您需要创建一个包含3个按钮的视图,然后将圆圈添加到视图中,而不是将每个按钮放在其自己的视图中. 总结
以上是内存溢出为你收集整理的Swift iPad的侧面板全部内容,希望文章能够帮你解决Swift iPad的侧面板所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)