본문 바로가기

개발/App Developer

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:으로 설정한 버튼의 Edge의 margin을 일괄 조절했다
양의 정수만 유효하고
normalImage , highlightImage , disableImage가 재조정 되었다.
*/

imageBtn.imageEdgeInsets = UIEdgeInsetsMake(5,5,5,5);

/*
다음으로 배경을 설정 : setBackgroundImage:forState:
모두 프레임으로 설정한 사이즈에 재조정 된다.
*/
[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을 scaleX=1 , scaleX=1 , x=10, y=10, alpha=1으로 에니메이션

/*
UIView의 클래스오브젝트의 함수를 사용하여
우선 움직일 준비를 했다

디폴트 설정
에니메이션 시간설정
에니메이션 의 타입설정
(※EaseInOut , ElaseIn , EaseOut, Linear이렇게 4개 밖에 없지만 자기가 만들어도 되는것 같다.)
이 에니메이션의 이벤트를 얻기위해서 delegate을 자기로 설정
에니메이션 시작 이벤트의 selector설정
에니메이션 종료 이벤트의 selector설정
*/
[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];

이렇게 움직였다.

//설정한 두개의 selector도 테스트했다.
- (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의 레이블 명을 변경할 때 일반적인 마인드로

myButton.textLable.text = @"aaa";

이와 같이 입력하면 에러는 없으나 아무것도 출력되지 않는다.

 

왜냐 .. 상태 프로퍼티가 없어 그런 것이다.

르개서 아래와 같이 forState와 함께 써주어 야 한다.

 

[myButton setTilte:@"hello" forState:UIcontrolStateNormal];

[myButton setTitle:@"oh~" forState:UIControlStateHighLighted];

 

forState를 보시면 아시겠지만 일반상태는 UIControlStateNormal이고 눌렸을때의 상태는 UIControlStateHighlighted입니다.

 

쉽게 타이틀 바꾸기

if([self.Button(버튼변수명).titleLabel.text isEqualToString:@"변경"]){  // 버튼 타이틀이 변경이라면

self.Button(버튼변수명).titleLabel.text = @"변경완료";//변경완료로 바꿔라

}


 

 


'개발 > App Developer' 카테고리의 다른 글

UITableView 에 Header, Footer를 넣어보자  (0) 2010.09.01
제공 함수 도움말 바로 링크  (0) 2010.09.01
info.plist  (0) 2010.09.01
XCode info.plist 아이폰,아이팟터치 주요항목 정리  (0) 2010.09.01
SQLite  (0) 2010.09.01