7

私のアプリを iPhone 5 と以前の小さい iPhone の両方で動作させるのに苦労しています。iOS6 を要求して Autolayout を使用すると、うまく機能します。

ただし、アプリには定期的に位置を入れ替える 4 つのボタンがあるため、配列から各ボタンに異なる位置を割り当ててから、次のような方法で動きをアニメートします。

[UIView animateWithDuration:0.5 animations:^{
    button1.center = [[locations objectAtIndex:0] CGPointValue];
    button2.center = [[locations objectAtIndex:1] CGPointValue];
    button3.center = [[locations objectAtIndex:2] CGPointValue];
    button4.center = [[locations objectAtIndex:3] CGPointValue];
}];

これは Autolayout では機能しません。何も変わりません。私が得た最高のものは、位置にアニメーション化され、すぐに元に戻ることです.

ただし、Autolayout を削除すると、小さな iPhone ではすべてのボタンが押しつぶされ、iPhone 5 のサイズで設定されたポイントを使用しているため、下の行が画面から外れてしまいます。私は元々、Interface Builder から取得した異なる CGPoint 番号を持っていましたが、Autolayout が使用する番号だったので「間違っていた」と思っていましたが、それらは「間違っていた」ものでした。あなたが見ることができるように配列:

buttonLocations = [NSArray arrayWithObjects:
        [NSValue valueWithCGPoint:CGPointMake(57, 523)],
        [NSValue valueWithCGPoint:CGPointMake(109, 523)],
        [NSValue valueWithCGPoint:CGPointMake(57, 471)],
        [NSValue valueWithCGPoint:CGPointMake(109, 471)],
        nil];

この問題を解決するにはどうすればよいですか? サイズの異なるデバイスごとに異なるポイントを設定することは、あまり効率的ではないようです。

4

3 に答える 3

2

1つの方法は、自動レイアウトシステムを使用して、制約の「一定の」値をアニメーション化することです。以下のサンプルコードでは、横一列に3つのボタンがあり、それぞれがスーパービューへの前縁制約を持っています。これが私がアニメートするものです。side1、side2、およびside3は、これらの制約に対するIBOutletsであり、button1、button2、およびbutton3は、3つのボタンへのアウトレットです。

-(void)animateButton {
    [self.view removeConstraint:self.side1];
    [self.view removeConstraint:self.side2];
    [self.view removeConstraint:self.side3];

    NSLayoutConstraint *con1 = [NSLayoutConstraint constraintWithItem:self.button1 attribute:NSLayoutAttributeLeading relatedBy:0 toItem:self.view attribute:NSLayoutAttributeLeading multiplier:1 constant:114];
    NSLayoutConstraint *con2 = [NSLayoutConstraint constraintWithItem:self.button2 attribute:NSLayoutAttributeLeading relatedBy:0 toItem:self.view attribute:NSLayoutAttributeLeading multiplier:1 constant:213];
    NSLayoutConstraint *con3 = [NSLayoutConstraint constraintWithItem:self.button3 attribute:NSLayoutAttributeLeading relatedBy:0 toItem:self.view attribute:NSLayoutAttributeLeading multiplier:1 constant:20];

    [self.view addConstraints:@[con1,con2,con3]];
    [UIView animateWithDuration:2 animations:^{
        [self.view layoutIfNeeded];
    }];
}
于 2012-12-14T18:03:33.547 に答える
0

この問題は、コードを少し変更するだけで修正できます。ボタンの位置を交換する代わりに、レイアウトの制約を交換します。このカテゴリをインポートすることにより、cocoapod SBP を使用してレイアウトの制約を交換できます

    #import "UIViewController+SBP.h"

次に、このメソッドを使用して

    - (void)switchViewConst:(UIView*)firstView secondView:(UIView*)secondView durationInSeconds:(double)durationInSeconds

ここにビデオチュートリアルがあります。

于 2015-11-15T21:37:24.293 に答える