티스토리 뷰

반응형

아이폰에서 안드로이드의 Tost 같은 효과를 내기위해 만들어 보았다.



1. UIView 이용하여 화면을 생성하고 Label 추가하였다.


-(void)createTostView

{

tostView = [[UIView alloc] initWithFrame:CGRectMake( 0, 0, 200, 40)];

tostView.center = CGPointMake(self.view.frame.size.width/2,

  (self.view.frame.size.height2)+(self.view.frame.size.height4));

[tostView setBackgroundColor:[UIColor whiteColor]];

[self.navigationController.view addSubview:tostView];

tostView.layer.cornerRadius = 8;    //뷰 라운드 효과 주기.

tostView.layer.borderColor = [UIColor lightGrayColor].CGColor;//테투리 색상

tostView.layer.borderWidth = 0.5f;//테두리 두께

tostView.layer.shadowColor = [UIColor blackColor].CGColor;

tostView.layer.shadowOffset = CGSizeMake( 2.5f, 3.5f);

tostView.layer.shadowOpacity = 0.4f;

tostView.layer.shadowRadius = 3.0f;

msgLabel = [[UILabel alloc] initWithFrame:CGRectMake(5, 5, tostView.frame.size.width-10, tostView.frame.size.height-10)];

[msgLabel setTextColor:[UIColor orangeColor]];

[msgLabel setFont:[UIFont systemFontOfSize:14]];

[msgLabel setTextAlignment:NSTextAlignmentCenter];

[msgLabel setNumberOfLines:0];

[msgLabel setAdjustsFontSizeToFitWidth:YES];

[tostView addSubview:msgLabel];

tostView.hidden = YES;

}




2. 애니메이션 효과 내기.


-(void)showTostMessage:(NSNotification *)noti

{

[self.view bringSubviewToFront:tostView];

NSString *msg = [noti object];

tostView.hidden = NO;

tostView.alpha = 0.0f;

msgLabel.text = msg;

[UIView animateWithDuration:1.0f animations:^{

tostView.alpha = 1.0f;

} completion:^(BOOL finished) {

[NSObject cancelPreviousPerformRequestsWithTarget:self];

[self performSelector:@selector(hideTostMessage) withObject:nil afterDelay:2.0f];

}];

}


-(void)hideTostMessage

{

[UIView animateWithDuration:1.0f animations:^{

tostView.alpha = 0.0f;

} completion:^(BOOL finished) {

tostView.alpha = 1.0f;

tostView.hidden = YES;

}];

}



3. 사용하기.


//먼저 호출될 함수 선언해주고.

- (void)viewDidLoad

{

[[NSNotificationCenter defaultCenter] addObserver:self

selector:@selector(showTostMessage:)

name:@"NOTI_TOST"

  object:nil];

}


//호출 주면 된다.

[[NSNotificationCenter defaultCenter] postNotificationName:NOTI_PUSH_MESSAGE

object:@"TOST 비스꼬름하게 가라"

  userInfo:nil];


반응형
댓글
반응형