我试图将地图放置在另一个视图下,以便可以通过向上滚动视图来显示它。这个 gif 应该展示:

此实现是通过实际内容下方的间隔符完成的:
var body: some View {
ZStack {
//i want to tap this view...
BackgroundMapCONTENTVIEW()
.onTapGesture {
print("i wish i could be tapped")
}
//...but this ScrollView is getting all the user input
GeometryReader { proxy in
ScrollView {
ZStack {
Rectangle()
.accentColor(Color.white)
.foregroundColor(Color.white)
.frame(width: proxy.size.width, height: proxy.size.height, alignment: .top)
.allowsHitTesting(false)
CompassCONTENTVIEW()
.frame(width: proxy.size.width, height: proxy.size.height, alignment: .top)
}
Spacer()
.frame(height: 450.0)
.opacity(0.0)
.disabled(true)
.allowsHitTesting(false)
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
如您所见,我尝试了 .allowsHitTesting(false) 和 .disabled(true)。但 ScrollView 当前正在获取所有触摸事件 …
我的单元格可能必须显示一个长文本。在这种情况下,它应该换行到第二行。
我在属性检查器中将“ 行数”设置为2,将“ 换行符”设置为“自动换行”。Interface Builder预览确认并按照预期/期望的方式运行。
属性检查器中的其他更改似乎也无效(例如,文本对齐)!
我希望创建一个自定义子类UIAlertController。如果我理解正确,则需要super.init(title...在子类初始化期间调用某处。
但是我一直遇到指定初始化程序的问题。我已经阅读了文档,无法弄清楚如何使其工作。这是我的代码(请注意代码中的注释):
class AccountInfoActionSheet: UIAlertController {
required init?(coder aDecoder: NSCoder) { //xcode says I need this
super.init(coder: aDecoder) //it also says I need to call a designated initializers so here it is
super.init(title: "Info", message: "Heres the Info", preferredStyle: .actionSheet) //but now I can't call this initializer
fatalError("init(coder:) has not been implemented")
}
}
Run Code Online (Sandbox Code Playgroud)
编辑:由于UIAlertController不能被子类化,我只是创建了一个函数,该函数返回UIAlertController需要在ViewController内部正确配置的函数。
我globalVariable在全局范围内声明了一个可能随时更改的变量。
不同的ViewControllers在我的应用程序需要做出不同的反应,当globalVariable变化。
因此,最好在每个属性中添加一个属性观察器,ViewController以便在globalVariable更改时执行所需的代码。
我似乎无法使用overrideor实现它extension。去这里的路是什么?
我想根据当前触摸的次数更改UIView的颜色。我已经成功创建了一个触摸:
class colorChangerViewClass: UIView {
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
setColor(color: .blue)
print("touchesBegan")
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
setColor(color: .green)
print("touchesEnded")
}
func setColor(color: UIColor) {
self.backgroundColor = color
}
}
Run Code Online (Sandbox Code Playgroud)
但是我正在努力实现多点触控。我觉得我不明白这里的内容。
UIView是否有一个属性可以检查当前有多少手指在触摸它?
我可以检查是否每次发生新的touchesBegan或touchesEnded。
我有一个函数,它接受一个类型的实例Being.有两个子类Being:Person和Animal.我希望函数能够识别两者中的哪一个被传递,并使用特定于每个子类的属性执行某些操作.
我认为这会奏效:
func fight(attacker1: Being, attacker2: Being) -> String {
if attacker1 is Person {
let name1 = attacker1.name as! Person //Error: Value of type Being has no member name
} else {
print("Its not a person")
}
Run Code Online (Sandbox Code Playgroud)
但事实并非如此.实现我想要的最顺畅/最短的方式是什么?
这可能是一个非常愚蠢的问题,但请耐心等待:根据我的理解,在阅读其他网站上的文档和各种示例后,guard语句会检查一个bool.如果是,则当前范围继续执行.如果没有,else则执行该子句.这里应该使用一个return语句来退出当前范围.然而,这对我不起作用.
基本上:为什么不return退出这个功能?
class Person {
var name: String
init (name: String) {
self.name = name
}
func reverseNameUnlessItsHans() {
guard name == "Hans" else { //should exit the scope...
print("It's Hans")
return
}
self.name = String(name.characters.reversed()) //...yet this is executed
}
}
var myPerson = Person(name: "Hans")
myPerson.reverseNameUnlessItsHans()
print(myPerson.name) //prints "snaH"
Run Code Online (Sandbox Code Playgroud) swift ×7
ios ×4
xcode ×2
casting ×1
control-flow ×1
guard ×1
iphone ×1
properties ×1
subclass ×1
swiftui ×1
uitableview ×1
uitouch ×1
uiview ×1