我正在尝试在 SwiftUI 中重写一个应用程序。在此之前,它是在故事板中完成的。该应用程序使用按钮实现横幅旋转器。这是代码:'''
import UIKit
class NavigteViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
let images = [UIImage(named: "Agrokomplexshop"), UIImage(named: "allradru"), UIImage(named: "Baggins"), UIImage(named: "Bestwatch"), UIImage(named: "Blackwoodbag"), UIImage(named: "CDEK"), UIImage(named: "Credit7RU"), UIImage(named: "cvetDivanov"), UIImage(named: "europamarket"), UIImage(named: "fitstars"), UIImage(named: "growfood"), UIImage(named: "Iskussnica"), UIImage(named: "kolesaDarom"), UIImage(named: "Letbefit"), UIImage(named: "LevelKitchen"), UIImage(named: "MadWave"), UIImage(named: "Mealjoyru"), UIImage(named: "MoskovskayaMehovayaKompaniya"), UIImage(named: "NFONorwegianFishOil"), UIImage(named: "OKBeauty"), UIImage(named: "Petrolplus"), UIImage(named: "Pichshop"), UIImage(named: "Pizzaallo"), UIImage(named: "playtoday"), UIImage(named: "Pokrishka"), UIImage(named: "Polis812RU"), UIImage(named: "ProfmaxPro"), UIImage(named: "Randewoo"), UIImage(named: "RenessansJiznRU"), UIImage(named: "rerooms"), UIImage(named: "Rockyshop"), UIImage(named: "sbermarket"), UIImage(named: "Shopneoline"), UIImage(named: "Smallcitysu"), UIImage(named: "Smartwaytoday"), UIImage(named: "sohoshop"), UIImage(named: "Sprinthost"), UIImage(named: "SuperStepRU"), UIImage(named: "TheFurnish"), UIImage(named: "ugindoma"), UIImage(named: "VKMuzika"), UIImage(named: "Vprokru"), UIImage(named: "YandexBuisnessRU"), UIImage(named: "yandexgodlyabiznesa"), UIImage(named: "zaochnik"), UIImage(named: "ZettaStrhovanieRU"), UIImage(named: "zoomag"), UIImage(named: "zooptorgrf"), UIImage(named: "AlfaBankCreditnieKartiRU"), UIImage(named: "aliexpress"), UIImage(named: "alter"), UIImage(named: "autoall"), UIImage(named: "autopiter"), UIImage(named: "Bbcream"), UIImage(named: "calltouch"), UIImage(named: "cantra"), UIImage(named: "CherehapaRU"), UIImage(named: "GeekBrains"), UIImage(named: "productstar"), UIImage(named: "Promomegafitpro"), UIImage(named: "puzzlemovies"), UIImage(named: "RBKPro"), UIImage(named: "skypro"), UIImage(named: "Tetrikaschool"), UIImage(named: "Ukassa"), UIImage(named: "Vipfish"), UIImage(named: "YESEducationGroup")]
let imag = images.randomElement()
bunnerImageBackground.setImage((imag as! UIImage) , for: .normal)
}
@IBOutlet weak var bunnerImageBackground: UIButton!
@IBAction func bunner(_ sender: Any) {
let imagesfor = [UIImage(named: "Agrokomplexshop"), UIImage(named: "allradru"), UIImage(named: "Baggins"), UIImage(named: "Bestwatch"), UIImage(named: "Blackwoodbag"), UIImage(named: "CDEK"), UIImage(named: "Credit7RU"), UIImage(named: "cvetDivanov"), UIImage(named: "europamarket"), UIImage(named: "fitstars"), UIImage(named: "growfood"), UIImage(named: "Iskussnica"), UIImage(named: "kolesaDarom"), UIImage(named: "Letbefit"), UIImage(named: "LevelKitchen"), UIImage(named: "MadWave"), UIImage(named: "Mealjoyru"), UIImage(named: "MoskovskayaMehovayaKompaniya"), UIImage(named: "NFONorwegianFishOil"), UIImage(named: "OKBeauty"), UIImage(named: "Petrolplus"), UIImage(named: "Pichshop"), UIImage(named: "Pizzaallo"), UIImage(named: "playtoday"), UIImage(named: "Pokrishka"), UIImage(named: "Polis812RU"), UIImage(named: "ProfmaxPro"), UIImage(named: "Randewoo"), UIImage(named: "RenessansJiznRU"), UIImage(named: "rerooms"), UIImage(named: "Rocky-shop"), UIImage(named: "sbermarket"), UIImage(named: "Shopneoline"), UIImage(named: "Smallcitysu"), UIImage(named: "Smartwaytoday"), UIImage(named: "sohoshop"), UIImage(named: "Sprinthost"), UIImage(named: "SuperStepRU"), UIImage(named: "TheFurnish"), UIImage(named: "ugindoma"), UIImage(named: "VKMuzika"), UIImage(named: "Vprokru"), UIImage(named: "YandexBuisnessRU"), UIImage(named: "yandexgodlyabiznesa"), UIImage(named: "zaochnik"), UIImage(named: "ZettaStrhovanieRU"), UIImage(named: "zoomag"), UIImage(named: "zooptorgrf"), UIImage(named: "AlfaBankCreditnieKartiRU"), UIImage(named: "aliexpress"), UIImage(named: "alter"), UIImage(named: "autoall"), UIImage(named: "autopiter"), UIImage(named: "Bbcream"), UIImage(named: "calltouch"), UIImage(named: "cantra"), UIImage(named: "CherehapaRU"), UIImage(named: "GeekBrains"), UIImage(named: "productstar"), UIImage(named: "Promomegafitpro"), UIImage(named: "puzzlemovies"), UIImage(named: "RBKPro"), UIImage(named: "skypro"), UIImage(named: "Tetrikaschool"), UIImage(named: "Ukassa"), UIImage(named: "Vipfish"), UIImage(named: "YESEducationGroup")]
switch bunnerImageBackground.currentImage {
case imagesfor[0]:
UIApplication.shared.open(URL(string: "https://aflink.ru/g/lkj8u73nnd31225e5a7704cb4947eb/")!)
case imagesfor[1]:
UIApplication.shared.open(URL(string: "https://aflink.ru/g/elgf718etq31225e5a770e020ad620/?erid=LatgBUcjU")!)
case imagesfor[2]:
UIApplication.shared.open(URL(string: "https://aflink.ru/g/m33u5flihp31225e5a7787b20ecef3/?erid=LatgBgkRu")!)
case imagesfor[3]:
UIApplication.shared.open(URL(string: "https://aflink.ru/g/7d074b20eb31225e5a7777d7343c30/?erid=LatgBiNsP")!)
case imagesfor[4]:
UIApplication.shared.open(URL(string: "https://aflink.ru/g/n90gqfg8zz31225e5a7768b0e348e3/?erid=LatgBs8Xq")!)
case imagesfor[5]:
UIApplication.shared.open(URL(string: "https://aflink.ru/g/8m5m8022me31225e5a77fa1399d1cb/")!)
case imagesfor[6]:
UIApplication.shared.open(URL(string: "https://aflink.ru/g/vojb18n6gs31225e5a7796bf0d7973/?erid=LatgBnShk")!)
case imagesfor[7]:
UIApplication.shared.open(URL(string: "https://aflink.ru/g/67570bafb631225e5a7766146bcf1d/")!)
case imagesfor[8]:
UIApplication.shared.open(URL(string: "https://aflink.ru/g/yh7dxsl4ze31225e5a770d5c628f32/?erid=LatgBk5GN")!)
case imagesfor[9]:
UIApplication.shared.open(URL(string: "https://aflink.ru/g/lcz5l7shqa31225e5a77253f07e8f6/")!)
case imagesfor[10]:
UIApplication.shared.open(URL(string: "https://aflink.ru/g/36wusytszy31225e5a77aeb982cd0a/")!)
case imagesfor[11]:
UIApplication.shared.open(URL(string: "https://aflink.ru/g/er5wwbo0j631225e5a77f665c6f820/")!)
case imagesfor[12]:
UIApplication.shared.open(URL(string: "https://aflink.ru/g/phemb8m2uc31225e5a77ccd85565ce/?erid=LatgCAysS")!)
case imagesfor[13]:
UIApplication.shared.open(URL(string: "https://aflink.ru/g/qm29jzxwek31225e5a77fcd3300043/")!)
case imagesfor[14]:
UIApplication.shared.open(URL(string: "https://aflink.ru/g/u7cz7332nc31225e5a77738d825a2a/")!)
case imagesfor[15]:
UIApplication.shared.open(URL(string: "https://aflink.ru/g/02ocqj6mtr31225e5a7702f7474c98/?erid=LatgBxmTS")!)
case imagesfor[16]:
UIApplication.shared.open(URL(string: "https://aflink.ru/g/ml2h339a7u31225e5a77c9dd5f6bbc/")!)
case imagesfor[17]:
UIApplication.shared.open(URL(string: "https://aflink.ru/g/06d83a456331225e5a77c6abdc5353/")!)
case imagesfor[18]:
UIApplication.shared.open(URL(string: "https://aflink.ru/g/l604dr7c1e31225e5a779c176d7edf/?erid=LatgBome6")!)
case imagesfor[19]:
UIApplication.shared.open(URL(string: "https://aflink.ru/g/nl23vc8ifb31225e5a7753b0491767/?erid=LatgBpT4S")!)
case imagesfor[20]:
UIApplication.shared.open(URL(string: "https://aflink.ru/g/ib927vcr1x31225e5a77fdbdccbe6f/?erid=LatgBxrwE")!)
case imagesfor[21]:
UIApplication.shared.open(URL(string: "https://aflink.ru/g/balo8rex4231225e5a77a28df5fc31/?erid=LatgC9HFD")!)
case imagesfor[22]:
UIApplication.shared.open(URL(string: "https://aflink.ru/g/bgi1r32ntf31225e5a77b0f1f587ba/")!)
case imagesfor[23]:
UIApplication.shared.open(URL(string: "https://aflink.ru/g/ibbrr5pyi831225e5a7778ec4c4caa/")!)
case imagesfor[24]:
UIApplication.shared.open(URL(string: "https://aflink.ru/g/bw09hq1psa31225e5a77729032ae9b/?erid=LatgBtona")!)
case imagesfor[25]:
UIApplication.shared.open(URL(string: "https://aflink.ru/g/m7xuhz7ysw31225e5a77440db5965d/")!)
case imagesfor[26]:
UIApplication.shared.open(URL(string: "https://aflink.ru/g/d2y1qd8flb31225e5a7739152ee3f6/?erid=LatgBhUb7")!)
case imagesfor[27]:
UIApplication.shared.open(URL(string: "https://aflink.ru/g/efixkgr4c431225e5a770dd11956d5/")!)
case imagesfor[28]:
UIApplication.shared.open(URL(string: "https://aflink.ru/g/9qw2p1opbr31225e5a77e76cad6e67/?erid=LatgBgjtz")!)
case imagesfor[29]:
UIApplication.shared.open(URL(string: "https://aflink.ru/g/5nilvc2n6731225e5a77592052c97a/?erid=LatgBbjCK")!)
case imagesfor[30]:
UIApplication.shared.open(URL(string: "https://aflink.ru/g/lrpvclu24w31225e5a77be166c0a8b/?erid=LatgBeihm")!)
case imagesfor[31]:
UIApplication.shared.open(URL(string: "https://aflink.ru/g/ugldybr34d31225e5a77326cfdd537/?erid=LatgBYJCD")!)
case imagesfor[32]:
UIApplication.shared.open(URL(string: "https://aflink.ru/g/pi8gje4m1p31225e5a771c454c946b/?erid=LatgBso7Y")!)
case imagesfor[33]:
UIApplication.shared.open(URL(string: "https://aflink.ru/g/vwrmyws33j31225e5a77d720df3727/?erid=LatgBnrbd")!)
case imagesfor[34]:
UIApplication.shared.open(URL(string: "https://aflink.ru/g/c2jesykfil31225e5a7792877be4a1/?erid=LatgCB1Es")!)
case imagesfor[35]:
UIApplication.shared.open(URL(string: "https://aflink.ru/g/x5npm7rr8e31225e5a77e6dc21c9f7/")!)
case imagesfor[36]:
UIApplication.shared.open(URL(string: "https://aflink.ru/g/mxmtlx74bh31225e5a77b183dfeeb8/?erid=LatgByErP")!)
case imagesfor[37]:
UIApplication.shared.open(URL(string: "https://aflink.ru/g/enml32rwhr31225e5a77a814938f71/?erid=LatgBXe4u")!)
case imagesfor[38]:
UIApplication.shared.open(URL(string: "https://aflink.ru/g/e119380ec031225e5a7775922924c2/?erid=LatgBVe6K")!)
case imagesfor[39]:
UIApplication.shared.open(URL(string: "https://aflink.ru/g/5ypaik84gp31225e5a77069ee805e1/?erid=LatgBVKg8")!)
case imagesfor[40]:
UIApplication.shared.open(URL(string: "https://aflink.ru/g/dnezl2xu4131225e5a77af54ab86e0/")!)
case imagesfor[41]:
UIApplication.shared.open(URL(string: "https://aflink.ru/g/paesexo5wj31225e5a77aa88a5b1cc/")!)
case imagesfor[42]:
UIApplication.shared.open(URL(string: "https://aflink.ru/g/tsur35yw0t31225e5a77ff277dce71/")!)
case imagesfor[43]:
UIApplication.shared.open(URL(string: "https://aflink.ru/g/kzlcevsny831225e5a7784814a437b/")!)
case imagesfor[44]:
UIApplication.shared.open(URL(string: "https://aflink.ru/g/sk55otxguo31225e5a776853a97b34/?erid=LatgBb6k3")!)
case imagesfor[45]:
UIApplication.shared.open(URL(string: "https://aflink.ru/g/27wmm2ciw331225e5a774d3555d70b/?erid=LatgBTy98")!)
case imagesfor[46]:
UIApplication.shared.open(URL(string: "https://aflink.ru/g/m7bdjiij7e31225e5a7745912f3e2d/?erid=LatgCAHzf")!)
case imagesfor[47]:
UIApplication.shared.open(URL(string: "https://aflink.ru/g/rvcnriueyx31225e5a77e22b63c9b7/?erid=LatgC9HKd")!)
case imagesfor[48]:
UIApplication.shared.open(URL(string: "https://aflink.ru/g/i352cbwpfv31225e5a771cb5598fff/")!)
case imagesfor[49]:
UIApplication.shared.open(URL(string: "https://alitems.co/g/vv3q4oey1v31225e5a77b6d1781017/")!)
case imagesfor[50]:
UIApplication.shared.open(URL(string: "https://aflink.ru/g/fxysja9qse31225e5a776704bcdbc1/?erid=LatgC1bqD")!)
case imagesfor[51]:
UIApplication.shared.open(URL(string: "https://aflink.ru/g/5rermd1rb531225e5a77aeed5c54e0/?erid=LatgBk8mS")!)
case imagesfor[52]:
UIApplication.shared.open(URL(string: "https://aflink.ru/g/1hedtmqch531225e5a771423f7dae0/?erid=LatgBpn1Q")!)
case imagesfor[53]:
UIApplication.shared.open(URL(string: "https://aflink.ru/g/9l1uizc2km31225e5a771bd767fd6d/?erid=LatgBYK7C")!)
case imagesfor[54]:
UIApplication.shared.open(URL(string: "https://aflink.ru/g/y3x3cfffmv31225e5a77f6ebb01ee1/?erid=LatgC3YPC")!)
case imagesfor[55]:
UIApplication.shared.open(URL(string: "https://aflink.ru/g/ezmwtz3pov31225e5a7785aed9e6f6/?erid=LatgBaf2A")!)
case imagesfor[56]:
UIApplication.shared.open(URL(string: "https://aflink.ru/g/2ey5h355qu31225e5a771e9ee21cc3/?erid=LatgBpnvS")!)
case imagesfor[57]:
UIApplication.shared.open(URL(string: "https://aflink.ru/g/k3dfvevwit31225e5a7765a37ca03d/")!)
case imagesfor[58]:
UIApplication.shared.open(URL(string: "https://aflink.ru/g/t9pnpumx6q31225e5a775e445e6c7b/?erid=LatgC7JZZ")!)
case imagesfor[59]:
UIApplication.shared.open(URL(string: "https://aflink.ru/g/ehygr1fnh831225e5a77ed7d1ea7c0/?erid=LatgBtjgG")!)
case imagesfor[60]:
UIApplication.shared.open(URL(string: "https://aflink.ru/g/ae4zpznc0i31225e5a7742f9f2178b/?erid=LatgC5CoU")!)
case imagesfor[61]:
UIApplication.shared.open(URL(string: "https://aflink.ru/g/hmgfm467d731225e5a774c86cb021e/?erid=LatgC5eJ7")!)
case imagesfor[62]:
UIApplication.shared.open(URL(string: "https://aflink.ru/g/kpr3e0qpel31225e5a775eaaeb7930/?erid=LatgBtSwH")!)
case imagesfor[63]:
UIApplication.shared.open(URL(string: "https://aflink.ru/g/54h3zonoi631225e5a7794d0e5a27a/")!)
case imagesfor[64]:
UIApplication.shared.open(URL(string: "https://aflink.ru/g/hlif2saueo31225e5a77dff88f55df/")!)
case imagesfor[65]:
UIApplication.shared.open(URL(string: "https://aflink.ru/g/lqb6l7197z31225e5a7794cf89477e/")!)
case imagesfor[66]:
UIApplication.shared.open(URL(string: "https://aflink.ru/g/tfogdtvvuf31225e5a772019e44728/")!)
default:
break
}
}
}
''' 现在如何在 SwiftUI 中实现它?我所做的一部分是在加载背景按钮时使图像随机。为此,我使用以下代码创建了一个单独的文件:'''
import SwiftUI
let images = [
Image("Agrokomplexshop"), Image("allradru"), Image("Baggins"), Image("Bestwatch"), Image("Blackwoodbag"), Image("CDEK"), Image("Credit7RU"), Image("cvetDivanov"), Image("europamarket"), Image("fitstars"), Image("growfood"), Image("Iskussnica"), Image("kolesaDarom"), Image("Letbefit"), Image("LevelKitchen"), Image("MadWave"), Image("Mealjoyru"), Image("MoskovskayaMehovayaKompaniya"), Image("NFONorwegianFishOil"), Image("OKBeauty"), Image("Petrolplus"), Image("Pichshop"), Image("Pizzaallo"), Image("playtoday"), Image("Pokrishka"), Image("Polis812RU"), Image("ProfmaxPro"), Image("Randewoo"), Image("RenessansJiznRU"), Image("rerooms"), Image("Rockyshop"), Image("sbermarket"), Image("Shopneoline"), Image("Smallcitysu"), Image("Smartwaytoday"), Image("sohoshop"), Image("Sprinthost"), Image("SuperStepRU"), Image("TheFurnish"), Image("ugindoma"), Image("VKMuzika"), Image("Vprokru"), Image("YandexBuisnessRU"), Image("yandexgodlyabiznesa"), Image("zaochnik"), Image("ZettaStrhovanieRU"), Image("zoomag"), Image("zooptorgrf"), Image("AlfaBankCreditnieKartiRU"), Image("aliexpress"), Image("alter"), Image("autoall"), Image("autopiter"), Image("Bbcream"), Image("calltouch"), Image("cantra"), Image("CherehapaRU"), Image("GeekBrains"), Image("productstar"), Image("Promomegafitpro"), Image("puzzlemovies"), Image("RBKPro"), Image("skypro"), Image("Tetrikaschool"), Image("Ukassa"), Image("Vipfish"), Image("YESEducationGroup")]
let imag = images.randomElement()
struct BunnerButton: ButtonStyle {
func makeBody(configuration: Configuration) -> some View {
configuration.label
imag!
.renderingMode(.original)
.frame(width: 300, height: 38)
}
}
''' 并为按钮添加样式:'''
Button("Реклама") {
//action
}
.buttonStyle(BunnerButton())
''' 但是我不明白如何执行操作。请帮帮我。
它可以是这样的,例如: