2017-01-30 48 views
0

我有一个动画,以编程方式在屏幕上向上移动三个按钮。但是,当我在iPhone 6或iPhone 4的模拟器上测试它时,按钮的位置都是错误的(但只适用于iPhone 5)。我该如何解决。这些按钮是以编程方式构建的,所以我无法真正使用自动布局将它们放置在视图控制器上。如何使用按钮动画实现自动布局

-(IBAction)Search:(id)sender { 

self.button.frame = CGRectMake(124, 475, 78, 72); 

self.buttonTwo.frame = CGRectMake(124, 475, 78, 76); 

self.buttonThree.frame = CGRectMake(124, 475, 78, 76); 

// animate 
[UIView animateWithDuration:0.75 animations:^{ 
    self.button.frame = CGRectMake(13, 403, 78, 72); 
    self.buttonTwo.frame = CGRectMake(124, 347, 78, 76); 
    self.buttonThree.frame = CGRectMake(232, 403, 78, 76); 
+0

为什么你不能使用autolayout?即使它是以编程方式创建的,你仍然可以定位它们。可以创建NSLayoutConstraints的实例。 – Joshua

回答

0

您对框架使用固定值,但屏幕宽度和/或高度不同。

如果它们是正确的iPhone 5-5s(也称为iPhone 4" ),然后处理它的方式是:

-(IBAction)Search:(id)sender { 

CGFloat screenWidth = self.view.bounds.size.width; 
CGFloat screenHeight = self.view.bounds.size.height; 

CGFloat normalizedX = (124/320) // You calculate these 'normalized' numbers, possibly from a designer's spec. 
            // it's the percent the amount should be over, as number from 0-1. 
            // This number is based on screen width of 320 having x 124 pt. 

CGFloat startingX = normalizedX * screenWidth; 
CGFloat startingY = (475/588) * screenHeight; 
CGFloat width = (78/320) * screenWidth; 
CGFloat height = (72/588) * screenHeight; 

CGRect startingRect = CGRectMake(startingX, startingY, width, height) 

self.button.frame = startingRect; 
self.buttonTwo.frame = startingRect; 
self.buttonThree.frame = startingRect; 

// animate 
[UIView animateWithDuration:0.75 animations:^{ 
    CGFloat firstX = (13/320) * screenWidth; 
    CGFloat lowerY = (403/588) * screenHeight; 
    self.button.frame = CGRectMake(firstX, lowerY, width, height); 

    CGFloat secondX = (124/320) * screenWidth; 
    CGFloat upperY = (347/588) * screenHeight; 
    self.buttonTwo.frame = CGRectMake(secondX, upperY, width, height); 

    CGFloat thirdX = (233/320) * ScreenWidth; 
    self.buttonThree.frame = CGRectMake(thirdX, lowerY, width, height); 
}]; 
} 

这将扩大一切行动的规模,并保持相对位置。注意:UIButtons会有相同的文字大小,你可以使用这些数字直到你得到想要的效果,希望这会有所帮助,

+0

我需要Objective-C中的这个。谢谢! – Elizabeth429

+0

这是相同的代码,只是次要语法chang ES。完成。 –