gpt4 book ai didi

ios - 以编程方式将两个图像添加到我的标签上方

转载 作者:行者123 更新时间:2023-11-28 06:44:25 28 4
gpt4 key购买 nike

我正在使用自定义库:CAPSPageMenu .这里我有一个 View Controller ,我以编程方式为其添加名称,该 View 有两个选项卡。当我单击任何选项卡时,相应页面将显示在下方。

我已经设置了名称并添加了在两个选项卡之间滑动的动画。但是现在我需要在我的标签上方添加图像,我该如何实现呢?

enter image description here

这是我的代码!

var pageMenu : CAPSPageMenu? // pager object

var controllerArray : [UIViewController] = [] // number view pager
override func viewDidLoad() {

super.viewDidLoad()
pageSettings()

}
func pageSettings() {
tableView.dataSource = self
tableView.delegate = self

let controller1 : UIViewController = UIViewController()
controller1.title = "FAVOURITES"
controllerArray.append(controller1)

let controller2 : UIViewController = UIViewController()
controller2.title = "RECENT SEARCH"
controllerArray.append(controller2)


let leftRecognizer = UISwipeGestureRecognizer(target: self, action: #selector(self.didLeftSwipe(_:)))
leftRecognizer.direction = .Left
self.tableView.addGestureRecognizer(leftRecognizer)

let rightRecognizer = UISwipeGestureRecognizer(target: self, action: #selector(self.didRightSwipe(_:)))
rightRecognizer.direction = .Right
self.tableView.addGestureRecognizer(rightRecognizer)
}

像这样我需要添加图像。正如我提到的两个标签上方的框

OUTPUT

最佳答案

似乎您使用了自定义库 CAPSPageMenu ,不幸的是你想做什么是不可用的。

然而,使用 swift 你可以做一些非凡的事情,比如直接在你的代码中扩展 PageMenu:(我只是做了一个例子,但你可以从它开始):

将这段代码添加到你的类的底部或任何你想要的地方:

extension UIView {
func fadeIn(duration: NSTimeInterval = 1.0, delay: NSTimeInterval = 0.0, completion: ((Bool) -> Void) = {(finished: Bool) -> Void in}) {
UIView.animateWithDuration(duration, delay: delay, options: UIViewAnimationOptions.CurveEaseIn, animations: {
self.alpha = 1.0
}, completion: completion) }

func fadeOut(duration: NSTimeInterval = 1.0, delay: NSTimeInterval = 0.0, completion: (Bool) -> Void = {(finished: Bool) -> Void in}) {
UIView.animateWithDuration(duration, delay: delay, options: UIViewAnimationOptions.CurveEaseIn, animations: {
self.alpha = 0.0
}, completion: completion)
}
}



extension CAPSPageMenu {
func addImageOverMyMenuItem(index:Int,imageName:String) {
if index>=0 && index<self.menuItems.count || self.menuItems[index].titleLabel!.frame.height>0 {
var labelFrame = self.menuItems[index].titleLabel!.frame
let image: UIImage = UIImage(named: imageName)!
let bgImage = UIImageView(image: image)
labelFrame.origin.y = self.menuItems[index].frame.origin.y
labelFrame.origin.x += (labelFrame.width/10)
labelFrame.size.width -= (labelFrame.width/10)*2
labelFrame.size.height = self.menuItems[index].titleLabel!.frame.size.height/3
bgImage.frame = labelFrame
bgImage.tag = 9999
bgImage.alpha = 0.0
self.menuItems[index].addSubview(bgImage)
bgImage.fadeIn(completion: {
(finished: Bool) -> Void in
if finished {
// image is showed, do whatever you want
}
})
} else {
print("Due to index out of range or titleLabel dont yet setted i cannot set image")
}
}

func removeImageOverMyMenuItem(index:Int) {
if index>=0 && index<self.menuItems.count {
if let bgImage = self.menuItems[index].viewWithTag(9999) {
bgImage.fadeOut(completion: {
(finished: Bool) -> Void in
if finished {
bgImage.removeFromSuperview()
}
})
}
}
}
}

您可以轻松地使用添加图像:

pageMenu?.addImageOverMyMenuItem(0, imageName: "search.png")

并删除:

pageMenu?.removeImageOverMyMenuItem(0)

附言结果将是这样的:

enter image description here

所以,最后按照 ViewController 中的官方演示 1 项目 (PageMenuDemoStoryboard) 的方法 viewDidAppear 我可以像这样添加我的图像:

// Initialize scroll menu
pageMenu = CAPSPageMenu(viewControllers: controllerArray, frame: CGRectMake(0.0, 0.0, self.view.frame.width, self.view.frame.height), pageMenuOptions: parameters)

pageMenu?.addImageOverMyMenuItem(0, imageName: "search.png")
pageMenu?.addImageOverMyMenuItem(1, imageName: "search.png")
pageMenu?.addImageOverMyMenuItem(2, imageName: "search.png")
self.addChildViewController(pageMenu!)
self.view.addSubview(pageMenu!.view)

pageMenu!.didMoveToParentViewController(self)
...

你得到这个结果:

enter image description here

附言如果不清楚,我只是使用名为“search.png”的图像制作了这个示例,但您可以使用要插入到项目中的所有图像。

关于ios - 以编程方式将两个图像添加到我的标签上方,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36920907/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com