【发布时间】:2012-09-14 18:57:11
【问题描述】:
理想情况下,我想编写代码,让我能够确定在执行某个功能时所需的视图执行哪些动画,例如(注,伪代码):
- (void)animateView:(UIView *)view withAnimations:(NSArray *)arrayOfAnimationBlocks
上述(即所需的)函数将依次执行一系列动画,并且在前一个动画完全执行之前不会执行每个动画。我还可以在运行时向arrayOfAnimationBlocks 添加和删除动画。
要做这样的事情,我正在尝试使用以下内容:
[UIView animateWithDuration:duration animations:animationBlock completion:completionBlock];
并且在调用函数时传递所有参数(duration、animationBlock、completionBlock)。
但是...
您似乎无法从动画块中访问self?我的动画块包含:
void (^animationBlock)(void) = ^
{
NSLog(@"[^animationBlock]");
[self.viewToAnimate setBounds:CGRectMake(self.viewToAnimate.bounds.origin.x, self.viewToAnimate.bounds.origin.y, self.viewToAnimate.bounds.size.width*2, self.viewToAnimate.bounds.size.height*2)];
};
我的完成块包含:
void (^completionBlock)(void) = ^
{
NSLog(@"[^completionBlock]");
[UIView animateWithDuration:duration animations:^{
[self.viewToAnimate setBounds:CGRectMake(self.viewToAnimate.bounds.origin.x, self.viewToAnimate.bounds.origin.y, self.viewToAnimate.bounds.size.width/2, self.viewToAnimate.bounds.size.height/2)];
} completion:^(BOOL finished){
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Animation Complete" message:@"The previous animations should be fully completed." delegate:self cancelButtonTitle:@"Dismiss" otherButtonTitles:nil];
alert.alertViewStyle = UIAlertViewStyleDefault;
[alert show];
}];
};
然后我当然有:
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 0) NSLog(@"Cancel pressed.");
else
{
NSLog(@"buttonIndex = %i", buttonIndex);
}
}
在animationBlock 和completionBlock Xcode 中都会出现以下红色错误:
(!) Use of undeclared identifier 'self'
【问题讨论】:
-
你在哪里声明这些块?
self只存在于方法内部;这是一个隐藏参数。 -
嗨乔希:这些在我的 UIViewController .m 文件中。我有一个名为 viewToAnimate 的属性,它通过 IBOutlet 连接到我的 UIViewController .m 文件。
标签: objective-c animation block