- UIButtonに画像を設定し、動かす
XcodeのHelpは、Flashなんかと比べると
とても良くできていて好きですが、
英語だから、頭が痛いので、メモ
ここも参考に、UIButtonに画像を設定して、アニメーション
●UIButtonに画像を設定
//使用するUIImageインスタンス作成
UIImage *normalImage = [ UIImage imageNamed : @"normal.png"];
UIImage *highlightImage = [UIImage imageNamed : @"highlight.png"];
UIImage *disableImage = [UIImage imageNamed : @"disable"];
UIImage *normalBgImage = [ UIImage imageNamed : @"normal.png"];
UIImage *highlightBgImage = [UIImage imageNamed : @"highlight.png"];
UIImage *disableBgImage = [UIImage imageNamed : @"disable"];
/*
UIButtonインスタンス作成
UIButtonTypeは、UIButton.hに
構造体で、
typedef enum {
UIButtonTypeCustom = 0,
UIButtonTypeRoundedRect,
UIButtonTypeDetailDisclosure,
UIButtonTypeInfoLight,
UIButtonTypeInfoDark,
UIButtonTypeContactAdd,
} UIButtonType;
0〜5の6つ定義されていた
*/
UIButton *imageBtn = [UIButton buttonWithType : UIButtonTypeCustom ];
//一番背面の色(backgroundImageよりも後ろ)
imageBtn.backgroundColor = [UIColor blackColor];
//ボタンのサイズと位置を指定 x=0 , y=0に、w=100,h=100の大きさを指定した
imageBtn.frame = CGRectMake(0,0,100,100);
//非表示
imageBtn.alpha=0;
//0に指定するとエラーがでることがあったので、0.1とかして限りなく小さくした
imageBtn.transform=CGAffineTransformMake(0.1,0.1);
/*
メイン画像を設定 : setImage:forState:
画像はリサイズされずに表示された
*/
[imageBtn setImage : normalImage forState : UIControlStateNormal ];
[imageBtn setImage : highlightImage forState : UIControlStateHighlighted];
//imageBtn.enabled=NO
[imageBtn setImage : disableImage forState : UIControlStateDisabled];
/*
次に、setImage:forState:で指定したボタンのエッジのマージンを一括調整した
正の整数のみ有効で、
normalImage , highlightImage , disableImageがリサイズされた
*/
imageBtn.imageEdgeInsets = UIEdgeInsetsMake(5,5,5,5);
/*
次に、背景画像を設定 : setBackgroundImage:forState:
すべてframeで指定したサイズにリサイズされた
*/
[imageBtn setImage : normalImage forState : UIControlStateNormal ];
[imageBtn setImage : highlightImage forState : UIControlStateHighlighted];
[imageBtn setImage : disableImage forState : UIControlStateDisabled];
/*
ラベルを設定 : setTitle:forState:
setImage:forStateと共に、使用すると下に隠れて見えなかったけど、
*/
[imageBtn setTitle:@"normal" forState : UIControlStateNormal];
[imageBtn setTitle:@"press" forState : UIControlStateHighlighted];
[imageBtn setTitle:@"disable" forState : UIControlStateDisabled];
/*
setImage画像もしくはsetTitleのラベルの位置を調整した
Left Center , Right ,Top,Bottomと指定したら、それっぽく調整された
*/
imageBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
imageBtn.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
//で、自分が保持しているviewに追加した
[self.view addSubView:imageBtn];
これで、画像をUIButtonに設定できた
●次に、このUIButtonを、scaleX=1 , scaleX=1 , x=10,y=10,alpha=1にアニメーション
/*
UIViewのクラスオブジェクトのメソッドを使用し、
まず動かす準備をした
ディレイ設定、
アニメーション時間設定、
アニメーションのタイプ設定
(※EaseInOut , ElaseIn , EaseOut,Linearの4つしか無いけど自作できるらしい)
このアニメーションのイベントを取得するために、delegateを自分に設定、
アニメーション開始イベントのセレクタ設定、
アニメーション終了イベントのセレクタ設定
*/
[UIView beginAnimations:@"imageBtnID" , context:nil];
[UIView setAnimationDelay:0];
[UIView setAnimationDuration:1];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationDelegate:self];
[UIView setAnimationWillStartSelector : @selector(onAnimateStart:context:)];
[UIView setAnimationDidStopSelector : @selector(onAnimateEnd:finished:context)];
/*
次に目的の値に、btnImageの表示が変化するように設定するために、
移動とスケーリングに、アファイン変換を使用した
*/
btnImage.alpha=1;
btnImage.transform=CGAffineTransformTranslate(CGAffineTransformMakeScale(0,0) , 10,10);
//準備ができたので、アニメーションを要求した
[UIView commitAnimestions];
これで動いた
//2つの指定したdelegateしたセレクタもテストした
- (void) onAnimateStart : (NSString*)animationID context:(void *)nil{
NSLog(@"Start Animation > Animation ID : %@",animationID);
}
- (void) onAnimateEnd : (NSString*)animationID finished:(BOOL) context:(void *)nil{
NSLog(@"End Animation > Animation ID : %@",animationID)
}
とても良くできていて好きですが、
英語だから、頭が痛いので、メモ
ここも参考に、UIButtonに画像を設定して、アニメーション
●UIButtonに画像を設定
//使用するUIImageインスタンス作成
UIImage *normalImage = [ UIImage imageNamed : @"normal.png"];
UIImage *highlightImage = [UIImage imageNamed : @"highlight.png"];
UIImage *disableImage = [UIImage imageNamed : @"disable"];
UIImage *normalBgImage = [ UIImage imageNamed : @"normal.png"];
UIImage *highlightBgImage = [UIImage imageNamed : @"highlight.png"];
UIImage *disableBgImage = [UIImage imageNamed : @"disable"];
/*
UIButtonインスタンス作成
UIButtonTypeは、UIButton.hに
構造体で、
typedef enum {
UIButtonTypeCustom = 0,
UIButtonTypeRoundedRect,
UIButtonTypeDetailDisclosure,
UIButtonTypeInfoLight,
UIButtonTypeInfoDark,
UIButtonTypeContactAdd,
} UIButtonType;
0〜5の6つ定義されていた
*/
UIButton *imageBtn = [UIButton buttonWithType : UIButtonTypeCustom ];
//一番背面の色(backgroundImageよりも後ろ)
imageBtn.backgroundColor = [UIColor blackColor];
//ボタンのサイズと位置を指定 x=0 , y=0に、w=100,h=100の大きさを指定した
imageBtn.frame = CGRectMake(0,0,100,100);
//非表示
imageBtn.alpha=0;
//0に指定するとエラーがでることがあったので、0.1とかして限りなく小さくした
imageBtn.transform=CGAffineTransformMake(0.1,0.1);
/*
メイン画像を設定 : setImage:forState:
画像はリサイズされずに表示された
*/
[imageBtn setImage : normalImage forState : UIControlStateNormal ];
[imageBtn setImage : highlightImage forState : UIControlStateHighlighted];
//imageBtn.enabled=NO
[imageBtn setImage : disableImage forState : UIControlStateDisabled];
/*
次に、setImage:forState:で指定したボタンのエッジのマージンを一括調整した
正の整数のみ有効で、
normalImage , highlightImage , disableImageがリサイズされた
*/
imageBtn.imageEdgeInsets = UIEdgeInsetsMake(5,5,5,5);
/*
次に、背景画像を設定 : setBackgroundImage:forState:
すべてframeで指定したサイズにリサイズされた
*/
[imageBtn setImage : normalImage forState : UIControlStateNormal ];
[imageBtn setImage : highlightImage forState : UIControlStateHighlighted];
[imageBtn setImage : disableImage forState : UIControlStateDisabled];
/*
ラベルを設定 : setTitle:forState:
setImage:forStateと共に、使用すると下に隠れて見えなかったけど、
*/
[imageBtn setTitle:@"normal" forState : UIControlStateNormal];
[imageBtn setTitle:@"press" forState : UIControlStateHighlighted];
[imageBtn setTitle:@"disable" forState : UIControlStateDisabled];
/*
setImage画像もしくはsetTitleのラベルの位置を調整した
Left Center , Right ,Top,Bottomと指定したら、それっぽく調整された
*/
imageBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
imageBtn.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
//で、自分が保持しているviewに追加した
[self.view addSubView:imageBtn];
これで、画像をUIButtonに設定できた
●次に、このUIButtonを、scaleX=1 , scaleX=1 , x=10,y=10,alpha=1にアニメーション
/*
UIViewのクラスオブジェクトのメソッドを使用し、
まず動かす準備をした
ディレイ設定、
アニメーション時間設定、
アニメーションのタイプ設定
(※EaseInOut , ElaseIn , EaseOut,Linearの4つしか無いけど自作できるらしい)
このアニメーションのイベントを取得するために、delegateを自分に設定、
アニメーション開始イベントのセレクタ設定、
アニメーション終了イベントのセレクタ設定
*/
[UIView beginAnimations:@"imageBtnID" , context:nil];
[UIView setAnimationDelay:0];
[UIView setAnimationDuration:1];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationDelegate:self];
[UIView setAnimationWillStartSelector : @selector(onAnimateStart:context:)];
[UIView setAnimationDidStopSelector : @selector(onAnimateEnd:finished:context)];
/*
次に目的の値に、btnImageの表示が変化するように設定するために、
移動とスケーリングに、アファイン変換を使用した
*/
btnImage.alpha=1;
btnImage.transform=CGAffineTransformTranslate(CGAffineTransformMakeScale(0,0) , 10,10);
//準備ができたので、アニメーションを要求した
[UIView commitAnimestions];
これで動いた
//2つの指定したdelegateしたセレクタもテストした
- (void) onAnimateStart : (NSString*)animationID context:(void *)nil{
NSLog(@"Start Animation > Animation ID : %@",animationID);
}
- (void) onAnimateEnd : (NSString*)animationID finished:(BOOL) context:(void *)nil{
NSLog(@"End Animation > Animation ID : %@",animationID)
}
Trackback : 0 : UIButtonに画像を設定し、動かす
http://www.onmyownlife.com/mt/mt-tb.cgi/55
Comment