1

我有一些代码,这是一小段摘录:

surfaceDimension = self.window.frame.size.height;
sliderDistancePerPoint = 1.0f / surfaceDimension / FINGER_RATIO;
addThisToSlider = sliderDistancePerPoint * diff * 1.6f;

在一个子类中,我覆盖了这个方法,并重复相同的代码,但旋转了:

surfaceDimension = self.window.frame.size.width;
sliderDistancePerPoint = 1.0f / surfaceDimension / FINGER_RATIO;
addThisToSlider = sliderDistancePerPoint * diff * 1.6f;

我可以通过哪些(各种)方法来虚拟化此代码以便它可以在任一方向上工作?我当然可以制作surfaceDimension一个方法,但我需要对所有依赖项重复此操作。其他想法?

4

1 回答 1

0

使用标准的 OO 实践并使方法足够通用,您可以很容易地进行此操作。这是一个相当不错的继承使用

// override in subclass
- (BOOL) useXOrientation {
    return NO;
}

- (CGPoint) setOriginFrom:(CGPoint)sourcePoint to:(CGPoint)targetPoint {
    if (self.useXOrientation)
        targetPoint.x = sourcePoint.x;
    else
        targetPoint.y = sourcePoint.y;
    return targetPoint;
}



- (float) sizeOfDimension:(CGSize)size {
    if (self.useXOrientation)
        return size.width;
    else
        return size.height;
}

// example method
- (void) resetFirstTouch:(CGPoint)point {
    self.firstTouch = [self setOriginFrom:point to:self.firstTouch];
    self.firstTouchInParent = [self setOriginFrom:[self convertPoint:self.firstTouch toView:self.superview] to:self.firstTouchInParent];
}
于 2013-05-08T23:17:05.420 回答