@swift
@フォロワー #0
Registering or the permissions granted for Push Notifications in Xcode 8/Swift 3.0 @Notifications @swift @iOS
lujian 質問投稿 • 2017-05-23 17:02 • @フォロワー #0 • 0 回答 • 4236 Views
[Swift]UINavigationBarのカスタマイズ方法について @swift
iQi @回答 • 2015-09-29 16:17 • @フォロワー #0 • 1 回答 • 2121 Views
[Swift]UINavigationBarのheight(幅の高さ)を調整する方法について @swift
iQi @回答 • 2015-09-29 16:11 • @フォロワー #0 • 1 回答 • 2919 Views
[iOS]Swift navigation bar : remove left, right icon space @swift @iOS
Lubo @回答 • 2015-06-20 16:38 • @フォロワー #4 • 2 回答 • 1883 Views
[iOS]Swift1.2で発生したエラーメソッドをoverrideできない @iOS @code @swift
iQi @回答 • 2015-06-12 15:19 • @フォロワー #2 • 1 回答 • 3026 Views
[iOS]Swift1.2で発生したエラーUTF16Countが使えない @code @iOS @swift
iQi @回答 • 2015-06-12 15:17 • @フォロワー #2 • 1 回答 • 3048 Views
[iOS]Swift1.2で発生したエラーCannot invoke 'split' with an argument list of type @iOS @swift
iQi @回答 • 2015-06-12 15:16 • @フォロワー #2 • 1 回答 • 1781 Views
[iOS]swift NSInternalInconsistencyExceptionエラー @iOS @swift
iQi @回答 • 2015-06-07 15:30 • @フォロワー #2 • 1 回答 • 2054 Views
[iOS]Swift String 大文字、小文字について @code @swift
iQi @回答 • 2015-05-29 17:01 • @フォロワー #2 • 1 回答 • 2311 Views
[iOS]SwiftでのMD5メソッドについて @MD5 @code @swift
iQi @回答 • 2015-05-29 14:22 • @フォロワー #2 • 1 回答 • 4041 Views
{{{
var leftNegativeSpacer: UIBarButtonItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FixedSpace, targe...
var leftNegativeSpacer: UIBarButtonItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FixedSpace, target: nil, action: nil)
leftNegativeSpacer.width = -16.0
self.navigationItem.leftBarButtonItems = [leftNegativeSpacer, leftBarButton]
var rightNegativeSpacer: UIBarButtonItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FixedSpace, target: nil, action: nil)
rightNegativeSpacer.width = -6.0
self.navigationItem.rightBarButtonItems = [rightNegativeSpacer, rightBarButton]
}}}
Swiftの場合:
{{{
var audioSession: AVAudioSession = AVAudioSession.sharedInstance()
audioSession.setCategory(AVAudioSessionCategoryPl...
{{{
var audioSession: AVAudioSession = AVAudioSession.sharedInstance()
audioSession.setCategory(AVAudioSessionCategoryPlayAndRecord, error: nil)
audioSession.setActive(true, error: nil)
}}}
Objective-Cの場合:
{{{
AVAudioSession* session = [AVAudioSession sharedInstance];
[audioSession setCategory:AVAudioSessionCategoryPlayback error:&error];
[audioSession setActive:YES error:nil];
}}}
>>>import AVFoundation
{{{
enum ProductCategory : String {
case Washers = "washers", Dryers = "dryers", Toasters = "toaster...
enum ProductCategory : String {
case Washers = "washers", Dryers = "dryers", Toasters = "toasters"
static let allValues = [Washers, Dryers, Toasters]
}
for category in ProductCategory.allValues{
//Do something
}
}}}
// Add extension
{{{
extension UILabel {
func setSizeFont (sizeFont: CGFloat) {
self.font = UIFont(name: self.font.fo...
{{{
extension UILabel {
func setSizeFont (sizeFont: CGFloat) {
self.font = UIFont(name: self.font.fontName, size: sizeFont)!
self.sizeToFit()
}
}
}}}
// Use
{{{
myLabel.setSizeFont(20)
}}}
{{{
extension String {
//readonly computed property
var length: Int
{
return countElements(self)
}
}
}}}
extension String {
//readonly computed property
var length: Int
{
return countElements(self)
}
}
}}}
A bridging header with 「#import <CommonCrypto/CommonCrypto.h> 」が必要です。
{{{
extension String {
var md5: String! {
...
{{{
extension String {
var md5: String! {
let str = self.cStringUsingEncoding(NSUTF8StringEncoding)
let strLen = CC_LONG(self.lengthOfBytesUsingEncoding(NSUTF8StringEncoding))
let digestLen = Int(CC_MD5_DIGEST_LENGTH)
let result = UnsafeMutablePointer<CUnsignedChar>.alloc(digestLen)
CC_MD5(str!, strLen, result)
var hash = NSMutableString()
for i in 0..<digestLen {
hash.appendFormat("%02x", result[i])
}
result.dealloc(digestLen)
return String(format: hash)
}
}
}}}
または、
{{{
extension Int {
func hexString() -> String {
return NSString(format:"%02x", self)
}
}
extension NSData {
func hexString() -> String {
var string = String()
for i in UnsafeBufferPointer<UInt8>(start: UnsafeMutablePointer<UInt8>(bytes), count: length) {
string += Int(i).hexString()
}
return string
}
func MD5() -> NSData {
let result = NSMutableData(length: Int(CC_MD5_DIGEST_LENGTH))!
CC_MD5(bytes, CC_LONG(length), UnsafeMutablePointer<UInt8>(result.mutableBytes))
return NSData(data: result)
}
func SHA1() -> NSData {
let result = NSMutableData(length: Int(CC_SHA1_DIGEST_LENGTH))!
CC_SHA1(bytes, CC_LONG(length), UnsafeMutablePointer<UInt8>(result.mutableBytes))
return NSData(data: result)
}
}
extension String {
func MD5() -> String {
return (self as NSString).dataUsingEncoding(NSUTF8StringEncoding)!.MD5().hexString()
}
func SHA1() -> String {
return (self as NSString).dataUsingEncoding(NSUTF8StringEncoding)!.SHA1().hexString()
}
}
}}}
{{{
if( count(self.textView.text.utf16) == 0 ){...
}}}
if( count(self.textView.text.utf16) == 0 ){...
}}}
{{{
try:
var setText:String {
get {
return self.text
}
set {
self.text = newValue
}
}
}}}
try:
var setText:String {
get {
return self.text
}
set {
self.text = newValue
}
}
}}}
FlappyBirdサンプルのソースは下記となります。
https://github.com/gscalzo/FlappySwift
AppDelegate.swiftなどもあり、ファイル数は5つくらいのSwiftファイルとなっています。全体の動きをつかむ...
https://github.com/gscalzo/FlappySwift
AppDelegate.swiftなどもあり、ファイル数は5つくらいのSwiftファイルとなっています。全体の動きをつかむのも難しくなさそうです。
コードをいくら見ていても実際に自分が作りたいものとは厚くて高い壁があるものです。実際に動くソースを見て、そこから学んでいくのはいかがでしょう。
FlappySwiftはiOS用、MIT Licenseのオープンソース・ソフトウェアです。
GameScene.swift 一部抜粋
{{{
//
// GameScene.swift
// FlappyBird
//
// Created by Nate Murray on 6/2/14.
// Copyright (c) 2014 Fullstack.io. All rights reserved.
//
import SpriteKit
class GameScene: SKScene {
var bird = SKSpriteNode()
var skyColor = SKColor()
var verticalPipeGap = 150.0
var pipeTextureUp = SKTexture()
var pipeTextureDown = SKTexture()
var movePipesAndRemove = SKAction()
override func didMoveToView(view: SKView) {
// setup physics
self.physicsWorld.gravity = CGVectorMake( 0.0, -5.0 )
// setup background color
skyColor = SKColor(red: 81.0/255.0, green: 192.0/255.0, blue: 201.0/255.0, alpha: 1.0)
self.backgroundColor = skyColor
// ground
var groundTexture = SKTexture(imageNamed: "land")
groundTexture.filteringMode = SKTextureFilteringMode.Nearest
var moveGroundSprite = SKAction.moveByX(-groundTexture.size().width * 2.0, y: 0, duration: NSTimeInterval(0.02 * groundTexture.size().width * 2.0))
var resetGroundSprite = SKAction.moveByX(groundTexture.size().width * 2.0, y: 0, duration: 0.0)
var moveGroundSpritesForever = SKAction.repeatActionForever(SKAction.sequence([moveGroundSprite,resetGroundSprite]))
for var i:CGFloat = 0; i < 2.0 + self.frame.size.width / ( groundTexture.size().width * 2.0 ); ++i {
var sprite = SKSpriteNode(texture: groundTexture)
sprite.setScale(2.0)
sprite.position = CGPointMake(i * sprite.size.width, sprite.size.height / 2.0)
sprite.runAction(moveGroundSpritesForever)
self.addChild(sprite)
}
// skyline
var skyTexture = SKTexture(imageNamed: "sky")
skyTexture.filteringMode = SKTextureFilteringMode.Nearest
var moveSkySprite = SKAction.moveByX(-skyTexture.size().width * 2.0, y: 0, duration: NSTimeInterval(0.1 * skyTexture.size().width * 2.0))
var resetSkySprite = SKAction.moveByX(skyTexture.size().width * 2.0, y: 0, duration: 0.0)
var moveSkySpritesForever = SKAction.repeatActionForever(SKAction.sequence([moveSkySprite,resetSkySprite]))
for var i:CGFloat = 0; i < 2.0 + self.frame.size.width / ( skyTexture.size().width * 2.0 ); ++i {
var sprite = SKSpriteNode(texture: skyTexture)
sprite.setScale(2.0)
sprite.zPosition = -20;
sprite.position = CGPointMake(i * sprite.size.width, sprite.size.height / 2.0 + groundTexture.size().height * 2.0)
sprite.runAction(moveSkySpritesForever)
self.addChild(sprite)
}
・・・
}}}
「cell.text」はiOS8以降、使えなくなぅったので、「cell.textLabel.text」を使ってください。
{{{
func tableView(tableView: UITableView, cellForRowAtIndexPath ind...
{{{
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
// Cellの.を取得する.
let cell = tableView.dequeueReusableCellWithIdentifier("MyCell", forIndexPath: indexPath) as UITableViewCell
// Cellに値を設定する.
cell.textLabel?.text = "\(myItems[indexPath.row])"
return cell
}
}}}