按下时UIButton会改变形状

时间:2013-09-17 13:30:10

标签: objective-c animation uibutton image-size

-(IBAction)actionButtonPressed:(id)sender {
//Write the code to show the buttons etc like play
[UIView animateWithDuration:0.5
                      delay:0.0
                    options:UIViewAnimationOptionAllowAnimatedContent
                 animations:^{
                     [self.actionBtn0 setFrame:CGRectMake(25, 25, 100, 100)];
                     [self.actionBtn1 setFrame:CGRectMake(40, 40, 70, 70)];
                     [self.actionBtn2 setFrame:CGRectMake(0, 0, 150, 150)];

                 } completion:^(BOOL finished) {
                     [UIView animateWithDuration:2.0
                                           delay:0.0
                                         options:UIViewAnimationOptionCurveLinear
                                      animations:^{
                                          playBtn.alpha = 1.0;
                                      }
                                      completion:NULL];
                 }];

}

我有一个按钮,当应用程序加载时,它很大并位于屏幕中央。然而,当它被按下时,我希望它缩小尺寸并移动到屏幕的右上角。实际上有3个按钮,但它们都共享上面显示的相同IBAction。

我遇到的问题是,根据按下按钮的时间,按钮的图像会变成随机奇怪的形状,我没有指定。图像应该是完美的圆形,但它们往往是椭圆形重绘。知道如何制止这个吗?

按下按钮前按钮的原始尺寸如下所示。

actionBtn0 = [[UIButton alloc] initWithFrame:CGRectMake(60, 185, 200, 200)];
[actionBtn0 setImage:[UIImage imageNamed:@"bigLineRing.png"] 
forState:UIControlStateNormal];
[actionBtn0 addTarget:self action:@selector(actionButtonPressed:) 
forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:actionBtn0];
[self.view bringSubviewToFront:actionBtn0];

actionBtn1 = [[UIButton alloc] initWithFrame:CGRectMake(90, 215, 140, 140)];
[actionBtn1 setImage:[UIImage imageNamed:@"smallLineRing.png"] 
forState:UIControlStateNormal];
[actionBtn1 addTarget:self action:@selector(actionButtonPressed:) 
forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:actionBtn1];
[self.view bringSubviewToFront:actionBtn1];

actionBtn2 = [[UIButton alloc] initWithFrame:CGRectMake(10, 135, 300, 300)];
[actionBtn2 setImage:[UIImage imageNamed:@"theEye.png"] forState:UIControlStateNormal];
[actionBtn2 addTarget:self action:@selector(actionButtonPressed:) 
forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:actionBtn2];
[self.view bringSubviewToFront:actionBtn2];

1 个答案:

答案 0 :(得分:0)

而不是使用这个UIButton的创建;

actionBtn1 = [[UIButton alloc] initWithFrame:CGRectMake(90, 215, 140, 140)];
[actionBtn1 setImage:[UIImage imageNamed:@"smallLineRing.png"] forState:UIControlStateNormal];

尝试使用它:

actionBtn1 = [UIButton buttonWithType:UIButtonTypeCustom];
actionBtn1.frame = CGRectMake(90, 215, 140, 140);
[actionBtn1 setImage:[UIImage imageNamed:@"smallLineRing.png"] forState:UIControlStateNormal];