我有一个ViewController类和一个View类。 ViewController有一个UIScrollView,View有一个UITableView,都是UIScrollViewDelegate的委托。
View和ViewController都使用scrollViewDidScroll:
委托方法,并且两者都有相同的代码。这显然是一个重复,所以我想解决这个问题。
问题是我不知道怎么做。
我无法将功能提取到基类中,我也不认为我可以有一个单独的类来实现该方法,我可以使用它的实例(它可以在Android中使用,但不能在iOS中使用)。我正在考虑使用积木,但不知道它们是如何在这里工作的。
该方法本身在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));
}
}
}
答案 0 :(得分:1)
创建一个封装所需内容的函数,并将任何所需的变量(可能包括self
)传递给它。
如果对象之间有更多重复的逻辑,则可以将它们组合到一个策略对象中,该对象具有一个实例(而不是继承)。基本上这将是你的代表的委托(这很好)。或者如果你有一组相关的共享函数,你可以使它们成为某些策略类的类方法。但如果它只是这样的一个方法,那么函数就可以了。
答案 1 :(得分:0)
我之前只提供了以下相关代码,以便您了解如何使用单独的课程来完成此目标。
@interface MyScrollDelegate : NSObject <UIScrollViewDelegate>
@property (weak) id originalSelf;
@end
@interface MyViewController
@property (strong) MyScrollDelegate *aScrollDelegate;
@end
@interface MyView
@property (strong) MyScrollDelegate *aScrollDelegate;
@end
@implementation MyViewController
- (void)loadView {
self.aScrollDelegate.originalSelf = self;
myScrollView.setDelegate = self.aScrollDelegate;
}
@end
@implementation MyView
- (instancetype)init {
self.aScrollDelegate.originalSelf = self;
myScrollView.setDelegate = self.aScrollDelegate;
}
@end