我正在尝试根据单独的类文件中发生的情况在一个 View 中触发动画。它得到了方法,它确实吐出了两个 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;
//}
}
最佳答案
您将老式动画与基于 block 的动画混合在一起。例如,在文档中,它声明为 setAnimationBeginsFromCurrentState
:
Use of this method is discouraged in iOS 4.0 and later. Instead, you should use theanimateWithDuration:delay:options:animations:completion: method to specify your animations and the animation options.
我不是 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];
除此之外,它似乎应该有效。如果影响框架,它可能会导致其他问题。在动画 block 之前计算帧可能是值得的。阿拉:
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
中的对象,并在其上调用方法,但 View 在 View 层次结构中无处可寻。您需要在屏幕上显示的实例上调用动画。希望这是有道理的?
编辑 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];
关于objective-c - 动画没有通过另一个 View Controller 执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13001465/