ios - 删除委托(delegate)方法的 2 个实现中的重复代码

标签 ios objective-c uitableview delegates uiscrollviewdelegate

我有一个 ViewController 类和一个 View 类。 ViewController 有一个 UIScrollView,View 有一个 UITableView,两者都是 UIScrollViewDelegate 的委托(delegate)。

View 和 ViewController 都使用了 scrollViewDidScroll: 委托(delegate)方法,并且都具有相同的代码。这显然是重复的,所以我想解决这个问题。

问题是我不知道怎么做。

我无法将功能提取到基类中,我也不认为我可以有一个单独的类来实现该方法,然后我可以使用它的实例(在 Android 中可以,但在 iOS 中不行)。我正在考虑使用 block ,但不知道它们在这里如何工作。

该方法本身对 ViewController/View 的 UI 执行一些更改;这是它的主体:

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {

    float multiplierYAnimationStop = [Utilities getFloatValueFromPlistFile:@"layout_values" forDictionaryKey:@"scroll_background_y_stop_multiplier"];
    float currentScrollOffsetY = scrollView.contentOffset.y;

    // No case for further back than the bottom of the screen (lower than 0)
    // and if it's higher than where it should stop, keep it at that point
    if (currentScrollOffsetY > screenHeight * multiplierYAnimationStop) {

        currentScrollOffsetY = screenHeight * multiplierYAnimationStop;
    }

    // Scale the background
    float newBackgroundScale = 1 - currentScrollOffsetY / screenHeight;

    if (newBackgroundScale < 0.75f) {

        newBackgroundScale = 0.75f;
    }

    [self scaleBackgroundToNewScale:newBackgroundScale];

    // Move the UILabel with the title and the Button
    float newLabelCenterY = originalLabelTitleCentreY - currentScrollOffsetY;
    float newButtonCenterY = originalButtonDownArrowCentreY - currentScrollOffsetY;
    self.labelHeadline.center = CGPointMake(self.labelHeadline.center.x, newLabelCenterY);
    self.buttonDownArrow.center = CGPointMake(self.buttonDownArrow.center.x, newButtonCenterY);

    // Blur the UILabel with the title and the Button
    float newHeadlineAlpha = 1 - currentScrollOffsetY / 100.0f;

    if (newHeadlineAlpha < 0.0f) {

        newHeadlineAlpha = 0.0f;
    }

    [self.labelHeadline setAlpha:newHeadlineAlpha];
    [self.buttonDownArrow setAlpha:newHeadlineAlpha];

    if (newHeadlineAlpha < 0.95f) {

        [self.buttonDownArrow setEnabled:NO];
    }
    else {

        [self.buttonDownArrow setEnabled:YES];
    }

    // Set the new alpha for the background overlay
    // (no bigger than scroll_overlay_max_opacity, should be scroll_overlay_max_opacity once the offset hits the stop point)
    float maxOpacity = [Utilities getFloatValueFromPlistFile:@"layout_values" forDictionaryKey:@"scroll_overlay_max_opacity"];
    float subtractionFactor = 1.0f - maxOpacity;

    float newOverlayAlpha = currentScrollOffsetY / screenHeight / multiplierYAnimationStop - subtractionFactor;

    if (newOverlayAlpha > maxOpacity) {

        newOverlayAlpha = maxOpacity;
    }

    [self.viewOverlay setAlpha:newOverlayAlpha];

    // If set, move the background vertically on scroll
    if ([Utilities getBoolValueFromPlistFile:@"layout_values" forDictionaryKey:@"is_scroll_background_movable"]) {

        if (newBackgroundScale == 0.75f) {

            self.imageBackground.center = CGPointMake(self.imageBackground.center.x, self.imageBackground.center.y - (currentScrollOffsetY - 0.25f * screenHeight));
        }
    }
}

最佳答案

创建一个封装您想要的内容的函数,并将任何需要的变量(可能包括 self)传递给它。

如果您的对象之间有更多重复的逻辑,您可以将其合并到一个策略对象中,两个对象都有一个实例(而不是继承)。本质上,这将是您的委托(delegate)的委托(delegate)(这很好)。或者,如果您有一组相关的共享函数,您可以将它们设为某些策略类的类方法。但如果只是像这样的单个方法,一个函数就可以了。

关于ios - 删除委托(delegate)方法的 2 个实现中的重复代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27824796/

相关文章:

jquery - iOS 上的 CSS3 和 jQuery 过渡 Flash

ios - 如何将数据放入自定义单元格中的按钮?

swift - 如何跨不同 View Controller 快速更改某些单元格的 View

ios - 重用后,tableview 单元格的背景层错误

ios - 找不到接受 Swift 中提供的参数的 “init” 的重载

iphone - 使用 AVFoundation 在自定义播放器中播放 youtube 视频

ios - 创建通知场景后如何将它添加到 WatchKit 目标?

ios - 如何让我的动画功能 swift 依赖于 objective-c 麦克风打击检测?

iOS UIView 动画取消不起作用

objective-c - 如何覆盖 NSWindow?