Objective c 未通过另一个视图控制器执行动画
我试图在一个视图中触发一个动画,基于在一个单独的类文件中发生的事情。它得到了方法的支持,因为它确实抛出了两条Objective c 未通过另一个视图控制器执行动画,objective-c,ios,cocoa-touch,Objective C,Ios,Cocoa Touch,我试图在一个视图中触发一个动画,基于在一个单独的类文件中发生的事情。它得到了方法的支持,因为它确实抛出了两条NSLog语句,但没有提交UIView动画 代码如下: ViewController.h @interface ViewController : UIViewController { } -(void)closeMenu; ViewController.m -(void)closeMenu{ //close the menu NSLog(@"Got here");
NSLog
语句,但没有提交UIView动画
代码如下:
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");
}
m(注释代码可能与这个问题无关,当然在实际应用中仍然使用。Jut认为这可能会使理解更容易)
您正在混合老式动画和基于块的动画。例如,在文档中,它为setAnimationBeginsFromCurrentState
:
iOS 4.0及更高版本不鼓励使用此方法。相反,你
应使用动画和持续时间:延迟:选项:动画:完成:
方法指定动画和动画选项
我不能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 subitemdichange:(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
// Use it as such
[self.myViewController closeMenu];
.m文件
@property (nonatomic, strong) ViewController* myViewController
// Use it as such
[self.myViewController closeMenu];
那个动画代码真的很奇怪。你混合了新的和旧的UIView动画代码,我认为你做不到(但我可能错了)
既然您已经开始使用基于块的API,我建议您也这样做(苹果也建议这样做)
有一种与您使用的方法类似的方法,它采用名为animateWithDuration:delay:options:animations:completion:
的选项。您可以为延迟传递0,为完成传递一个BOOL的空块
要为选项传递的两个标志是UIViewAnimationOptionBeginFromCurrentState
和UIViewAnimationOptionCurveEaseInOut
您的代码看起来像这样
[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){}];
你说它不承诺是什么意思?你是说动画没有发生?在动画中设置menuView帧的行上设置断点。到达断点了吗?menuView是否为非零?@rmaddy是的,动画不会发生。已到达断点且menuView为非nil,我已使用IBAre创建。您确定它已在Interface builder中连接吗?@Wrightsc是的,如果我在ViewController.m中调用[self closeMenu]
,此无效方法有效。但通过这种方式,它不会t@rmaddy更新:当从另一个类调用menuView时,它是nil。但是当从它自己的类调用它时效果很好…我如何在实例中调用动画。。。?(这似乎是在正确的轨道上,但你能编辑你的答案与如何做)将不胜感激!谢谢@SirKaydian添加了一些关于将它们保存在实例变量中的内容,这可能就是问题所在;如果看不到它被使用的全部背景,很难判断。实际上,之前的代码是在创建一个新实例,而不是使用以前创建的实例。但这也不起作用。也许我不清楚。我有ViewController的xib文件,我想从另一个类文件中设置它的动画。达到了预期的效果。但在这两种情况下,它仍然无法设置视图的动画