2011-12-01 4 views
0

UIButton의 너비를 왼쪽으로 만 사용하는 방법을 알고 싶습니다. 왼쪽으로 확장하는 앱 스토어 버튼과 거의 같습니다. 그러나 나는 여러 버튼을 사용합니다.UIButton 너비를 오른쪽에서 왼쪽으로 만 사용하는 방법

편집이이 작업 코드입니다 : 애니메이션

//viewdidload 
buttonCaption = [UIButton buttonWithType:UIButtonTypeCustom]; 
//default placement 
buttonCaption.frame = CGRectMake(245, 385, 70, 30); //default width size 
[self nextAnimation:buttonCaption.frame.size.width]; 

패스 기본값

-(void)nextAnimation:(float)previousWidth { 

//button setting 
buttonCaption.titleLabel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin ; 
//buttonCaption.titleLabel.font =[UIFont systemFontOfSize:20]; 
[[buttonCaption layer] setCornerRadius:5.0f]; 
[[buttonCaption layer] setMasksToBounds:YES]; 
[[buttonCaption layer] setBackgroundColor:[[UIColor colorWithRed:0 green:0 blue:0 alpha:0.7] CGColor]]; 
[buttonCaption.titleLabel setFrame:CGRectMake(0,0, 25, 25)]; 
CGSize stringsize = [tempCaption sizeWithFont:[UIFont systemFontOfSize:20]]; 
CGFloat diff = stringsize.width - previousWidth; 
NSLog(@"diff %f",diff); 



[UIView animateWithDuration:0.5 

        animations:^{ 
        [buttonCaption setFrame:CGRectMake(buttonCaption.frame.origin.x-diff, 
                 buttonCaption.frame.origin.y, 
                 buttonCaption.frame.size.width + diff, 
                 buttonCaption.frame.size.height)];     


        } 
        completion:^(BOOL completed) 
       { 
[self nextAnimation:stringsize.width]; 
       } 
    ]; 
} 
당신은 이전의 폭과 새로운 폭의 차이를 필요

답변

1

[UIView animateWithDuration:0.5f 
       animations:^{ 
        float kOfset = 0.25f; 
        CGFloat newSizeX = buttonCaption.frame.size.width*(1+kOfset); 
        CGFloat ofsetX = buttonCaption.frame.size.width*kOfset; 
        [buttonCaption setFrame:CGRectMake(buttonCaption.frame.origin.x-ofsetX, buttonCaption.frame.origin.y, newSizeX, buttonCaption.frame.size.height)];     
       } 
       completion:^(BOOL finished){ }]; 

변경 kOfset 당신이 원하는 가치를이 블록 애니메이션 코드를 사용해보십시오 (이것은 %의 크기입니다).

+0

감사 @beryllium, 오른쪽에서 왼쪽으로 코드를 소비하지만, text.width가 작을 때 다시 크기가 조정되지 않습니다. 코드가 나를 이해하는 데 어떻게 도움이되는지 설명해 주시겠습니까? – Desmond

+0

그냥 새 프레임을 설정했습니다. [buttonCaption setFrame : ... – beryllium

+0

새 프레임을 다시 크기를 설정 하시겠습니까? 난 여기에서 값을 가지고 5 다른 문자열 크기가 CGSize stringsize = [tempCaption sizeWithFont : [UIFont systemFontOfSize : 20]]; 일부는 다른 것보다 길 수도 있으므로 동적으로 크기를 조정해야합니다. – Desmond

0
[buttonCaption setFrame:CGRectMake(110-2,385,stringsize.width +5, stringsize.height)]; 
+0

죄송합니다. 와트가 아니므로 고맙겠습니다. – Desmond

1

-이 전화 widthChange.

buttonCaption.frame = newFrame; 

는 기본적으로, 당신은 당신이 확장하는 동시에 왼쪽 X 원점을 이동해야합니다

CGRect newFrame = buttonCaption.frame; 
newFrame.origin.x -= widthChange; 
newFrame.size.width += widthChange; 

지금이 당신이 애니메이션 유일한 일이 다음과 같이 그런 다음, 새로운 프레임을 계산 폭.

+0

감사합니다 jrturton, 일부 샘플 코드를 공유 할 수 있습니까? – Desmond

+0

내 질문에있는 것 외에 다른 것을 추가 할 수 있는지 잘 모르겠습니다. 어느 부분에 문제가 있습니까? – jrturton

+0

감사합니다 jrturton, 그것을 가지고 작동 – Desmond

관련 문제