2017-01-30 5 views
0

私は、プログラム上で3つのボタンを画面上に上に移動するアニメーションを持っています。しかし、私がiPhone 6または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); 
}]; 
} 

これは、最大のすべてのサイズを拡大し、相対的な位置を維持します。あなたが望む効果が得られるまで、これらの数字を使って遊ぶことができます。

+0

Objective-Cでこれが必要です。ありがとう! – Elizabeth429

+0

これは同じコードです。 es。完了しました。 –

関連する問題