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

最佳答案

试试这个块动画代码

[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更改为所需的值(以%表示的大小)。

关于iphone - 如何仅从右向左扩展UIButton宽度,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/8337695/

10-12 01:32