我开始学习Swift,并决定构建一个没有故事板的应用程序。
我的SceneDelegate scene
函数实例化一个TabBarController
window = UIWindow(frame: UIScreen.main.bounds)
let tb = TabBarController()
window?.rootViewController = tb
window?.makeKeyAndVisible()
window?.windowScene = windowSceme
我有一个从UITabBarController扩展的TabBarController,它几乎对标签栏进行样式设置并将其全部设置
对于标签栏的每个项目,我都有一个ViewController。出于这个问题的目的,我将指定第一个“主页”。
在扩展ViewController的Home中,我有以下内容
let homePageView = HomePageView()
override func viewWillLayoutSubviews() {
navigationController?.isnavigationBarHidden = true
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(true)
homePageView.setupHomePage()
view.addSubview(homePageView)
}
override func viewWillDisappear(_ animated: Bool) {
homePageView.dispose()
}
控制器几乎只负责调用HomePageView()
(主要组件)中屏幕上将要显示的内容。最后,还有另外两个视图。一个转盘HomePageCarrousel()
和一个标头HomePageSocialUp()
。该视图将实例化后两个实例,并以编程方式设置布局并将其添加为子视图。它还包括一个dispose函数,该函数将实例化的类设置为nil,例如-> Instantiate-homePageCarrousel = HomePageCarrousel()
,而dispose函数具有homePageCarrousel = nil
那很好,但是当我通过选项卡栏离开当前视图控制器并导航回去时,现在HomePageCarrousel()
中有两个HomePageSocialUp()
和HomeView
的实例
我可能在某处拥有很强的参考力,但我不知道何时。有人可以指出我应该在哪里调试它,或者是什么造成了问题。
如果出现问题,我还将提供重复的两个视图的代码
HomePageSocialUp
class HomePageSocialUp: UIView {
let logo = UIImage(named: "LogoSmiles")
let socialImages: [UIImage] = [UIImage(named: "tripadvisor")!,UIImage(named: "instagram")!,UIImage(named: "facebook")!,UIImage(named: "linkedin")!]
func setupHeaderCircle() {
guard let insetTop = UIApplication.shared.keywindow?.safeAreaInsets.top else {return}
let circlePath = UIBezierPath(arcCenter: CGPoint(x: UIScreen.main.bounds.width / 2,y: insetTop + 60),radius: CGFloat(90),startAngle: CGFloat(0),endAngle: CGFloat(Double.pi * 2),clockwise: true)
let shapelayer = CAShapelayer()
shapelayer.path = circlePath.cgPath
shapelayer.fillColor = UIColor.white.cgColor
layer.addSublayer(shapelayer)
}
func setupSocialHeader() {
setupHeaderCircle()
layer.masksToBounds = true;
backgroundColor = UIColor.TintColor
layer.shadowColor = UIColor.lightGray.cgColor
layer.shadowOpacity = 0.8
layer.shadowOffset = CGSize(width: 0.0,height: 0.0)
layer.shadowRadius = 3.0
layer.masksToBounds = false
frame = CGRect(x: 0,y: 0,width: UIScreen.main.bounds.width,height: 280)
let imageView = UIImageView()
guard let logo = logo else {return}
guard let insetTop = UIApplication.shared.keywindow?.safeAreaInsets.top else {return}
imageView.frame = CGRect(x: CGFloat(Int((UIScreen.main.bounds.width - (logo.size.width)))) / 2,y: 120 - (logo.size.width / 2),width: logo.size.width,height: logo.size.height)
imageView.image = logo
addSubview(imageView)
}
}
HomeCarCarrousel
class HomePageCarrousel: UIScrollView {
var images: [UIImage]?
var originX = 0
var numberOfIterations = 0
var timer: Timer?
func setupCarrousel() {
let x = (Int(UIScreen.main.bounds.width) - (Int(UIScreen.main.bounds.width) - 60)) / 2
frame = CGRect(x: x,y: (Int(frame.origin.y) - 350) / 2,width: Int(UIScreen.main.bounds.width) - 60,height: 350)
let newImage = textToImage(drawText: "Creating Smiles in unique places.",frame: frame,inImage: UIImage(named: "smiles1")!,atPoint: CGPoint(x: frame.origin.x + 20,y: frame.height - 20))
images = [newImage,UIImage(named: "smiles2")!]
guard timer == nil else { return }
timer = Timer.scheduledTimer(timeInterval: 2,target: self,selector: #selector(startScrolling),userInfo: nil,repeats: true)
guard let imageCount = images?.count,let images = images else { return }
contentSize = CGSize(width: frame.width * CGFloat(images.count),height: frame.height)
for image in 0..<imageCount {
let imageView = UIImageView()
imageView.image = images[image]
imageView.contentMode = .scaleAspectFill
imageView.clipsToBounds = true
let xPosition = frame.width * CGFloat(image)
imageView.frame = CGRect(x: xPosition,width: frame.width,height: frame.height)
addSubview(imageView)
}
timer?.fire()
}
func textToImage(drawText text: String,frame: CGRect,inImage image: UIImage,atPoint point: CGPoint) -> UIImage {
let textColor = UIColor.white
let textFont = UIFont(name: "Helvetica Bold",size: 12)!
let scale = UIScreen.main.scale
UIGraphicsBeginImageContextWithOptions(image.size,false,scale)
let textFontAttributes = [
NSAttributedString.Key.font: textFont,NSAttributedString.Key.foregroundColor: textColor,] as [NSAttributedString.Key : Any]
image.draw(in: CGRect(origin: CGPoint.zero,size: image.size))
text.draw(in: frame,withAttributes: textFontAttributes)
let newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return newImage!
}
@objc func startScrolling() {
print(originX)
guard let images = images else { return }
if originX == images.count {
originX = 0
numberOfIterations += 1
}
if numberOfIterations > 2 {
timer?.invalidate()
timer = nil
numberOfIterations = 0
}
let x = CGFloat(originX) * frame.size.width
setContentOffset(CGPoint(x: x,y: 0),animated: true)
originX += 1
}
}
非常感谢