一些小步骤开始围绕Swift缠头.我基本上移植了一个旧类,它只是找到一个名称的匹配图标并返回相应的UIImage.事情的Swift部分似乎正在运行,看起来(几乎)像这样:
@objc class ImageHandler{
func iconForData(data: MyData) -> UIImage{
let imagesAndNames = [
"1": "tree.png",
"2": "car.png",
"3": "house.png",
"7": "boat.png",
]
var imageName: String? = imagesAndNames[data.imageName]
if !imageName{
imageName = "placeholder.png"
}
let icon = UIImage(named: imageName)
return icon
}
}
Run Code Online (Sandbox Code Playgroud)
上面没有任何警告.然而,我的旧Objective-C类要求在swift类上使用alloc方法.
ImageHandler *imageHandler = [ImageHandler alloc] init];
Run Code Online (Sandbox Code Playgroud)
返回错误"没有选择器'alloc'的已知类方法,我猜这是真的,但是如何逃避这个?我是否必须以我的swift-class NSObject为基础来避免这种情况?
嗨,我有一个ViewController在按钮上执行segue.
- (IBAction)moveToCoolViewButtonTapped {
[self performSegueWithIdentifier:@"toCoolView" sender:nil];
}
Run Code Online (Sandbox Code Playgroud)
这个工作正常,除了第一次执行时的烦人延迟,我想由于视图尚未初始化.我显然不希望过早地创建很多观点.(还有其他几个segues来自同一个viewController).所以,也许是一个长镜头:但我想知道是否有人有任何出色的技巧来避免最初的滞后?
好吧,这是我在使用CGImageSource时遇到的情况,并注意到CFDictionary和NSDictionary之间的免费桥接在某些情况下似乎遇到了问题.我已设法构建以下示例以显示我的意思:
func optionalProblemDictionary() -> CFDictionary? {
let key = "key"
let value = "value"
var keyCallBacks = CFDictionaryKeyCallBacks()
var valueCallBacks = CFDictionaryValueCallBacks()
let cfDictionary = CFDictionaryCreate(kCFAllocatorDefault, UnsafeMutablePointer(unsafeAddressOf(key)), UnsafeMutablePointer(unsafeAddressOf(value)), 1, &keyCallBacks, &valueCallBacks)
return cfDictionary
}
Run Code Online (Sandbox Code Playgroud)
相当简单(有点傻)但它的功能返回和可选的CFDictionary.尝试从此函数创建NSDictionary时,"fun"开始:
以下为什么不工作?
if let problemDictionary = optionalProblemDictionary() as? NSDictionary {
print(problemDictionary) // never enters, no warnings, compiles just fine
}
Run Code Online (Sandbox Code Playgroud)
虽然这很好吗?
if let cfDictionary = optionalProblemDictionary() {
let problemDictionary = cfDictionary as NSDictionary
print(problemDictionary)
}
Run Code Online (Sandbox Code Playgroud)
XCode 7.0(7A220)
被认为基本上总是使用指定的初始化程序,我觉得创建新的viewInstances [UIView new];而不是[[UIView alloc] initWithFrame:CGRectZero];?
有没有理由不这样做?有什么实际的区别吗?如果你正在创建一个没有任何框架信息的新视图实例,那么回退就不会[UIView new];简单地删除大量的代码错误吗?我的猜测将是[[UIView alloc] initWithFrame:CGRectZero];实际上是引擎盖下反正叫(?)
嗨我的问题是关于两个精灵的旋转.当我触摸屏幕的右半部分时,旋转开始并移动sprite2和sprite3.如果我触摸屏幕的左半部分,旋转会停止,因为速度 - 速度= 0.如果我再次触摸左半部分,旋转开始.
但是,如果我触摸与当前旋转方向相对应的屏幕的一半,则速度会重复.我希望能够改变旋转方向,但速度保持不变.
视频演示了这个问题:http://youtu.be/HxLwl1QZiNM
import SpriteKit
class GameScene: SKScene {
let sprite = SKSpriteNode(imageNamed:"bWhite")
let sprite2 = SKSpriteNode(imageNamed:"bBlue")
let sprite3 = SKSpriteNode(imageNamed:"bRed")
let circle = SKSpriteNode(imageNamed:"bCircle")
override func didMoveToView(view: SKView) {
/* Setup your scene here */
backColor = SKColor(red: 0, green: 0, blue: 0, alpha: 1)
self.backgroundColor = backColor
sprite.setScale(1.25)
sprite2.setScale(1)
sprite3.setScale(1)
sprite.position = CGPointMake(self.frame.size.width/2, (self.frame.size.height/2)-200);
circle.position = CGPointMake(self.frame.size.width/2, (self.frame.size.height/2)-200);
sprite3.zRotation = 172.77
sprite2.anchorPoint = CGPointMake(-1.10, 0.5);
sprite3.anchorPoint = CGPointMake(-1.10, 0.5);
self.addChild(sprite)
self.addChild(circle)
sprite.addChild(sprite2)
sprite.addChild(sprite3)
} …Run Code Online (Sandbox Code Playgroud) swift ×3
ios ×2
objective-c ×2
alloc ×1
cfdictionary ×1
cgrect ×1
ios8 ×1
nsdictionary ×1
rotation ×1
segue ×1
sprite-kit ×1
swift2 ×1
uiview ×1