0

我正在尝试根据单独的类文件中发生的情况在一个视图中触发动画。它得到了方法,它确实吐出了两个NSLog语句,但没有提交UIView Animation

这是代码:

视图控制器.h

@interface ViewController : UIViewController {

}

-(void)closeMenu;

视图控制器.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;
    //}
    }

如果您需要从类外部访问它,这还不够,请使用属性。IE

头文件

@property (nonatomic, strong) ViewController* myViewController

.m 文件

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

那个动画代码真的很奇怪。您正在混合新旧 UIView 动画代码,我认为您不能这样做(但我可能是错的)。

由于您已经开始使用基于块的 API,我建议您走这条路线(Apple 推荐同样的方法)。

有一种与您使用的方法类似的方法,它采用名为animateWithDuration:delay:options:animations:completion:. 您可以传入 0 来表示延迟,并传入一个空块,该块需要 BOOL 来完成。

您要为选项传递的两个标志是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 回答