0

別のクラス ファイルで何が起こっているかに基づいて、1 つのビューでアニメーションをトリガーしようとしています。メソッドにたどり着きます。これは、2 つのステートメントを吐き出しますが、NSLogコミットはしないという事実によって裏付けられています。UIView Animation

コードは次のとおりです。

ViewController.h

@interface ViewController : UIViewController {

}

-(void)closeMenu;

ViewController.m

-(void)closeMenu{

    //close the menu
    NSLog(@"Got here");

    [UIView animateWithDuration:0.6 animations:^{

        [UIView setAnimationBeginsFromCurrentState:YES]; // so it doesn't cut randomly, begins from where it is

        [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];

        [menuView setFrame:CGRectMake(menuView.frame.origin.x, -menuView.frame.size.height, menuView.frame.size.width, menuView.frame.size.height)];

    }];


    NSLog(@"Got here2");

}

OtherClass.m (コメントされたコードはこの質問とは無関係かもしれません。もちろん、実際のアプリケーションではまだ使用されています。理解を容易にするかもしれないと思いました)

#import "ViewController.h"

...

//- (void) item:(SDGroupCell *)item subItemDidChange:(SDSelectableCell *)subItem
//{
    ViewController *foo = [[[ViewController alloc] init] autorelease];

    //SelectableCellState state = subItem.selectableCellState;
    //NSIndexPath *indexPath = [item.subTable indexPathForCell:subItem];
    //switch (state) {
        //case Checked:
            //NSLog(@"Changed Sub Item at indexPath:%@ to state \"Checked\"", indexPath);

            //close the menuView

            [foo closeMenu];

            //break;
        //case Unchecked:
            //NSLog(@"Changed Sub Item at indexPath:%@ to state \"Unchecked\"", indexPath);
            //break;
        //default:
            //break;
    //}
}
4

2 に答える 2

1

昔ながらのアニメーションとブロック ベースのアニメーションを組み合わせています。たとえば、ドキュメントでは、次のように述べていsetAnimationBeginsFromCurrentStateます。

このメソッドの使用は、iOS 4.0 以降ではお勧めできません。代わりに、animateWithDuration:delay:options:animations:completion: メソッドを使用して、アニメーションとアニメーション オプションを指定する必要があります。

これがサポートされているかどうかは 100% わかりません。アニメーション コードを少なくとも次のように変更する必要があります。

[UIView animateWithDuration:0.6
                      delay:0.0
                    options:UIViewAnimationOptionBeginFromCurrentState|UIViewAnimationOptionCurveEaseInOut  
                 animations:^{
    [menuView setFrame:CGRectMake(menuView.frame.origin.x, -menuView.frame.size.height, menuView.frame.size.width, menuView.frame.size.height)];

                            }
                 completion:nil];

それとは別に、それはうまくいくようです。フレームに影響を与えると、何か問題が発生する可能性があります。アニメーション ブロックの前にフレームを計算する価値があるかもしれません。アラ:

CGRect newFrame = CGRectMake(menuView.frame.origin.x, -menuView.frame.size.height, menuView.frame.size.width, menuView.frame.size.height)

[UIView animateWithDuration...:^{ menuView.frame = newFrame; }...];

編集:ちょっと待って、でオブジェクトを割り当て/初期化し、(void) item:(SDGroupCell *)item subItemDidChange:(SDSelectableCell *)subItemそのメソッドを呼び出しているように見えますが、ビューはビュー階層のどこにもありません。画面に表示されているインスタンスでアニメーションを呼び出す必要があります。それが理にかなっていることを願っていますか?

編集 2:既に表示されているインスタンスで呼び出すには、通常、インスタンス変数に格納する必要があります。あなたの状況でどのように正確に言うことはできませんが、一般的には次の形式になります。

@interface OtherClass () {
    ViewController* m_viewController;
}
@end

....

- (void)viewDidLoad // Or where ever you first create your view controller
{
    ...
    // If you're adding a ViewController within another ViewController, you probably need View Controller Containment
    m_viewController = [[ViewController alloc] init];
    [self addChildViewController:m_viewController];
    [m_viewController didMoveToParentViewController:self];
    [self.view addSubview:m_viewController.view];
    ...
}

// If you're using ARC code, the dealloc wouldn't typically be necessary
- (void)dealloc
{
    [m_viewController release];
    [super dealloc];
}

//- (void) item:(SDGroupCell *)item subItemDidChange:(SDSelectableCell *)subItem
//{
    //SelectableCellState state = subItem.selectableCellState;
    //NSIndexPath *indexPath = [item.subTable indexPathForCell:subItem];
    //switch (state) {
        //case Checked:
            //NSLog(@"Changed Sub Item at indexPath:%@ to state \"Checked\"", indexPath);

            //close the menuView

            [m_viewController closeMenu];

            //break;
        //case Unchecked:
            //NSLog(@"Changed Sub Item at indexPath:%@ to state \"Unchecked\"", indexPath);
            //break;
        //default:
            //break;
    //}
    }

クラス外からアクセスする必要がある場合は、これでは不十分です。そのためにはプロパティを使用してください。いえ

ヘッダー ファイル

@property (nonatomic, strong) ViewController* myViewController

.m ファイル

// Use it as such
[self.myViewController closeMenu];
于 2012-10-21T20:45:49.753 に答える
1

そのアニメーション コードは本当に奇妙です。あなたは新しい UIView アニメーション コードと古い UIView アニメーション コードを混在させています。

ブロックベースの API を使い始めたので、その方法をお勧めします (Apple も同じことを推奨しています)。

と呼ばれるオプションを取る、使用したものと同様の方法がありますanimateWithDuration:delay:options:animations:completion:。遅延には 0 を、完了には BOOL を受け取る空のブロックを渡すことができます。

オプションに渡したい 2 つのフラグはUIViewAnimationOptionBeginFromCurrentStateUIViewAnimationOptionCurveEaseInOutです。

あなたのコードは次のようになります

[UIView animateWithDuration:0.6 
                      delay:0.0
                    options:UIViewAnimationOptionBeginFromCurrentState | UIViewAnimationOptionCurveEaseInOut  
                 animations:^{
                    // set your frame here...
                    [menuView setFrame:CGRectMake(menuView.frame.origin.x,
                                                  -menuView.frame.size.height,
                                                  menuView.frame.size.width,
                                                  menuView.frame.size.height)];
               } completion:^(BOOL finished){}];
于 2012-10-21T20:47:41.227 に答える