我有一个自定义视图:
struct ImageContent: View {
var body: some View {
Image("smile")
.resizable()
.scaledToFit()
}
}
Run Code Online (Sandbox Code Playgroud)
哪个被放置到另一个视图中GeometryReader:
var body: some View {
GeometryReader { geometry in
ImageContent()
//Image("smile").resizable().scaledToFit()
}
}
Run Code Online (Sandbox Code Playgroud)
问题是,ImageContent视图不是在屏幕上居中,而是被放置在顶部,但是,通过删除ImageContent子视图并将视图的内容直接添加到几何阅读器中将解决这个问题(见图)。
此外,删除GeometryReader也可以解决问题。
我需要子视图,因为我将实现一些额外的逻辑,还需要 ,GeometryReader因为有一个手势添加到Image使用它的 。
任何的想法?
我有一个固定大小的视图控制器(它不能调整大小).此特定视图控制器在窗口中显示为工作表,我通过设置禁用了调整大小preferredContentSize.由于它不能再调整大小,我没有任何约束地离开了视图.但是,当我构建应用程序时,我收到了很多警告:
Views without any layout constraints may clip their content or overlap other views.
Run Code Online (Sandbox Code Playgroud)
我知道如果你不在标准窗口中添加任何约束,它将在调整大小时剪切.但是,在我的特定窗口上禁用了调整大小,以便我知道它不会发生.有没有办法让这个警告沉默?这个特定的视图控制器有很多视图,它阻止了我在问题导航器中可能遇到的其他有用的警告.
该视图控制器通过"Sheet"segue呈现.
我知道您可以使用AVFoundation设备的相机扫描QR码.现在问题来了,我怎样才能从静态UIImage对象中做到这一点?
iPhone X几天前发布.我正在尝试将我的一个游戏更新到新手机.但是,在具有SpriteKit场景的一个场景中,场景不会缩放以填充整个屏幕.
以下是显示场景的代码:
GameScene *scene = [GameScene unarchiveFromFile:@"GameScene"];
scene.scaleMode = SKSceneScaleModeFill;
// skView is basically self.view casted to SKView
[skView presentScene:scene];
Run Code Online (Sandbox Code Playgroud)
SKS文件中场景的大小为2048x1536.
这就是它的样子:
红色部分是场景内容.一个有趣的事情是,背景(蓝色和灰色,确实正确显示).
有任何解决这个问题的方法吗?我只是拉伸场景来填充显示器(没有任何东西被圆角和凹口覆盖)
我正在编写一些代码来处理Swift中带双引号的字符串.这是我到目前为止所做的:
func someMethod {
let string = "String with \"Double Quotes\""
dealWithString(string)
}
func dealWithString(input: String) {
// I placed a breakpoint here.
}
Run Code Online (Sandbox Code Playgroud)
当我运行代码时,断点像往常一样停在那里但是当我将以下内容输入调试器时:
print input
Run Code Online (Sandbox Code Playgroud)
这就是我得到的:
(String) $R0 = "String with \"Double Quotes\""
Run Code Online (Sandbox Code Playgroud)
我用反斜杠得到了这个字符串.但是,如果我试图从源中删除反斜杠,它将给我编译错误.这有解决方法吗?
我的 macOS 应用程序有一个通知服务扩展。
这是该扩展的代码:
import UserNotifications
class NotificationService: UNNotificationServiceExtension {
var contentHandler: ((UNNotificationContent) -> Void)?
var bestAttemptContent: UNMutableNotificationContent?
override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
print("Extension received notification!")
self.contentHandler = contentHandler
bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent)
bestAttemptContent?.title = "Title modified!"
}
override func serviceExtensionTimeWillExpire() {
// Called just before the extension will be terminated by the system.
// Use this as an opportunity to deliver your "best attempt" at modified content, otherwise the original push …Run Code Online (Sandbox Code Playgroud) 我制作了一个在第一个视图控制器(启动屏幕后立即显示的控制器)上同时支持横向和纵向模式的应用程序。我可以以纵向模式启动此应用,将其旋转至横向,然后再回到纵向,没有任何问题。但是,只要该应用最初以横向模式启动,它的行为就会非常奇怪。我想到的一个简单的解决方法是强制应用以纵向模式启动,然后让用户能够在启动后将其旋转为横向。
有什么办法可以做到这一点?
我使用的是Xcode 8.3,iOS 10.3,正在测试我的应用的设备是iPad Pro 12.9。
这是我第一次使用SKAudioNode.首先,我在我GameScene班级的顶部宣布了一个属性:
var backgroundMusic: SKAudioNode!
Run Code Online (Sandbox Code Playgroud)
现在我添加了一个帮助方法:
func playBackgroundMusic(name: String) {
if backgroundMusic != nil {
backgroundMusic.removeFromParent()
}
backgroundMusic = SKAudioNode(fileNamed: name)
backgroundMusic.autoplayLooped = true
addChild(backgroundMusic)
}
Run Code Online (Sandbox Code Playgroud)
现在我这样调用这个方法:
playBackgroundMusic("ABC.caf")
Run Code Online (Sandbox Code Playgroud)
它在这一行引发致命错误:
backgroundMusic.autoplayLooped = true
Run Code Online (Sandbox Code Playgroud)
说:在解开可选值时意外发现nil.
我确实做了以下事情
现在还应该在哪里检查错误?
编辑:
这是我的配置信息:
设备和模拟器都不起作用.
EDIT2:
我将我的代码更改为以下内容:
func playBackgroundMusic(name: String) {
if backgroundMusic != nil {
backgroundMusic.removeFromParent()
}
let temp = SKAudioNode(fileNamed: name)
temp.autoplayLooped = true
backgroundMusic = temp
//backgroundMusic = SKAudioNode(fileNamed: "SpaceGame.caf")
//backgroundMusic.autoplayLooped = true
addChild(backgroundMusic)
} …Run Code Online (Sandbox Code Playgroud) 我正在制作一个需要运行shell脚本的OS X App.这是我的快速代码:
func runTask(arguments: [String]) {
output.string = ""
task = NSTask()
task.launchPath = "/bin/bash"
task.arguments = arguments;
errorPipe = NSPipe()
outputPipe = NSPipe()
task.standardError = errorPipe
task.standardOutput = outputPipe
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(didCompleteReadingFileHandle(_:)), name: NSFileHandleReadCompletionNotification, object: task.standardOutput!.fileHandleForReading)
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(didCompleteReadingFileHandle(_:)), name: NSFileHandleReadCompletionNotification, object: task.standardError!.fileHandleForReading)
errorPipe.fileHandleForReading.readInBackgroundAndNotify()
outputPipe.fileHandleForReading.readInBackgroundAndNotify()
task.launch()
}
func didCompleteReadingFileHandle(sender: NSNotification) {
let data: NSData = sender.userInfo![NSFileHandleNotificationDataItem] as! NSData;
let string = NSString(data: data, encoding: NSUTF8StringEncoding)!
// The output property is a NSTextView object
output.string?.appendContentsOf(String(string))
}
Run Code Online (Sandbox Code Playgroud)
现在我尝试调用该runTask方法: …
我有一个 NSImage 对象,我有一个 CIDetector 对象,用于检测该图像上的 QR 代码。检测到后,我想修剪该图像,使其中只包含二维码。这就是我获得二维码边界的方法:
NSArray *features = [myQRDetector featureInImage:myCIImage];
CIQRCodeFeature *qrFeature = features[0];
CGRect qrBounds = qrFeature.bounds;
Run Code Online (Sandbox Code Playgroud)
现在我如何修剪图像,使其仅包含qrBounds变量描述的区域。
我有一个看起来像这样的方法:
static inline float average(int numbers, ...) {
}
Run Code Online (Sandbox Code Playgroud)
你如何使用for循环来获得我已经将数字放入的所有值.例如:
average(1, 3, 5, 7);
Run Code Online (Sandbox Code Playgroud) ios ×4
macos ×4
swift ×3
sprite-kit ×2
c ×1
for-loop ×1
ipad ×1
iphone ×1
nsimage ×1
nstask ×1
orientation ×1
qr-code ×1
skaudionode ×1
string ×1
swiftui ×1