【问题标题】:Is it possible to pass existing methods to callbacks defined similar to UIView.animateWithDuration?是否可以将现有方法传递给定义类似于 UIView.animateWithDuration 的回调?
【发布时间】:2013-09-03 19:15:14
【问题描述】:

该方法最多接受两个回调,animationscomplete。如果我在类方法中定义了需要做什么,如何将它们传递给animateWithDuration

来自: https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIView_Class/UIView/UIView.html#//apple_ref/occ/clm/UIView/animateWithDuration:animations:

签名看起来像

+ (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;

【问题讨论】:

  • 附带说明,当您使用这样的错误参数时,您应该有一个 BOOL 返回类型。该方法的调用者检查成功/失败的返回值,以决定他们是否应该读取 registerError 变量。请参阅 NSError 编程指南...现在我认为可能是编译器警告...

标签: ios objective-c callback uiviewanimation


【解决方案1】:

只需从块内调用方法。

    [UIView animateWithDuration:0.4
                 animations:^{
                     [self yourMethod];
                     //...or
                     [YourClass yourMethod]; // as you said 'class' methods 
                 }
                 completion:^(BOOL finished) {
                     [self yourCompletion];
                 }];

【讨论】:

  • +1。是的,我可以做到。我以这种方法为例。我有自己的基于此类回调的类,我想知道是否可以避免此类子调用。
  • 我不明白你的意思
  • 签名有几个参数,看起来很不吸引人。最大的问题是NSError ** parameter,即向回调返回错误的能力。我将不得不为此创建一些类全局变量,而不是让它成为方法的本地变量。我会将其添加到问题中。
  • 这些参数将如何填写?我还是不明白。什么是丑陋的签名?你的方法,还是 animateWithDuration?也许在你的问题中写一些你希望它看起来像的示例代码。
  • 刚刚更新了问题。请在问题正文中查看我的签名。
【解决方案2】:

我也认为您的签名是错误的,并且您将对 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
}

【讨论】:

  • +1。是的,你是对的。我的签名是错误的。我自己也意识到了这一点。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-07
  • 2011-11-30
  • 1970-01-01
相关资源
最近更新 更多