该方法最多可接受两个回调,animations
和complete
。如果我定义了在类方法中需要做什么,如何将它们传递给animateWithDuration
?
签名看起来像
+ (void)animateWithDuration:(NSTimeInterval)duration animations:(void (^)(void))animations
通常它看起来像:
[UIView animateWithDuration:0.2f
delay:0.0f
options:UIViewAnimationOptionCurveEaseIn
animations:^{
_someView.frame = CGRectMake(-150, -200, 460, 650);
_someView.alpha = 0.0;
}
completion:^(BOOL finished) {
}];
所以我要找的是为我班上的某个方法分配animations
。
选择此方法作为众所周知的示例。然而根本就是我有自己的类试图采用相同的方法,我想知道是否可以通过不在回调中调用类方法来保持代码清理。
我不喜欢的最重要的事情是NSError **
参数我用于我的回调,在出现错误时调用。子调用将需要某种全局变量,我真的不喜欢这样。所以这是我完整性的签名:
- (void)registerWithEmail:(NSString *)email
name:(NSString *)name
willRegister:(void (^)(void))willRegister
didRegister:(void (^)(void))didRegister
registerError:(void (^)(NSError**))registerError;
答案 0 :(得分:1)
只需在块内调用该方法即可。
[UIView animateWithDuration:0.4
animations:^{
[self yourMethod];
//...or
[YourClass yourMethod]; // as you said 'class' methods
}
completion:^(BOOL finished) {
[self yourCompletion];
}];
答案 1 :(得分:1)
我也认为您的签名是错误的,并且您正在混合使用块对NSError对象的间接引用。它应该是:
- (void) registerWithEmail:(NSString *)email
name:(NSString *)name
willRegister:(void (^)(void))willRegister
didRegister:(void (^)(void))didRegister
registerError:(void (^)(NSError*))registerError;
......你会这样称呼它:
[self registerWithEmail:@"email@email.com" name:@"dave" willRegister:^{
//.. will register
} didRegister:^{
//.. did register
} registerError:^(NSError *error) {
// do somethign with the error
}];
如果您想使用对NSError变量的间接引用,您的签名可能看起来更像:
- (BOOL)registerWithEmail:(NSString *)email
name:(NSString *)name
willRegister:(void (^)(void))willRegister
didRegister:(void (^)(void))didRegister
registerError:(NSError**)registerError;
..你会这样称呼它:
NSError *error = nil;
BOOL result = [self registerWithEmail:@"email@email.com" name:@"dave" willRegister:^{
//.. will register
} didRegister:^{
//.. did register
} registerError:&error];
if (!result && error) {
//Do somethign with the error
}