我正在 SwiftUI 中创建一个自定义路径并用红色填充它,我想为我的路径提供边框或描边。我们怎样才能做到这一点?
struct ContentView: View
{
var body: some View
{
Path { path in
path.addArc(center: CGPoint(x: 100, y: 300), radius: 200, startAngle: Angle(degrees: -90), endAngle: Angle(degrees: 0), clockwise: false)
}
.fill(Color.red)
}
}
Run Code Online (Sandbox Code Playgroud) 我有一个名为UIViewControllerModel的类,我喜欢为该类初始化背景颜色,我确信这是一个语法错误,需要帮助来纠正。
class UIViewControllerModel: UIViewController {
var backgroundColor: UIColor
required init(backgroundColor: UIColor) {
super.init()
self.backgroundColor = backgroundColor
}
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = backgroundColor
}
}
Run Code Online (Sandbox Code Playgroud)
我想扩展 SwiftUI.Button,以便我可以添加一个 type 属性并将该类型更改为不同类型的主题按钮。但我是 Swift 新手,我不知道如何让我的标签和动作道具变得通用。
换句话说,如果我这样做
import SwiftUI
struct Button: View {
var type: String = ""
var action = {}
var label = {
Text("Button")
}
var body: some View {
ZStack(content: {
SwiftUI.Button(action: action, label: label)
})
}
}
Run Code Online (Sandbox Code Playgroud)
它将闭包限制为仅允许标签返回 text()
如何才能做到这一点
还有关于我应该如何根据类型更改按钮的“更改”的任何建议。
注意:有人对此表示反对,因为它类似于来自另一个用户的按钮样式查询,但事实并非如此。
该解决方案只是将预制样式添加到默认的 SwiftUI.button 结构中,但这不是我的目标。
我正在尝试使用可以传递的类型属性来扩展 SwiftUI.Button,并将根据该输入设置样式。
尽管他们有着相同的结果,但他们并没有实现相同的目标。我的解决方案将提供一个动态样式的组件,可以在整个项目中使用。无需尾随.buttonStyle(BlueButtonStyle())
背景:我在使用SwiftUI Transitions. 我有一个FormView包含 4 个不同的Sub-Forms. 当用户按下 时Next-Button,下一个Sub-Form View会出现Transition (.move -> 从右到左)。
我也有一个Back Button. 当用户按下此按钮时,Sub-Form View将显示上一个,当前具有相同的Transition (.move -> 从右到左)。但是,我想将Transition本例中的(.move -> left to right)反转为 (.move -> left to right) 。
代码
enum FormStep {
case step1, step2, step3, step4
}
struct CustomForm: View {
@State private var step: FormStep = .step1
var body: some View {
// This Button …Run Code Online (Sandbox Code Playgroud) 我正在使用可选的开关,我注意到开关不能接受 nil 作为情况!
func test(value: Bool?) {
switch value {
case true: print("true")
case false: print("false")
case nil: print("nil")
}
}
Run Code Online (Sandbox Code Playgroud)
错误:切换必须详尽无遗
然后我使用默认值来解决问题,但我想了解为什么 nil 不能在第一个代码中工作?
我想在一个Text视图中使用多种文本格式SwiftUI中使用多种文本格式。这看起来像这样:
\n\n我想在同一个文本框中同时拥有多种文本格式,即使它真的真的真的真的
\n真的文本较长,因此需要正确换行。(甚至可能包括链接!)
我发现了这个(如何在 TextField swiftUI 中加粗文本?) \xc2\xa0\xc2\xa0thread ,但这根本不是我想要的。
\n这将类似于HTML\ 的工作span
我有这个矩形,我想给它带来移动的破折号效果!如果形状是圆形,我可以简单地用动画旋转视图,因为这是一个矩形!我没有其他想法可以实现这一目标!
struct ContentView: View {
var body: some View {
Rectangle()
.strokeBorder(style: StrokeStyle(lineWidth: 5, dash: [10]))
.frame(width: 200, height: 200)
.foregroundColor(Color.blue)
}
}
Run Code Online (Sandbox Code Playgroud)
我正在尝试在用户拉动刷新后从 UIViewcontroller 向 SwiftUI View 发送通知。
@objc private func fetchScheduleData(_ sender: UIRefreshControl) {
NotificationCenter.default.post(name: Notification.Name(rawValue: "didPullToRefreash"), object: nil)
}
Run Code Online (Sandbox Code Playgroud)
在 SwiftUI 视图上我尝试设置此方法 .onchange()
NotificationCenter.default.addObserver(self, selector: #selector(didPullToRefreashHelper), name: Notification.Name(rawValue: "didTapNotification"), object: nil)
Run Code Online (Sandbox Code Playgroud)
但 onChange 它不起作用。我想知道我将如何做到这一点。
我想从 SwiftUI 中的FileImporter选择文件,但无法选择文件
这是我的代码:
struct ContentView: View {
@State var isShowing = false
var body: some View {
VStack {
Button {
isShowing.toggle()
} label: {
Text("documents")
}.fileImporter(isPresented: $isShowing, allowedContentTypes: [.item]) { result in
switch result {
case .success(let Fileurl):
print(Fileurl)
case .failure(let error):
print(error)
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
我该如何解决这个问题?
正如我们所知,我们有一个非常有用的包装器,称为 @ViewBuilder 用于视图,但我缺少相同的 Shape 协议包装器,由于没有可用的包装器,我希望尝试构建一个用于学习多孔的包装器。
以下是视图的工作代码,并希望构建形状的包装器:
@ViewBuilder func testForView(value: Bool) -> some View {
if value {
Text("Hello")
}
else {
Button("world!") {}
}
}
// The Goal:
@ShapeBuilder func testForShape(value: Bool) -> some Shape {
if value {
Circle()
}
else {
Rectangle()
}
}
Run Code Online (Sandbox Code Playgroud)
我有使用包装器甚至定义自定义包装器的经验,但我所有的尝试都是包装一个值,而不是一个函数!我从来没有尝试过包装一个函数,坦白说我不知道它是 Swift 还是 SwiftUI 中的东西。那么我怎样才能使@ShapeBuilder像@ViewBuilder一样工作,我知道我必须使用Tuple或更正确的TupleShape,所以我认为我也应该定义TupleShape。
我从用户那里获取一个文件夹 url,然后寻找该文件夹中的任何 mp3 文件,问题本身在标题中,而我只是想在过程中使用UTType。
正如你所看到的,我完成了代码中的所有步骤,只需要isMP3函数中的最后一步来完成拼图。那么我如何使用路径或 URL 并找出它的 UTType 并使用它进行比较。
同样在我的方法中,Xcode 给出了一个错误并说:
在范围内找不到“UTType”
不知道为什么会出现这个错误,通常情况下不应该是这样,因为它是苹果定义的类型。
struct ContentView: View {
@State private var fileImporterIsPresented: Bool = false
var body: some View {
Button("Select your Folder") { fileImporterIsPresented = true }
.fileImporter(isPresented: $fileImporterIsPresented, allowedContentTypes: [.folder], allowsMultipleSelection: false, onCompletion: { result in
switch result {
case .success(let urls):
if let unwrappedURL: URL = urls.first {
if let contents = try? FileManager.default.contentsOfDirectory(atPath: unwrappedURL.path) {
contents.forEach { item in
if isMP3(path: unwrappedURL.path + …Run Code Online (Sandbox Code Playgroud) 我想访问我的枚举的值,以防万一,我做了一个切换来访问值,但正如你所看到的,我正在收获我的代码,我怎样才能以更好的方式做到这一点?
enum TestEnum {
case a(value: Int)
case b(value: Int)
case c(value: Int)
var value: Int {
switch self {
case .a(let value):
return value
case .b(let value):
return value
case .c(let value):
return value
}
}
}
Run Code Online (Sandbox Code Playgroud) 我使用下面的代码来定义一个int类型,以便稍后在 Swift 中使用。我正在连接 C 和 Swift。
int testValue = 100;
Run Code Online (Sandbox Code Playgroud)
但testValue像 Swift 一样被强制转换Int32。这是 Swift 中的用例:
print(type(of: testValue)) // prints: Int32
Run Code Online (Sandbox Code Playgroud)
很明显,我可以转换testValue为Swift 中的 Type,但是range 与rangeInt之间存在巨大差异,因此 的值类型应该与Swift 的类型兼容。Int32Int/Int64testValueInt
所以我的目标是从 C 发送某种与Swift 中的int标准Int类型相匹配的类型。这个问题/问题应该从 C 部分回答,并且 Swift 应该收到想要使用的兼容类型。