swiftをお試し
ちょいちょいswiftの日本語の説明があったので自分なりにサンプルを書いてみた。
コメントはObject-cとの比較
とりあえずHello World的なことはできたからヨシとする。
ドキュメントレベルなのでNDAもセーフな範囲かと。
テストはViewベースでSwiftで書いてみた。
import UIKit
class ViewController: UIViewController {
@IBOutlet var lblResult1 : UILabel //StoryBardにUITextを配置してドラッグして命名
@IBOutlet var lblResult2 : UILabel
@IBOutlet var lblResult3 : UILabel
@IBOutlet var lblResult4 : UILabel
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
startApp()//[self startApp];
}
func startApp (){//-(Void) startApp{
//varは変数
//letは定数
//変数をいろいろ定義してみる
var integerValue:Integer = 30 //明示的にInteger
var integerValueUnknown = 30 //整数は推論でInt
var intValue:Int = 30 //明示的にInt
var intValueUnknown = 30 //整数は推論でInt
let floatActive:Float = 12.0 //明示的にFloat
let doubleActiveUnknown = 12.0 //推論でDouble
let strMessage:String = "どらえもん" //明示的にString
let strMessageUnknown = "どらえもん" //推論でString
//明示的に宣言した場合最後に?をつけるとnilを容認する。
//?をつけない場合はnilは入れられない
//let result1:String = strMessage + String(intValue.intValue)
let result1 = strMessage + String(intValue)
let result2 = "答え:\(strMessage + String(intValue))"
//NSArrayならこんな感じ
var aryShoppingList: String [] = ["Eggs","Milk",]//Arrayの中の型をStringに固定
lblResult4.text = aryShoppingList[1]//[aryShopping objectAtIndex:0]
//verで書けばmutable letで書けばイミュータブル
let aryShoppingList2 = [intValue,floatActive,strMessage]//Arrayの中の型はid
//NSDictionaryならこんな感じ
var dicAirports: Dictionary <String,String> = ["TYO":"TOKYO","CTS":"CHITOSE"]
lblResult3.text = dicAirports["TYO"]//[dicAirports objectForKey:"TYO"]
let company = "apple"
var strMobile_os:String = ""
//NSstring *strMobile_os = nil;
//switch文サンプル
switch company {
case "google":
strMobile_os = "Android"
case "apple":
strMobile_os = "iOS"
default:
strMobile_os = "Unknown"
}
lblResult2.text = strMobile_os
//if文サンプル
var strResult:String = ""
if intValue >= 30{
strResult = "intValueは30以上"
}else{
strResult = "intValueは30未満"
}
lblResult1.text = result2
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
UIViewの簡単アニメーション
とりあえずUIViewをアニメーションしたい時はこれ。
下手に使うと他のアニメーションも巻き込まれるので注意。
UIViewのクラスメソッドを利用する。
//アニメーションに入る前にスタート地点にUIViewを配置しておく
[viewTarget setFrame:CGRectMake(0.0f, 0.0f, 100.0f, 100.0f)]; //位置とサイズ
CGFloat viewAngle = 0.0f; //回転角度
viewTarget.transform = CGAffineTransformMakeRotation
(viewAngle*(M_PI/180.0f));
▼移動して、移動終了時の処理もしたい時
[UIViewanimateWithDuration:1.0f//アニメーションにかける時間
animations:^{
//アニメの終了点の指定
[viewTarget setFrame:CGRectMake
(100.0f, 100.0f, 100.0f, 100.0f)];
CGFloat viewAngle = 170.0f; //回転角度
viewTarget.transform =
CGAffineTransformMakeRotation
(viewAngle*(M_PI/180.0f));
}
completion:^(BOOL finished){
//アニメーションが終了したときの処理
}];
//他にこんなクラスメソッドも用意されているので、使いたいオプションに応じて利用する
▼移動するだけ
[UIView animateWithDuration:(NSTimeInterval)
animations:^(void)animations];
▼移動する前の待機時間を設定、動きの加速減速設定、終了後の設定もしたい時
[UIView animateWithDuration:(NSTimeInterval)
delay:(NSTimeInterval)
options:(UIViewAnimationOptions)
animations:^(void)animations
completion:^(BOOL finished)completion];
▼上記に加えてポヨンポヨンしたい時
[UIView animateWithDuration:(NSTimeInterval)
delay:(NSTimeInterval)
usingSpringWithDamping:(CGFloat)
initialSpringVelocity:(CGFloat)
options:(UIViewAnimationOptions)
animations:^(void)animations
completion:^(BOOL finished)completion];
//各オプションの意味と設定値
Duration: アニメーションにかける時間(秒)
animations: アニメ終了時の座標や、回転などを設定(UIViewの設定値)
delay: アニメション開始までの待機時間(秒)
options: UIViewAnimationCurveで始まるアニメの加速減速の設定
(UIViewAnimationCurve定数)
UIViewAnimationCurveEaseIn 徐々に加速する
UIViewAnimationCurveEaseInOut 最初は加速、終了時に減速
UIViewAnimationCurveEaseOut 徐々に減速する
UIViewAnimationCurveLinear 一定速度でアニメーション
usingSpringWithDamping: 移動終端でぽよんぽよんする。(0.0f~1.0f)
0:ぽよんぽよん最大 1:ぽよんぽよん最小
initialSpringVelocity: バネの初速。(0.0f~1.0f)
良くわからん時は0にしとけ。
1にすると1秒かけて最初の移動するよ。
参考ページ
Delegateの実装
Delegateを実装したいときは、実装したいクラスに下記をコピペ。
Delegateを実装したいけど詳しいことはわからない。
そんなときは、呪文だと思って下記ルールに従って実装すればサクッと実装完了
動作を覚えてから、その意味を知るのも悪くはないと思う。
「CustomViewController」となっている部分は、実装したクラス名に置き換える。
Delegateの基本実装 .h
#import <UIKit/UIKit.h>
//▼この行追加
@protocol CustomViewControllerDelegate;
@interface CustomViewController : UIViewController
//▼この行追加
@property (nonatomic, assign) id < CustomViewControllerDelegate > delegate;
//公開メソッド、プロパティ実装場所
@end
//▼最終行まで追加
@protocol CustomViewControllerDelegate <NSObject>
@required
//必須DelagateMethods Delegateを受ける親クラスで実装しないと警告が出る。
- (void)CustomVC_ReqiredDelegate:(CustomViewController*)controller;
@optional
//オプショナルDelagateMethods Delegateを受ける親クラスで実装するしないは自由。
- (void)CustomVC_OptionalDelegate:(CustomViewController*)controller;
@end
Delegateの基本実装 .m
//Delegateを受けるメソッドが実装されているかどうか確認する
if ([_delegate respondsToSelector:@selector(CustomVC_ReqiredDelegate:)]) {
//Delegateを実際に発生させる。対象メソッドが実装されていないと落ちるので注意。[_delegate CustomVC_ReqiredDelegate:self];
}
iOS開発 基本レシピ
3.5inchiOS7(iphone,iPodTouch)Icon 120×120 57×57 114×114 80×80 29×29 58×58iTunesConnect 1024×1024
[self.view bringSubviewToFront:最前面に移動するView];[self.view sendSubviewToBack:最背面に移動するView];
UIViewの角丸(CALayerを使った角丸の方法)
QuartzCore.framework をプロジェクトに追加
#import <QuartzCore/QuartzCore.h>
//角丸にするview.layer.cornerRadius = 5; //角丸のサイズview.clipsToBounds = true;
//枠線をつける[view.layer setBorderWidth:1.0f]; //線の太さ
[view.layer setBorderColor:[[UIColor blueColor] CGColor]]; //線の色
参考ページ
more...