0

我想知道如何将 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];
                }
  ]; 
}   
4

3 回答 3

1

您需要旧宽度和新宽度之间的差异——称之为widthChange。然后,按如下方式计算您的新框架:

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

现在这是您制作动画的唯一内容:

buttonCaption.frame = newFrame; 

基本上,您需要在扩展宽度的同时将 x 原点向左移动。

于 2011-12-01T07:26:59.877 回答
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为您想要的值(这是以 % 为单位的大小)。

于 2011-12-01T07:43:46.057 回答
0
[buttonCaption setFrame:CGRectMake(110-2,385,stringsize.width +5, stringsize.height)];
于 2011-12-01T07:14:46.777 回答